某些订阅事件使用ThreadOption.UIThread。其他人使用ThreadOption.Background。我只是简单地将Background作为一个好主意。我也不一致。
当特定的MVVM可见且FuelCellsNearUpdated事件不断发送不同的值时,带有ThreadOption.BackgroundThread的带Subscribe事件的所有可见屏幕窗口似乎在大约2分钟后停止更新。带有仅基于UIThread的MVVM的其他屏幕窗口区域可以正常更新。
与其他更新值或矩形的屏幕窗口不同,FuelCellsNearUpdated导致XAML ItemsControl更新3x3表来更新图像和文本。如果FuelCellsNearUpdated值未更改或窗口不可见,则屏幕窗口不会停止更新。
我正在寻找有关为何使用ThreadOption.Background时订阅事件似乎停止工作的解释。我需要放心地解释一下,当我在任何地方都使用ThreadOption.UIThread时,导致屏幕窗口停止更新的问题已解决。
如果我将所有事件都更改为ThreadOption.UIThread,即使在连续数天与FuelCellsNearUpdated进行连续活动之后,所有屏幕窗口区域也会按预期更新。
Some code segments for a few view models:
ShellViewModel:
public ShellViewModel(IDbService dbService, ITagDataService tagDataService, IEventAggregator eventAggregator)
{}
private void RenderUpdateLoop()
{ ...
EventAggregator.GetEvent<TagDataUpdatedEvent>().Publish(hmiDataInfo);
EventAggregator.GetEvent<FuelCellInfoNearUpdatedEvent>().Publish(fuelCellsNear);
...
EventAggregator.GetEvent<UpdateHmiClocksEvent>().Publish(true);
...}
WinOperFooterViewModel:
[ImportingConstructor]
public WinOperFooterViewModel(ITagDataService tagDataService, IEventAggregator eventAggregator)
{
EventAggregator = eventAggregator;
this.tagDataService = tagDataService;
EventAggregator.GetEvent<TagDataUpdatedEvent>().Subscribe(TagDataUpdated, ThreadOption.UIThread);
EventAggregator.GetEvent<LogMsgEvent>().Publish("WinFooterViewModel initialization complete.");
}
WinOperFuelCellsViewModel:
public WinOperFuelCellsViewModel(ITagDataService tagDataService, IEventAggregator eventAggregator, IDbService dbService)
{...
EventAggregator.GetEvent<FuelCellInfoNearUpdatedEvent>().Subscribe(FuelCellsNearUpdated, ThreadOption.BackgroundThread);
EventAggregator.GetEvent<TagDataUpdatedEvent>().Subscribe(TagValuesUpdated, ThreadOption.BackgroundThread);
...}
WinOperFuelCellsView.xaml:
...
<Grid x:Name="Grid">
<Viewbox x:Name="Viewbox" Stretch="Uniform" StretchDirection="Both">
<Canvas x:Name="UnitCanvas"
Width="{Binding ContentWidth}"
Height="{Binding ContentHeight}"
Background="Transparent">
<ItemsControl Name="ChessBoard" ItemsSource="{Binding Cells}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Transparent" DataContext="{Binding}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="BorderCell" >
<Viewbox StretchDirection="DownOnly" Panel.ZIndex="100">
<TextBlock x:Name="CurrentCell" Foreground="White" Text="{Binding Path=CellName}">
<TextBlock.LayoutTransform>
<RotateTransform Angle="{Binding Path=TextRotation}" />
</TextBlock.LayoutTransform>
</TextBlock>
</Viewbox>
<Image Panel.ZIndex="1" Style="{StaticResource ChessPieceStyle}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding ScalePos.X}" />
<Setter Property="Canvas.Top" Value="{Binding ScalePos.Y}" />
<Setter Property="Canvas.Width" Value="{Binding Width}" />
<Setter Property="Canvas.Height" Value="{Binding Height}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
<Canvas.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="{Binding Path=ScreenRotation}" />
<TranslateTransform Y="{Binding ActualWidth, ElementName=Viewbox}" />
</TransformGroup>
</Canvas.LayoutTransform>
</Canvas>
</Viewbox>
</Grid>
....
如果ThreadOption.Background是一个问题,我希望它永远无法工作。