c# - UWP SolidColorBrush - 应用程序调用为不同线程异常编码的接口(interface)

标签 c# multithreading xaml uwp

我有一个通过 SignalR 接收动态更新的 UWP 应用程序。我使用的是 Template10,SignalR 监听器位于 ViewModel 类中。

当 SignalR 收到消息时 - 模型会更新。更新模型的代码块包装在 Despatcher 方法中:

VM - SignalR 调用的方法:

private async void AddOrder(WorkOrder order)
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            order.Lines = new ObservableCollection<WorkOrderLine>(order.Lines.OrderByDescending(m => m.QtyScanned < m.Qty);
            this.Orders.Add(order);
        });
    }

然后在模型类中我有以下代码(WorkOrderLine 类上还有另一个子 observablecollection):

private void TrolleyAllocations_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged("WorkOrderLineItems");
            ForegroundColor = GetForegroundColour();
        }

GetForegroundColor 如下:

private SolidColorBrush GetForegroundColour()
    {
        try
        {
            if (WorkOrderLineItems.Where(m => m.Status == UnitStatus.Other).Any())
            {
                return new SolidColorBrush(Colors.Red);
            }
            else if (WorkOrderLineItems.Where(m => m.Status == UnitStatus.AssemblyLine).Any())
            {
                return new SolidColorBrush(Colors.Green);
            }
            else if (WorkOrderLineItems.Where(m => m.Status == UnitStatus.PreLoad).Any())
            {
                return new SolidColorBrush(Colors.Black);
            }
            else if (WorkOrderLineItems.Where(m => m.Status == UnitStatus.FullAndComplete).Any())
            {
                return new SolidColorBrush(Colors.LightGray);
            }

            return new SolidColorBrush(Colors.Black);
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"Exception in foreground colour: {ex.Message} {ex.StackTrace}");
            return null;
        }
    }

现在,在任何 new SolidColorBrush() 上都会引发以下异常:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

在最近的更改之前,我使用 x:Bind 中的转换器来完成 GetForegroundColor 方法正在执行的工作(由于转换器会导致性能下降,我决定更改该方法) - 并且它工作得很好。我还更新了一些其他数据绑定(bind)属性 - 更新了 UI(代码省略),并且效果很好。

任何想法都将不胜感激。这让我发疯。

最佳答案

您需要在主线程上运行模型的更改。 我在使用 MVVM 的 UWP 中遇到了同样的问题。

我认为您需要使用调度程序包装事件的处理程序代码,以便在 UI 线程上运行它。

private void TrolleyAllocations_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
      Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
      {
           RaisePropertyChanged("WorkOrderLineItems");
           ForegroundColor = GetForegroundColour();
      }
}

现在您的处理程序被调用并从后台任务中触发,因此 GetForegroundColor() 位于同一线程上。

关于c# - UWP SolidColorBrush - 应用程序调用为不同线程异常编码的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45090531/

相关文章:

c# - 在保存文件之前确保目录存在或根据需要创建

Android IPC LocalSocket 与 Binder (AIDL)

c# - WPF C# 如何为 HighlightBrush 颜色设置动画?

c# - MVVM 可以在 View 中命令控件吗?

c# - 表单例份验证在 asp.net 中如何工作?

c# - 从数组到数据表

c# - Breeze 自定义操作

java - for循环中的同步方法

java - 这些 sleep 线程如何工作?

wpf - 为什么WPF边框控件没有mousedoubleclick事件?