从异步任务中更新观察力的WPF会引发XamlParseException



当我尝试更新我从单独的线程中使用的XAML中使用的观测值时,我会得到一个XamlParseException,该XamlParseException说disportenceNcySource必须在同一线程上与同一线程创建为相同的线程依赖项。我正在使用Caliurn Micro将视图模型绑定到视图。

我尝试了几种实现目标的方法,以下似乎是我最合理的方法。我正在将syncronization -context从UI传递到任务,因此可以在完成重量工作量后更新UI。

我在做什么错?

查看

<Window x:Class="QuickScope.Views.NavigatorView"
    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:QuickScope.Views"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:quickScope="clr-namespace:QuickScope"
    mc:Ignorable="d">
<Grid>
    <TextBox Grid.Row="0" 
             Name="TextBox"
             HorizontalContentAlignment="Stretch"
             VerticalContentAlignment="Center"
             Margin="5,0,5,5"
             Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
    </TextBox>
    <Popup PlacementTarget="{Binding ElementName=TextBox}">
        <ListView x:Name="ItemList" ItemsSource="{Binding Items}" SelectedItem ="{Binding SelectedItem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <Image Source="{Binding IconSource}" Width="20" Height="20"></Image>
                        <Label Content="{Binding SearchName}"></Label>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Popup>
</Grid>

ViewModel

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using Caliburn.Micro;
using QuickScope.Views;
namespace QuickScope.ViewModels
{
public class NavigatorViewModel : Screen
{
    private readonly ItemService _itemService;
    public NavigatorViewModel()
    {
        _itemService = new ItemService();
        Items = new ObservableCollection<ItemViewModel>();
    }
    public ObservableCollection<ItemViewModel> Items { get; }
    public ItemViewModel SelectedItem { get; set; }
    private string _searchText;
    public string SearchText
    {
        get => _searchText;
        set
        {
            _searchText = value;
            NotifyOfPropertyChange(() => SearchText);
            UpdateItemList(_searchText);
        }
    }
    private void UpdateItemList(string searchText)
    {
        Items.Clear();
        var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
        Task.Factory.StartNew(() =>
        {
            //the long running workload
            var items = _itemService.GetByFilter(searchText);
            //update the ui
            Task.Factory.StartNew(() =>
            {
                foreach (var item in items)
                    Items.Add(item);
                if (items.Any())
                {
                    SelectedItem = items.First();
                    NotifyOfPropertyChange(() => SelectedItem);
                }
            }, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
        });
    }
}
}

编辑

我尝试了Shadrix的方法,但不幸的是它也没有起作用。我还尝试了彼得·邓高斯(Peter Dunihos)明显重复的答案,但我也没有成功(我得到了上面描述的相同的Xamlparseexception)。

我尝试的最后一件事是CM在Onuithread方法中的构建,它基本上包装了dispatcher.currentdispatcher。显然(考虑到我的最后两次失败尝试),它也失败了。当前的实现看起来像以下内容(我删除了其他非相关属性和方法):

public class NavigatorViewModel : Screen
{
public NavigatorViewModel()
{
    Items = new ObservableCollection<ItemViewModel>();
}
public ObservableCollection<ItemViewModel> Items { get; set; }
public ItemViewModel SelectedItem { get; set; }
private string _searchText;
public string SearchText
{
    get => _searchText;
    set
    {
        _searchText = value;
        NotifyOfPropertyChange(() => SearchText);
        UpdateItemList(_searchText);
    }
}
private void UpdateItemList(string searchText)
{
    Items.Clear();
    var updater = new ItemUpdater();
    updater.ItemsUpdated += (s, e) => {
        OnUIThread(() =>
        {
            var items = ((ItemsUpdatedEventArgs) e).Items;
            foreach (var item in items)
            {
                Items.Add(item);
            }
            NotifyOfPropertyChange(() => Items);
        });
    };
    var updateThread = new Thread(updater.GetItems);
    updateThread.Start(searchText);
}
}
public class ItemUpdater
{
public event EventHandler ItemsUpdated;
private readonly ItemService _itemService;
public ItemUpdater()
{
    _itemService = new ItemService();
}
public void GetItems(object searchText)
{
    var items = _itemService.GetByFilter((string)searchText);
    ItemsUpdated?.Invoke(this, new ItemsUpdatedEventArgs(items));
}
}
public class ItemsUpdatedEventArgs : EventArgs
{
public ItemsUpdatedEventArgs(IList<ItemViewModel> items)
{
    Items = items;
}
public IList<ItemViewModel> Items { get; }
}

这让我发疯了,我无法解决这个问题,因此,如果有人想帮助一个年轻的大三学生,我会高度认可。:)

您可以在此处找到完整的源代码。

谢谢大家!

使用当前UI线程的Dispatcher

//update the ui
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
    foreach (var item in items)
    {
        Items.Add(item);
    }
    if (items.Any())
    {
        SelectedItem = items.First();
        NotifyOfPropertyChange(() => SelectedItem);
    }
}));

相关内容

  • 没有找到相关文章

最新更新