如何从视图模型中的构造函数调用异步任务? [英] How to call an Async Task from the Contructor in a View Model?

查看:127
本文介绍了如何从视图模型中的构造函数调用异步任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在加载我的应用程序时填充查找编辑列表,但我需要将其称为异步。

I am trying to populate a look up edit list on load of my application, but I need to call it async.

如何完成此操作? 我在网上看到了一些答案,但没有一个有效。

How can this be completed?  I've seen some answers online, but none of them have worked.

谢谢。

推荐答案

你好ttaylor29,

Hi ttaylor29,

这是一个简单的样本供你参考。

Here is a simple sample for your reference.

#XAML

<Window x:Class="WpfApp1.AsyncLoadedDemo"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="AsyncLoadedDemo" Height="300" Width="300">
    <Grid>
        <ListBox HorizontalAlignment="Left" 
                 Height="240" Margin="10,10,10,10" 
                 VerticalAlignment="Top" Width="260"
                 x:Name="listBox1"
                 ItemsSource="{Binding Lists}"/>

    </Grid>
</Window>

#Code Behind

#Code Behind

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for AsyncLoadedDemo.xaml
    /// </summary>
    public partial class AsyncLoadedDemo : Window
    {
        public ObservableCollection<string> Payments { get; set; }
        public AsyncLoadedDemo()
        {
            InitializeComponent();
            this.DataContext = new AsyncLoadedDemoViewModel();
        }
    }

    public class AsyncLoadedDemoViewModel : DependencyObject
    {
        public List<string> Lists
        {
            get { return (List<string>)GetValue(ListsProperty); }
            set { SetValue(ListsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Lists.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ListsProperty =
            DependencyProperty.Register("Lists", typeof(List<string>), typeof(AsyncLoadedDemoViewModel), new PropertyMetadata(null));

        public AsyncLoadedDemoViewModel()
        {
            TaskMethod();
        }

        public async void TaskMethod()
        {
            await Taskt();
        }

        public Task Taskt()
        {
            return Task.Run(() =>
            {
                List<string> l;
                using (var db = new EFDemoContext())
                {
                    l = db.payments.Select(t => t.PaymentMode).ToList();
                }

                this.Dispatcher.Invoke(
                    () =>
                    {
                        Lists = l;
                    }
                    );
            });
        }
    }

}

祝你好运,

张龙


这篇关于如何从视图模型中的构造函数调用异步任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆