【问题标题】:MvvmCross, UWP, MessengerPlugin: Failed to load PageMvvmCross、UWP、MessengerPlugin:无法加载页面
【发布时间】:2017-05-08 00:26:55
【问题描述】:

问题是:当我将 IMvxMessanger 的构造函数 DI 添加到 ViewModel 时出现错误:“加载页面失败";如果我要删除这个 DI,我的工具可以正常工作。顺便说一句,IHelloService 不会给出这个错误,它只有在没有 IMvxMessanger 的情况下才能正常工作。

我遵循了这本手册:https://mvvmcross.com/docs/a-windows-universal-app-platform-project

Mvvm跨版本:4.4.0

代码示例:

第一视图模型

public class FirstViewModel : MvxViewModel
{
    private readonly IHelloService _helloService;
    private readonly IMvxMessenger _messenger;

    public FirstViewModel(IHelloService helloService, IMvxMessenger messenger)
    {
        _helloService = helloService;
        _messenger = messenger;
    }

    private string _hello = "Hello MvvmCross";
    public string Hello
    {
        get { return _hello; }
        set { SetProperty(ref _hello, value); }
    }
}

FirstView.xaml.cs

public sealed partial class FirstView : MvxWindowsPage
{
    public FirstView()
    {
        InitializeComponent();
    }
}

FirstView.xaml

<views:MvxWindowsPage
x:Class="MvvmCrossDocs.UWP.Views.FirstView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MvvmCrossDocs.UWP.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:MvvmCross.WindowsUWP.Views"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <TextBox Text="{Binding Hello, Mode=TwoWay}" />
        <TextBlock Text="{Binding Hello}" />
    </StackPanel>
</Grid>

设置

public class Setup : MvxWindowsSetup
{
    public Setup(Frame rootFrame) : base(rootFrame)
    {
    }

    protected override IMvxApplication CreateApp()
    {
        return new Core.App();
    }
}

App.xaml.cs 中也是这一行

if (rootFrame.Content == null)
{
    var setup = new Setup(rootFrame);
    setup.Initialize();

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

【问题讨论】:

    标签: c# mvvmcross xamarin.uwp


    【解决方案1】:

    我认为问题在于您在 UWP 项目中缺少 MvxMessenger 的引导类。 UWP 使用 Nuget 3 引入的 project.json 模板。目前,当您安装 nuget 包时,它们不允许将附加文件添加到您的项目中。

    解决方法是手动添加bootstrap文件夹和相关插件bootstrap.cs文件,或者你可以在你的Setup.cs注册插件的接口和实现。

    引导方法:

    创建MessengerPluginBootstrap.cs

    using MvvmCross.Platform.Plugins;
    
    namespace <<YOUR_NAMESSPACE>>.Bootstrap
    {
        public class MessengerPluginBootstrap
                : MvxPluginBootstrapAction<MvvmCross.Plugins.Messenger.PluginLoader>
        {
        }
    }
    

    Setup.cs 方法:

    针对MvxMessengerHub实现注册IMvxMessenger的接口。

    protected override void InitializeLastChance()
    {
        base.InitializeLastChance();
        Mvx.RegisterSingleton<IMvxMessenger>(new MvxMessengerHub());
    }
    

    【讨论】:

    • 我从我的 Droid 项目中复制了 Bootstrap 文件夹并解析了命名空间 -> 0 个更改,得到了同样的错误。
    • 我也在 Sturtup.cs 文件中尝试了InitializeLastChance。同样的错误:(
    • 嗯,很有趣。你有异常的堆栈跟踪吗?您是否尝试过清洁和重建?
    • 我创建了一个测试存储库,您可以使用 TipCalc 示例来签出该存储库,该存储库会引入 MvxMessenger。 github.com/Plac3hold3r/TipCalc_MvxMassager
    • 这真的很奇怪,因为我按照你在新项目中所说的做了,而且效果很好!格拉茨!
    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多