【问题标题】:Setting up Windows Phone 8.1 project in Xamarin Forms solution with MvvmCross使用 MvvmCross 在 Xamarin Forms 解决方案中设置 Windows Phone 8.1 项目
【发布时间】:2016-12-06 00:22:05
【问题描述】:

我一直在开发我的 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);
    }
}

【问题讨论】:

  • 您拥有的是基于 Windows Phone Silverlight 还是 Windows Phone WinRT 的应用程序?
  • 它是 WinRT 应用程序
  • 您的应用在打开首页时崩溃。您是否定义了任何静态资源?您可以将示例项目上传到某个地方吗?
  • 我没有在第一页定义任何图标或任何文件,它实际上是简单的视图痒标签和按钮。如果有办法解决它,我不想上传我的项目,但如果有需要,我可以明天上传。顺便说一句,感谢您的回复。
  • 我想帮忙,事实上我在为 iOS/Android/UWP 配置 mvvmcross 项目时遇到了类似的错误。但它不是 Xamarin.Forms

标签: windows-phone-8.1 xamarin.forms mvvmcross


【解决方案1】:

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

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

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

【讨论】:

  • 太棒了。如您所见,这正是我的第一个问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多