Xamarin.Forms MvvmCross:解决UI项目中的IMvxMessenger

时间:2018-06-25 08:27:13

标签: xamarin.forms mvvmcross messenger

我下载了nuget软件包以显示弹出窗口:https://github.com/rotorgames/Rg.Plugins.Popup

我有两个.NetStandart:Core和UI项目。 UI依赖于Core。 我需要通过从ViewModel调用它来显示一个弹出窗口。

然后我创建一个类来订阅我的消息:

public class PopupService : IPopupService
    {
        private IMvxMessenger _messenger;


        public static void Init()
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                var messenger = Mvx.Resolve<IMvxMessenger>();
                Mvx.RegisterSingleton<IPopupService>(() => new PopupService(messenger));
            });
        }

        public PopupService(IMvxMessenger messenger)
        {
            _messenger = messenger;

            _messenger.Subscribe<AlertPopupMessage>(async (message) => await AlertPopupCallbackAsync(message));
        }


        public async Task AlertPopupCallbackAsync(AlertPopupMessage message)
        {
            var alertPopup = new AlertPopup(message.PopupInfo);
            await PopupNavigation.PushAsync(alertPopup);
        }   
    }

请注意 Init 方法。当我尝试解决IMvxMessenger时,出现了 MvvmCross.Exceptions.MvxIoCResolveException

UI.App.xaml.cs 类:

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            PopupService.Init();
        }
    }

0 个答案:

没有答案