WPF MVVM异步加载通知

本文关键字:通知 加载 异步 MVVM WPF | 更新日期: 2023-09-27 18:04:19

我在窗口中有两个视图,其中一个异步加载数据。我如何通知第二个视图,数据是加载的,它需要更新标签中的数据?用回调创建singleton ?

主窗口。xaml

    <Grid>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <views:FirstView Grid.Column="0"></views:FirstView>
        <views:SecondView Grid.Column="1"></views:SecondView>
    </Grid>
</Grid>

FirstView.xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Command="{Binding LoadData}"/>
    <ListView ItemsSource="{Binding Items}" Grid.Column="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding Name}" Grid.Column="0"></Label>
                    <Label Content="{Binding Hours}" Grid.Column="1"></Label>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

FirstViewModel:

 public class FirstViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the FirstViewModel class.
    /// </summary>
    public ObservableCollection<ItemStruct> Items { get; set; }
    public ICommand LoadData { get; set; }
    public FirstViewModel()
    {
        LoadData = new RelayCommand(() => LongLoadData());
        Items = new ObservableCollection<ItemStruct>();
        Items.Add(new ItemStruct { Name="First",Hours="Loading"});
        Items.Add(new ItemStruct { Name = "Second",Hours="Loading" });
    }
    public void LongLoadData()
    {
        Action Load = new Action(AsyncLoad);
        IAsyncResult result = Load.BeginInvoke(null, null);
    }
    private void AsyncLoad()
    {
        foreach (ItemStruct item in Items)
        {
            Random rnd = new Random();
            System.Threading.Thread.Sleep(3000);
            item.Hours = rnd.Next(1, 100).ToString();
        }
    }
}

SecondView.xaml:

 <Grid>
    <ListView ItemsSource="{Binding Items}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding Name}" Grid.Column="0"></Label>
                    <Label Content="{Binding Hours}" Grid.Column="1"></Label>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

SecondViewModel:

    public class SecondViewModel : ViewModelBase
{
    public ObservableCollection<ItemStruct> Items { get; set; }
    public SecondViewModel()
    {
        Items = new ObservableCollection<ItemStruct>();
        Items.Add(new ItemStruct { Name="First",Hours="Loading"});
        Items.Add(new ItemStruct { Name = "Second",Hours="Loading" });
    }
}

WPF MVVM异步加载通知

可以使用MvvmLight Messenger进行虚拟机之间的通信。给ViewModels注射

public FirstVieModel(IMessenger messenger)
{
   this.messenger = messenger;
}

当数据加载时调用信使(发送)正确的消息。

this.messenger.Send<SomethingLoadedMessage>(new SomethingLoadedMessage(..));

receiver ViewModel处理也很简单:

this.messenger.Register<SomethingLoadedMessage>(this, OnSomethingLoaded);

在构建松散耦合的多组件WPF应用程序时,通常使用messenger是一种很好的做法。