将XNA添加到Silverlight WP7项目

时间:2012-02-27 14:24:34

标签: silverlight windows-phone-7 xna nullreferenceexception

我目前正在为WP7制作游戏,主要是在Silverlight中制作的。但现在我需要一个可以使用XNA的页面。 XNA页面将接收战斗数据,然后为用户显示它。

我已尝试在我的解决方案中制作“Windows Phone Silverlight和XNA”项目(BattleSimulator)并导航到GamePage.xaml。但是我得到一个NullReferenceException(Application.Current as App)和一个警告。警告在BattleSimulator项目中。

警告

Warning 1   The project 'BattleSimulatorLib' cannot be referenced.  The referenced project is targeted to a different framework family (.NETFramework)  

代码

 public GamePage()
    {
        InitializeComponent();

        // Get the content manager from the application
        contentManager = (Application.Current as App).Content; //NullReference here

堆栈跟踪

   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)

解决问题的一种方法是将我的所有silverlight类和标记复制到新的SL和XNA项目中,但这是我的if-all-else-failed计划。

有谁知道如何解决这个NullReferenceException或警告?或者我应该尝试以其他方式做到这一点?

编辑:在此处找到有关警告的信息:http://forums.create.msdn.com/forums/p/93769/561676.aspx 这是无害的。

1 个答案:

答案 0 :(得分:0)

空引用很可能是as运算符未能将类型转换为App,当您尝试在结果上找到Content属性时,会给您留下NullReferenceException。 as

对于SL / XNA项目,还有一些在应用程序全局级别定义的额外内容以及App对象中的一些额外样板代码。这些是SL / XNA特定的,没有为纯SL项目定义。对于SL / XNA项目,还需要对XNA类库进行一些额外的项目引用。

如果您仔细研究标准SL和SL / XNA基本应用程序项目之间的差异,可以手动将此额外代码添加到现有SL项目中,但在您的情况下创建新的SL / XNA可能更容易项目并将代码移入其中。

例如,一些新增内容(这不包括额外的样板代码,因此请自行检查)

来自App.xaml:

<!--The SharedGraphicsDeviceManager is used to render with the XNA Graphics APIs-->
<xna:SharedGraphicsDeviceManager />

来自App.xaml.cs:

    /// <summary>
    /// Provides access to a ContentManager for the application.
    /// </summary>
    public ContentManager Content { get; private set; }

    /// <summary>
    /// Provides access to a GameTimer that is set up to pump the FrameworkDispatcher.
    /// </summary>
    public GameTimer FrameworkDispatcherTimer { get; private set; }

    /// <summary>
    /// Provides access to the AppServiceProvider for the application.
    /// </summary>
    public AppServiceProvider Services { get; private set; }