MvvmCross、UWP、MessengerPlugin:加载页面失败
MvvmCross, UWP, MessengerPlugin: Failed to load Page
问题是:当我将IMvxMessanger的构造函数DI添加到ViewModel时,我得到了一个错误:“无法加载页面”;如果我删除这个 DI,我的工具就可以正常工作。顺便说一句,IHelloService 不会给出这个错误,它只工作正常 w/o IMvxMessanger.
我遵循了本手册:https://mvvmcross.com/docs/a-windows-universal-app-platform-project
MvvmCross 版本:4.4.0
代码示例:
FirstViewModel
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();
}
我认为问题在于您在 UWP 项目中缺少 MvxMessenger 的 bootstrap class。 UWP 使用 Nuget 3 引入的 project.json 模板。目前,当您安装 nuget 包时,它们不允许将附加文件添加到您的项目中。
解决方法是手动添加bootstrap文件夹和相关插件bootstrap.cs
文件,或者您可以在Setup.cs
.[=17中注册插件的接口和实现=]
Bootstrap 方法:
创建 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());
}
问题是:当我将IMvxMessanger的构造函数DI添加到ViewModel时,我得到了一个错误:“无法加载页面”;如果我删除这个 DI,我的工具就可以正常工作。顺便说一句,IHelloService 不会给出这个错误,它只工作正常 w/o IMvxMessanger.
我遵循了本手册:https://mvvmcross.com/docs/a-windows-universal-app-platform-project
MvvmCross 版本:4.4.0
代码示例:
FirstViewModel
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();
}
我认为问题在于您在 UWP 项目中缺少 MvxMessenger 的 bootstrap class。 UWP 使用 Nuget 3 引入的 project.json 模板。目前,当您安装 nuget 包时,它们不允许将附加文件添加到您的项目中。
解决方法是手动添加bootstrap文件夹和相关插件bootstrap.cs
文件,或者您可以在Setup.cs
.[=17中注册插件的接口和实现=]
Bootstrap 方法:
创建 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());
}