2016-12-03 38 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

太好了。这正是我的第一个问题,你可以看到:) –

相关问题