相关疑难解决方法(0)

构造函数中的WinRT异步数据加载

我想在ViewModel的构造函数中加载一些数据,但由于WinRT的异步性质,我被迫使用异步方法.不幸的是我不能有异步构造函数所以我试图将异步方法用作同步方法.我确信在应用程序加载时有一种更好的加载数据(异步)的方法,但我的想法暂时还是空白.

我正在寻找一种方法来修复我的应用程序使用我想要的思路,或者使用更合适的方法永久修复它.

代码非常简单(甚至缺少ViewModel)只是为了演示我面临的问题.

public sealed partial class MainPage : Page
{

    public string Data { get; set; }

    public DataService _dataService { get; set; }

    public MainPage()
    {
        this.InitializeComponent();

        _dataService = new DataService();
        var t = _dataService.GetData();

        Data = t.Result;
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param> …
Run Code Online (Sandbox Code Playgroud)

c# async-await windows-runtime

8
推荐指数
1
解决办法
3506
查看次数

标签 统计

async-await ×1

c# ×1

windows-runtime ×1