MvvmCross, UWP, MessengerPlugin: Failed to load Page

384 views Asked by At

Problem is: when i added constructor DI of IMvxMessanger to ViewModel i got an error: "Failed to load Page"; If i'll remove this DI, my tool works fine. Btw, IHelloService will not give this error, it works fine only w/o IMvxMessanger.

I followed this manual: https://mvvmcross.com/docs/a-windows-universal-app-platform-project

MvvmCross version: 4.4.0

Code examples:

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>

Setup

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

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

Also this lines in App.xaml.cs

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

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

There are 1 answers

5
Plac3Hold3r On BEST ANSWER

I believe the issue is that you are missing the bootstrap class for MvxMessenger in your UWP project. UWP uses the project.json template that was introduced with Nuget 3. At present, when you install nuget packages they are not allow to add addition file to your project.

The workaround is to manually add the bootstrap folder and relevant plugin bootstrap.cs file, or you can register the interface and implementation of the plugin in your Setup.cs.

Bootstrap Approach:

Create a MessengerPluginBootstrap.cs

using MvvmCross.Platform.Plugins;

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

Setup.cs Approach:

Register the interface of IMvxMessenger against the MvxMessengerHub implementation.

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