使用MVVMCross设置Windows Phone 8.1项目



我已经开发了我的Xamarin表单应用程序了一段时间,现在我想将Windows Phone Project添加到其中。我试图通过MVVMCross GitHub项目进行指南进行设置,但似乎已经过时了。

首先,我在解决方案中添加了Windows Phone 8.1空白应用程序项目,并进行了指南。我添加了我需要的nuget软件包,没有任何问题,但是当我修改app.xaml.cs and mainpage.xaml.cs文件时,我无法使用示例中使用的类和教程。当我将项目中的参考与示例MVVMCross项目中的参考进行比较时,我发现了一些差异。

我的参考

对我来说可疑的是这个mvvmcross.forms.presenter.windows81参考。在查看了我看到的Xamarin表格的MVVMCross存储库后,有MVVMCross.Forms.Presenter.WindowsPhone名称空间和View Ervanserer。所以我在想,也许这是问题。

在尝试和混合代码与Xamarin.Forms指南和我的UWP项目后,我来到了该地点,在某种程度上,应用程序要盯着,方法应该执行(当我仅遵循MVVMCross指南时,我的第一个视图模型尚未构建),但是应用程序遇到了这个stacktrace的崩溃:

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

好的。今天我想,我会再给它一个镜头,如果我无法运行它,那么我将发布我的代码。我挣扎了大约一个小时,终于设法运行了我的开始页面。

我已经打开了此示例,并基本上替换了Mainpage.xaml.cs,app.xaml.cs和setup.cs的内容。然后我开始了构建,瞧,我的主页出现了。

我的问题是我没有注意到,该MVVMCross教程针对Windows Phone Silverlight。无论如何,感谢您的回复和祝您好运。

最新更新