繁体   English   中英

将XNA添加到Silverlight WP7项目

[英]Add XNA to Silverlight WP7 project

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

我尝试在解决方案中制作“ Windows Phone Silverlight和XNA”项目(BattleSimulator),然后导航到GamePage.xaml。 但是我在(Application.Current as App)上收到了一个N​​ullReferenceException和一个警告。 该警告在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-fails计划。

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

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

null引用很可能是as运算符使对App的类型转换失败的原因,当您尝试在as的结果上查找Content属性时,它将为您留下NullReferenceException。

对于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; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM