wpf - 如何将 View 中控件的 UI 调度程序传递给 ViewModel

标签 wpf silverlight mvvm mvvm-light

有一个类似的问题(How to pass the UI Dispatcher to the ViewModel 有人给我提供一些示例或完整代码。我是 MVVM 新手,所以我不明白。

private async void BindMatchGridinAsync()
        {
            await BindMatchGridAsync(); 
        }        
        async Task<ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC>> BindMatchGridAsync()
        {
            await Task.Run(() => 
                {
                    BindMatchGrid();
                });
            return null;
        }
        void BindMatchGrid()
        {
            BindMatchGridDel bindDel = new BindMatchGridDel(BindMatchGridData);
            matchList = new List<GetMatchDetailsDC>();            
            matchList = proxy.GetMatch().ToList();            
            dataGridParent.Dispatcher.BeginInvoke(bindDel, null);
        }
        public delegate void BindMatchGridDel();
        void BindMatchGridData()
        {
            dataGridParent.ItemsSource = matchList;
        }

现在,在这段代码 BindMatchGridinAsync() 中,我已将代码的构造函数放入文件(.cs 文件)后面。它提供了适当的异步操作。但我需要在 ViewModel 中实现相同的功能。请建议我如何实现它。 预先感谢您。

最佳答案

您像 WinForms 开发人员一样思考这个问题,其中每个控件都可以位于单独的线程上。

在 WPF 中,有一个 UI 线程。该线程称为调度程序。因此每个控件都将使用相同的调度程序。

如上所述:

Application.Current.Dispatcher.BeginInvoke(new Action(DoSomething),null);

完全可以接受,并且可以与您的所有控件一起使用,因为所有控件都使用应用程序调度程序。

关于wpf - 如何将 View 中控件的 UI 调度程序传递给 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18532497/

相关文章:

c# - User.config 损坏

silverlight - 使用 StatLight 和 TeamCity 自动化 silverlight 单元测试

sql - 如何在 Entity Framework 中使用数据透视表?

wpf - 具有MVVM模式的模态窗口

wpf - 一个模型实体,多个页面 -> 多个 View ?多个 View 模型?

wpf - 如何使用.NET Core 3和Visual Studio创建WPF应用

c# - 当用户单击带有 MVVM 的关闭按钮时,如何在 WPF 对话框中取消等待?

c# - 使用 C# 在 WPF 中的网格上设置背景图像

c# - WebClient 的 Windows Phone BackgroundWorker?

c# - 在 MVVM Light 的 ViewModel 之外注册消息?