c# - 如何在 View 模型中调用异步方法

标签 c# wpf asynchronous mvvm

我正在尝试学习 WPF 应用程序中的 MVVM 模式。我在我的 View 模型中编写了这个异步方法(它必须是异步的,因为我使用的是 HttpClient 并且它的方法是异步的):

public async Task<Dictionary<int, BusStop>> GetBusStops()
    {
        var busStopDict = new Dictionary<int, BusStop>();
        var url = "my url";

        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.GetAsync(url))
        using (HttpContent content = response.Content)
        {
            string data = await content.ReadAsStringAsync();
            var regularExpression = Regex.Match(data, "\\[(.)*\\]");
            var result = regularExpression.Groups[0];

            var json = JValue.Parse(result.ToString());
            var jsonArray = json.ToArray();

            foreach (var a in jsonArray)
            {
                // irrelevant logic

                busStopDict.Add(nr, bs);
            }
        }

        return busStopDict;
    }

此方法返回一个充满公交车站的字典(我的模型)。我想将这个字典与 View 中的组合框绑定(bind),但我无法让它工作,因为我无法在我的 View 模型的构造函数中调用这个异步方法,而且我不知道我可以在哪里调用它。你有什么建议吗?

最佳答案

我不建议在 View 模型构造函数中编写逻辑。相反,我会在您的 View 中创建一个 Loaded 事件触发器,以确保您不会干扰 View 的加载过程。

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"


<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding LoadedCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

然后在您的 View 模型中,我建议执行以下操作:

为您的 Loaded 事件添加以下属性

public DelegateCommand LoadedCommand { get; }

然后在你的构造函数中赋值

LoadedCommand = new DelegateCommand(async () => await ExecuteLoadedCommandAsync());

添加加载的方法并在其中调用你的方法

private async Task ExecuteLoadedCommandAsync()
{
    var busStops = await GetBusStops();
    //TODO: display the busStops or do something else
}

此外,将“Async”作为后缀添加到您的异步方法中是一种很好的命名模式。它使您能够快速查看哪些方法是异步的。 (因此将“GetBusStops”重命名为“GetBusStopsAsync”)

这是一个简单的DelegateCommand实现

public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public event EventHandler CanExecuteChanged;

    public DelegateCommand(Action<object> execute) 
               : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute, 
               Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public override bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }

    public override void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if( CanExecuteChanged != null )
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}

使用此实现时,您需要将 View 模型构造函数中 DelegateCommand 的初始化更改为以下内容

LoadedCommand = new DelegateCommand(async (param) => await ExecuteLoadedCommandAsync());

关于c# - 如何在 View 模型中调用异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39456542/

相关文章:

c# - 如何在设计时不知道要选择的属性的名称的情况下编写 linq select 语句?

c# - 即使没有对象为 null,也会出现 NullReferenceException

C#:在给定时间以编程方式启动计算机

wpf - 将 "not visible"WPF 控件渲染为位图图像

Javascript循环遍历数组异步

c# - asp.net core RC2 图片不显示

WPF 与 INotifyPropertyChanged 的​​绑定(bind)不会更新

c# - 在 InkCanvas 中绘制的线条的发光效果

php - jQuery ajax函数调用php脚本返回一个值?

c# - SQLite 在 CreateTablesAsync 上挂起 - Xamarin Forms