我无法将异步方法的结果传播到UI。
XAML
<Window x:Class="COVMin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:COVMin"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid Margin="0,0,0,0">
<Border BorderThickness="1" BorderBrush="Black" Background="White" Margin="4" VerticalAlignment="Top" Height="170">
<Polygon Points="{Binding Points}" Stretch="Fill" Fill="Black" Opacity="0.8" />
</Border>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="24" Marin="0,0,0,10" VerticalAlignment="Bottom" Width="75" Command="{Binding DrawPointsCommand}"/>
</Grid>
视图模型
class MainViewModel : ViewModelBase
{
private PointCollection points { get; set; }
public PointCollection Points
{
get { return this.points; }
set
{
this.points = value;
OnPropertyChanged("Points");
}
}
public ICommand DrawPointsCommand { get; private set; }
/// <summary>
/// Simplified, in real it´s long time operation causing UI to freeze.
/// </summary>
private Task<PointCollection> ConvertToPointCollection()
{
return Task.Run<PointCollection>(() =>
{
PointCollection points = new PointCollection();
points.Add(new System.Windows.Point(0, 6236832));
points.Add(new System.Windows.Point(255, 6236832));
return points;
});
}
/// <summary>
///
/// </summary>
private async Task<PointCollection> Process()
{
this.Points = await ConvertToPointCollection();
return this.Points;
}
/// <summary>
/// Method calling long-time operation bound to button as a Command.
/// </summary>
private async void GetValues()
{
this.Points = await Process();
}
/// <summary>
/// Constructor.
/// </summary>
public MainViewModel()
{
this.DrawPointsCommand = new DelegateCommand(GetValues);
}
}
ViewModelBase
/// <summary>
/// Base class for PropertyChanged event handling.
/// </summary>
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
DelegateCommand类
public class DelegateCommand : ICommand
{
private readonly Action _action;
public DelegateCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
问题出在OnPropertyChange上,导致System.ArgumentException告诉我必须在与DependencyObject相同的线程上创建DependencySource。我花了相当多的时间尝试使用Dispatchers,但事实并非如此。
答案 0 :(得分:0)
主要问题是PointCollection
是DependencyObject
,因此由创建它的线程拥有。通常,您不能在其他线程中使用此类对象。使用Dispatcher
(显式或隐式地使用await
作为您的代码示例)在这里没有用,因为它不是拥有该对象的UI线程。实际上,它正试图在导致问题的UI线程上使用该对象。
对于DependencyObjects
同样是Freezable
对象,“{不是跨线程共享”规则有一个重要的例外,因为PointCollection
是ConvertToPointCollection()
对象。如果在任何其他线程尝试访问它之前冻结拥有线程中的对象,那么在其他线程中使用它是安全的。
因此,您可以将private Task<PointCollection> ConvertToPointCollection()
{
return Task.Run<PointCollection>(() =>
{
PointCollection points = new PointCollection();
points.Add(new System.Windows.Point(0, 6236832));
points.Add(new System.Windows.Point(255, 6236832));
points.Freeze();
return points;
});
}
方法更改为:
PointCollection
当然,这也会阻止以后修改对象。如果您需要能够修改集合,那么您将不得不采取不同的方法。例如,在UI线程中创建List<Point>
,然后使用中间类型(如PointCollection
)将新点从后台线程传递到UI线程,然后UI线程可以复制那些指向beforeInsert hook
。