我正在寻找在层之间进行异步通信的最佳实践。
我正在使用mvvm light toolkit

当前,我在模型中使用了backgroundworker,因为我在自动生成的代码中看到了这一点。不是与backgroundworker一起使用,而是与async调用一起使用。

public void GetConfig(Action<Config, Exception> callback)
{
    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += (backgroundWorkerSender, backgroundWorkerArgs) =>
    {
        try
        {
            backgroundWorkerArgs.Result = AppEnvironment.Instance.Config;
        }
        catch (Exception exception)
        {
            backgroundWorkerArgs.Result = null;
        }
    };

    backgroundWorker.RunWorkerCompleted += (backgroundWorkerSender, backgroundWorkerArgs) =>
    {
        if (backgroundWorkerArgs.Result != null)
        {
            callback((Config) backgroundWorkerArgs.Result, null);
        }
        else
        {
            /* ToDo: exceptionhandling */
        }
    };

    backgroundWorker.RunWorkerAsync();
}

现在,我发现AsyncDelegateCommand实现了ViewModel中的异步部分。
private ICommand _refreshObjectDefinitionCommand;
public ICommand RefreshObjectDefinitionCommand
{
    get
    {
        return _refreshObjectDefinitionCommand
          ?? (_refreshObjectDefinitionCommand = new AsyncDelegateCommand(delegate
              {
                  IsBusy = true;
                  _dataService.GetObjectDefinition(
                    (xmlObjectDef, errorConfig) =>
                    {
                        if (errorConfig != null)
                        {
                            /* ToDo Lenz: exceptionhandling */
                            return;
                        }

                        ObjectDefinition = xmlObjectDef;
                    });

                  _dataService.GetObjectDefinitionTreeView(
                      (treenodes, errorConfig) =>
                      {
                          if (errorConfig != null)
                          {
                              /* ToDo Lenz: exceptionhandling */
                              return;
                          }

                          TreeNodes = treenodes;
                      });
              },
                                () => _isConnected, o => IsBusy = false, exception => IsBusy = false));
    }
}

我对最佳做法有些困惑?我读了很多文章。但是无论如何,他们总是持不同意见。是否有任何规定可以在正常维护下获得最佳兼容性?

一些值得深思的东西

型号:

http://csharperimage.jeremylikness.com/2009/12/simplifying-asynchronous-calls-in.html

http://www.dzone.com/articles/mvvmlight-and-async

ViewModel

http://www.codeproject.com/Articles/123183/Asynchronus-MVVM-Stop-the-Dreaded-Dead-GUI-Problem

http://www.codeproject.com/Articles/441752/Async-MVVM-Modern-UI

最佳答案

好吧,我想说模型的感染并将其转换为View Model是异步的。谁来做,取决于体系结构,它可以在 View 模型本身上完成,也可以使用 Controller 层进行此类异步加载以及将初始化的VM映射到 View 。同样,背景工作人员过去也应该使用Task类进行并行操作。当然,当通知有关VM更改的 View 时,请不要忘记通过调度程序进行调用。

代码示例:

    Task<string>.Factory.StartNew(() =>
{
     string text = GetArticleText();
     Application.Current.Dispatcher.BeginInvoke(new Action(()=>MyTextProperty = text));
});

10-08 02:39