简体   繁体   English

收到Messenger.default.Receive后,UWP MvvmLight更新UI

[英]UWP MvvmLight Update UI after getting Messenger.default.Receive

I am using MVVMLight in an UWP application, I have two screens: on MainScreen I have a button to open a second screen, and I also have another button to send some data from MainScreen to the second screen. 我在UWP应用程序中使用MVVMLight,我有两个屏幕:在MainScreen ,有一个按钮可打开第二个屏幕,还有另一个按钮,可将一些数据从MainScreen发送到第二个屏幕。 I am using 我在用

Messenger.Default.Send(someobject)

and similarly 同样

Messenger.Default.Register<Some>(this, (action) => ReceiveMsg(action));

I have to click the button on the main screen and send data to other view. 我必须单击主屏幕上的按钮,然后将数据发送到其他视图。 The problem is the data is not getting updated on the second screen, and causing an exception 问题是数据没有在第二个屏幕上更新,并导致异常

The application called an interface that was marshalled for a different thread 该应用程序调用了一个已编组为不同线程的接口

I tried several ways to update UI, like this , this in fact I have tried all these possibilities like following 我尝试了几种方法来更新UI,像这样这个其实我已经试过像以下这些可能性

 private async void ReceiveMsg(Some action)
    {
        try
        {
            //await Task.Factory.StartNew(() => {
            //    T1 = action.T1;
            //    RaisePropertyChanged(() => T1);
            //});

            //SharingData.UpdateScore(action);
            //DispatcherHelper.Initialize();

            //await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal
            //     , () =>
            //     {
            //    T1 = action.T1;
            //    RaisePropertyChanged(() => T1);
            //     });

            //await Dispatcher.DispatchAsync(() =>
            //{
            //    T1 = action.T1;
            //    RaisePropertyChanged(() => T1);
            //},1000, Windows.UI.Core.CoreDispatcherPriority.Normal);

            //await Window.Current.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            //{
            //    T1 = action.T1;
            //    RaisePropertyChanged(() => T1);
            //});

            //var views= Windows.ApplicationModel.Core.CoreApplication.Views.ToList()[0];
            //await views.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
            //    T1 = action.T1;
            //    RaisePropertyChanged(() => T1);
            //});


            var thiswindow = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;

            await thiswindow.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
             {
                 team1 = action.Team1;
                 this.RaisePropertyChanged("Team1");

             });

            //DispatcherHelper.CheckBeginInvokeOnUI(
            //() =>
            //{
            //    T1 = action.T1;
            //    RaisePropertyChanged(() => T1);
            //});
            //DispatcherHelper.Reset();
            //DispatcherHelper.Initialize();

        }
        catch (Exception ex)
        {
            //DispatcherHelper.Reset();
            //Console
        }
    }

I have tried all the above segments one by one, but nothing works and still getting the "marshalled for a different thread" error. 我已经尝试了上述所有段,但是没有任何效果,仍然出现“编组为不同线程”的错误。

Please tell me what I am doing wrong? 请告诉我我做错了什么? The property is getting update, like T1='Some Value' but not reflecting on UI and on RaisePropertyChanged its giving the exception. 该属性正在更新,例如T1 ='Some Value',但没有反映在UI和RaisePropertyChanged上,给出了异常。

I have to click the button on the main screen and send data to other view. 我必须单击主屏幕上的按钮,然后将数据发送到其他视图。 The problem is the data is not getting updated on the second screen, and causing an exception 问题是数据没有在第二个屏幕上更新,并导致异常

The application called an interface that was marshalled for a different thread. 该应用程序调用了一个已编组为不同线程的接口。

The Send and Receive action of Messenger will be invoked in same thread. MessengerSendReceive操作将在同一线程中调用。 When you click the button in the MainScreen , the CoreWindow.Dispatcher is dispatcher of MainScreen . 当您单击MainScreen的按钮时, CoreWindow.DispatcherMainScreen调度程序。 If you use this dispatcher to modify the content of second screen, it will throw exception. 如果使用此调度程序修改第二个屏幕的内容,它将引发异常。

To resolve it, you should invoke Send method in the main thread of second screen. 要解决此问题,应在第二个屏幕的主线程中调用Send方法。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    newView = CoreApplication.CreateNewView();
    int newViewId = 0;
    await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        var id = Environment.CurrentManagedThreadId;
        Frame frame = new Frame();
        frame.Navigate(typeof(SecondPage), null);
        Window.Current.Content = frame;

        Window.Current.Activate();

        newViewId = ApplicationView.GetForCurrentView().Id;
    });
    bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}

private async void SendBtn_Click(object sender, RoutedEventArgs e)
{
    await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        var id = Environment.CurrentManagedThreadId;
        Messenger.Default.Send("Test send message!!!");
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM