簡體   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