簡體   English   中英

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

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

我在UWP應用程序中使用MVVMLight,我有兩個屏幕:在MainScreen ,有一個按鈕可打開第二個屏幕,還有另一個按鈕,可將一些數據從MainScreen發送到第二個屏幕。 我在用

Messenger.Default.Send(someobject)

同樣

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

我必須單擊主屏幕上的按鈕,然后將數據發送到其他視圖。 問題是數據沒有在第二個屏幕上更新,並導致異常

該應用程序調用了一個已編組為不同線程的接口

我嘗試了幾種方法來更新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
        }
    }

我已經嘗試了上述所有段,但是沒有任何效果,仍然出現“編組為不同線程”的錯誤。

請告訴我我做錯了什么? 該屬性正在更新,例如T1 ='Some Value',但沒有反映在UI和RaisePropertyChanged上,給出了異常。

我必須單擊主屏幕上的按鈕,然后將數據發送到其他視圖。 問題是數據沒有在第二個屏幕上更新,並導致異常

該應用程序調用了一個已編組為不同線程的接口。

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!!!");
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM