使用 MvvmCross 在 Xamarin Forms 解决方案中设置 Windows Phone 8.1 项目

Setting up Windows Phone 8.1 project in Xamarin Forms solution with MvvmCross

我已经开发我的 Xamarin Forms 应用程序一段时间了,现在我想向它添加 Windows Phone 项目。我曾尝试使用 MvvmCross GitHub 项目的指南进行设置,但它似乎已过时。

起初我在解决方案中添加了Windows Phone 8.1 空白应用程序项目并继续使用指南。我已经添加了我需要的 NuGet 包,没有任何问题,但是当我修改 App.xaml.cs 和 MainPage.xaml.cs 文件时,我无法使用 类用于示例和教程。当我将项目中的引用与示例 MvvmCross 项目中的引用进行比较时,我发现了一些差异。

My references

对我来说可疑的是这个 MvvmCross.Forms.Presenter.Windows81 参考。在查看了 Xamarin Forms 的 MvvmCross 存储库后,我发现有 MvvmCross.Forms.Presenter.WindowsPhone 名称空间和视图展示器。所以我在想,也许这就是问题所在。

经过数小时的尝试并将代码与 Xamarin.Forms 指南和我的 UWP 项目混合后,我来到了这个地方,应用程序以某种方式凝视,应该执行的方法正在执行(当我跟随仅 MvvmCross 指南,我的第一个视图模型尚未构建),但应用程序在这个堆栈跟踪开始时崩溃:

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);
    }
}

好的伙计们。今天我想,我会再试一次,如果我无法 运行 它,那么我将 post 我的代码。我挣扎了大约一个小时,终于 运行 我的起始页。

我打开了this sample,基本上用它们的实现替换了我的MainPage.xaml.cs、App.xaml.cs和Setup.cs的内容。然后我开始构建,瞧,我的主页出现了。

我的问题是我没有注意到 this MvvmCross tutorial 的目标是 Windows Phone Silverlight。无论如何,感谢您的回复,祝您项目顺利。