2016-12-03 62 views
0

我一直在開發我的Xamarin Forms應用程序一段時間,現在我想將Windows Phone項目添加到它。我試圖用MvvmCross GitHub項目的指南進行設置,但似乎過時了。使用MvvmCross在Xamarin Forms解決方案中設置Windows Phone 8.1項目

起初,我已將Windows Phone 8.1空白應用程序項目添加到解決方案,並繼續指導。我添加了我需要的NuGet包,但沒有任何問題,但是當我修改App.xaml.cs和MainPage.xaml.cs文件時,我無法使用樣本中使用的類和教程。當我將我的項目中的參考文獻與示例MvvmCross項目中的參考文獻進行比較時,我發現了一些差異。

My references

可疑的事情對我來說是這樣的MvvmCross.Forms.Presenter.Windows81參考。在查看MvvmCross存儲庫中的Xamarin Forms之後,我看到,有視圖展示器的MvvmCross.Forms.Presenter.Windows命名空間。所以我在想,也許這是問題所在。

經過幾個小時的嘗試並將代碼與Xamarin.Forms指南和我的UWP項目混合在一起之後,我來到了應用程序以某種方式凝視的地方,應執行的方法正在執行(當我僅遵循MvvmCross指南,我的第一個視圖模型尚未建立),但是應用在這個堆棧跟蹤非常beggining崩潰:

System.TypeLoadException was unhandled by user code 
HResult=-2146233054 
Message=Could not find Windows Runtime type 'Windows.Foundation'. 
Source=mscorlib 
TypeName=Windows.Foundation 
StackTrace: 
at System.StubHelpers.WinRTTypeNameConverter.GetTypeFromWinRTTypeName(String typeName, Boolean& isPrimitive) 
    at System.StubHelpers.SystemTypeMarshaler.ConvertToManaged(TypeNameNative* pNativeType, Type& managedType) 
    at Windows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter) 
    at MvvmCross.WindowsCommon.Views.MvxWrappedFrame.Navigate(Type viewType, Object parameter) 
    at MvvmCross.Forms.Presenter.Windows81.MvxFormsWindows81PagePresenter.CustomPlatformInitialization(NavigationPage mainPage) 
    at MvvmCross.Forms.Presenter.Core.MvxFormsPagePresenter.TryShowPage(MvxViewModelRequest request) 
    at MvvmCross.Forms.Presenter.Core.MvxFormsPagePresenter.Show(MvxViewModelRequest request) 
    at MvvmCross.WindowsCommon.Views.MvxWindowsViewDispatcher.<>c__DisplayClass2_0.<ShowViewModel>b__0() 
    at MvvmCross.WindowsCommon.Views.MvxWindowsMainThreadDispatcher.RequestMainThreadAction(Action action) 
    at MvvmCross.WindowsCommon.Views.MvxWindowsViewDispatcher.ShowViewModel(MvxViewModelRequest request) 
    at MvvmCross.Core.ViewModels.MvxNavigatingObject.ShowViewModelImpl(Type viewModelType, IMvxBundle parameterBundle, IMvxBundle presentationBundle, MvxRequestedBy requestedBy) 
    at MvvmCross.Core.ViewModels.MvxNavigatingObject.ShowViewModel[TViewModel](IMvxBundle parameterBundle, IMvxBundle presentationBundle, MvxRequestedBy requestedBy) 
    at MvvmCross.Core.ViewModels.MvxAppStart`1.Start(Object hint) 
    at PatrolHelper.WinPhone81.App.OnLaunched(LaunchActivatedEventArgs e) 

這裏是實現我App.xaml.cs文件:

protected override void OnLaunched(LaunchActivatedEventArgs e) 
    { 
#if DEBUG 
     if (System.Diagnostics.Debugger.IsAttached) 
     { 
      this.DebugSettings.EnableFrameRateCounter = true; 
     } 
#endif 

     Frame rootFrame = Window.Current.Content as Frame; 

     // Do not repeat app initialization when the Window already has content, 
     // just ensure that the window is active 
     if (rootFrame == null) 
     { 
      // Create a Frame to act as the navigation context and navigate to the first page 
      rootFrame = new Frame(); 

      // TODO: change this value to a cache size that is appropriate for your application 
      rootFrame.CacheSize = 1; 

      // Set the default language 
      rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; 

      Xamarin.Forms.Forms.Init(e); 

      if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) 
      { 
       // TODO: Load state from previously suspended application 
      } 

      // Place the frame in the current Window 
      Window.Current.Content = rootFrame; 
     } 

     if (rootFrame.Content == null) 
     { 
      // Removes the turnstile navigation for startup. 
      if (rootFrame.ContentTransitions != null) 
      { 
       this.transitions = new TransitionCollection(); 
       foreach (var c in rootFrame.ContentTransitions) 
       { 
        this.transitions.Add(c); 
       } 
      } 

      rootFrame.ContentTransitions = null; 
      rootFrame.Navigated += this.RootFrame_FirstNavigated; 
     } 

     Setup setup = new Setup(rootFrame); 
     setup.Initialize(); 

     IMvxAppStart start = Mvx.Resolve<IMvxAppStart>(); 
     start.Start(); 

     if (!rootFrame.Navigate(typeof(MainPage), e.Arguments)) 
     { 
      throw new Exception("Failed to create initial page"); 
     } 

     // Ensure the current window is active 
     Window.Current.Activate(); 
    } 

的MainPage .xaml:

<forms:WindowsPhonePage 
x:Class="PatrolHelper.WinPhone81.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:PatrolHelper.WinPhone81" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:forms="using:Xamarin.Forms.Platform.WinRT" 
mc:Ignorable="d" 
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

<Grid> 

</Grid> 

和MainPage.xaml.cs中

public sealed partial class MainPage : WindowsPhonePage 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 

     var start = Mvx.Resolve<IMvxAppStart>(); 
     start.Start(); 

     var presenter = Mvx.Resolve<IMvxViewPresenter>() as MvxFormsWindows81PagePresenter; 

     LoadApplication(presenter.MvxFormsApp); 
    } 

    /// <summary> 
    /// Invoked when this page is about to be displayed in a Frame. 
    /// </summary> 
    /// <param name="e">Event data that describes how this page was reached. 
    /// This parameter is typically used to configure the page.</param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 
    } 

    protected override void OnNavigatedFrom(NavigationEventArgs e) 
    { 
     base.OnNavigatedFrom(e); 
    } 
} 
+0

是Windows Phone Silverlight還是Windows Phone WinRT應用程序? –

+0

這是WinRT的應用程序 – dobowicz

+0

當您打開第一頁時,您的應用程序崩潰。你有沒有定義任何靜態資源?是否有可能在某處上傳示例項目? –

回答

0

好球員。今天我想,我會再給它一次,如果我不能運行它,那麼我會發布我的代碼。我掙扎了大約一個小時,終於成功運行了我的起始頁面。

我打開了this sample,基本上將我的MainPage.xaml.cs,App.xaml.cs和Setup.cs的內容替換爲它們的實現。然後我開始構建,瞧,我的主頁出現了。

我的問題是,我沒有注意到,this MvvmCross tutorial是針對Windows Phone Silverlight。無論如何感謝您的項目回覆和祝你好運。

+0

太好了。這正是我的第一個問題,你可以看到:) –

相關問題