【问题标题】:UWP MvvmLight Update UI after getting Messenger.default.Receive获取 Messenger.default.Receive 后 UWP MvvmLight 更新 UI
【发布时间】:2017-08-28 15:34:44
【问题描述】:

我在 UWP 应用程序中使用 MVVMLight,我有两个屏幕:在 MainScreen 上我有一个按钮可以打开第二个屏幕,我还有另一个按钮可以将一些数据从 MainScreen 发送到第二个屏幕。我正在使用

Messenger.Default.Send(someobject)

类似

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

我必须单击主屏幕上的按钮并将数据发送到其他视图。 问题是第二个屏幕上的数据没有更新,导致异常

应用程序调用了一个为不同线程编组的接口

我尝试了几种更新 UI 的方法,例如 thisthis 实际上我已经尝试了所有这些可能性,例如以下

 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
        }
    }

我已经一一尝试了上述所有部分,但没有任何效果,仍然出现“为不同的线程编组”错误。

请告诉我我做错了什么? 该属性正在更新,例如 T1='Some Value' 但未反映在 UI 和 RaisePropertyChanged 上,它给出了异常。

【问题讨论】:

    标签: c# xaml uwp mvvm-light


    【解决方案1】:

    我必须单击主屏幕上的按钮并将数据发送到其他视图。问题是第二个屏幕上的数据没有更新,导致异常

    应用程序调用了一个为不同线程编组的接口。

    MessengerSendReceive 动作将在同一个线程中调用。当您单击MainScreen 中的按钮时,CoreWindow.DispatcherMainScreen 的调度程序。如果你使用这个调度器修改第二屏的内容,它会抛出异常。

    要解决这个问题,你应该在第二屏的主线程中调用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!!!");
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-04
      • 2016-11-04
      • 2019-04-27
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多