I’m currently making a game for WP7 which is mostly made in Silverlight. But now I need a page where I can use XNA. The XNA page will receive data of a battle and then visualize it for the user.
I’ve tried making a “Windows Phone Silverlight and XNA” project(BattleSimulator) in my solution and navigate to the GamePage.xaml. But I get a NullReferenceException on (Application.Current as App) and a warning. The warning is in the BattleSimulator project.
Warning
Warning 1 The project 'BattleSimulatorLib' cannot be referenced. The referenced project is targeted to a different framework family (.NETFramework)
Code
public GamePage()
{
InitializeComponent();
// Get the content manager from the application
contentManager = (Application.Current as App).Content; //NullReference here
Stacktrace
at BattleSimulator.GamePage..ctor()
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Activator.InternalCreateInstance(Type type, Boolean nonPublic, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type)
at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
One way of solving it would be to copy all my silverlight classes and markups to a new SL and XNA project, but that’s my if-all-else-fails plan.
Does anyone know how to solve this NullReferenceException or warning? Or should I try to do this in some other way?
EDIT: Found info on the warning here: http://forums.create.msdn.com/forums/p/93769/561676.aspx
It is harmless.
The null reference is most likely that the
asoperator is failing the typecast toAppwhich will leave you with a NullReferenceException when you attempt to find theContentproperty on the result of theas.For a SL/XNA project there are a couple of extra things which are defined at the application global level along with some extra boilerplate code in your
Appobject. These are SL/XNA specific and not defined for a pure SL project. For SL/XNA projects some extra project references to XNA class libraries are also needed.It is possible to add this extra code manually to an existing SL project if you carefully study the differences between the standard SL and SL/XNA basic application projects, but it is probably easier in your case to create a new SL/XNA project and move your code into it.
For example some additions are (this doesn’t include the extra boilerplate code, so check for yourself)
From App.xaml:
From App.xaml.cs: