简体   繁体   English

使用MVVM Light在WP Universal App上异步加载数据

[英]Loading data asynchronously on WP Universal App using MVVM Light

I'm developing a Windows Phone Universal App using MVVM Light (I'm new to MVVM pattern). 我正在使用MVVM Light开发Windows Phone通用应用程序(我是MVVM模式的新手)。 My problem is to make it work like that: 我的问题是使它像这样工作:

  1. Application is launched (or the user navigates to different view), 启动应用程序(或用户导航到其他视图),
  2. then we see the basic UI, 然后我们看到基本的用户界面,
  3. and then we wait for the data to be loaded, while the application stays responsive. 然后我们等待数据加载,而应用程序保持响应状态。

So far I have tested many different approaches like: 到目前为止,我已经测试了许多不同的方法,例如:

and in each case my application was showing splash screen (or was blocking before navigating to another page) unless the data was loaded. 并且在每种情况下, 除非加载了数据,否则我的应用程序都显示初始屏幕(或在导航到另一页之前被阻止)。 The methods return data properly, views display it well. 该方法正确返回数据,视图显示良好。

So my question is, what is the proper way to load data asynchronously in Universal App? 所以我的问题是, 在Universal App中异步加载数据的正确方法什么? I will be grateful for your help. 感谢您的帮助。

This is how my code looks like for now: 这是我的代码现在的样子:

MainViewModel.cs MainViewModel.cs

    public MainViewModel()
    {
        this._navigationService = ServiceLocator.Current.GetInstance<INavigationService>();

        _newsService = ServiceLocator.Current.GetInstance<INewsService>();

        LoadMainPageCommand =
            new RelayCommand(async() => await LoadMainPageData());
    }

    public RelayCommand LoadMainPageCommand { get; set; }

    private async Task LoadMainPageData()
    {
        // to make it work a bit longer
        for (int i = Int32.MaxValue; i > 20000; i--) ;

        NewsCategories= await _newsService.GetNewsCategoriesAsync();
        RaisePropertyChanged("NewsCategories");
    }

HubPage.xaml HubPage.xaml

<Page
x:Class="xxx.HubPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:xxx"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:data="using:xxx.Data"
mc:Ignorable="d">
<i:Interaction.Behaviors>
    <core:EventTriggerBehavior EventName="Loaded">
        <core:InvokeCommandAction Command="{Binding LoadMainPageCommand}"/>
    </core:EventTriggerBehavior>
</i:Interaction.Behaviors>
...

INewsService.cs INewsService.cs

public interface INewsService
{
    Task<ObservableCollection<NewsCategory>> GetNewsCategoriesAsync();
}

Your problem is in your "test" code: 您的问题出在您的“测试”代码中:

private async Task LoadMainPageData()
{
  // to make it work a bit longer
  for (int i = Int32.MaxValue; i > 20000; i--) ;

  NewsCategories= await _newsService.GetNewsCategoriesAsync();
  RaisePropertyChanged("NewsCategories");
}

This is a method with an asynchronous signature that is doing synchronous work. 这是一种具有异步签名的方法,正在执行同步工作。 If you want to stick in a delay, then do it asynchronously: 如果要延迟,请异步执行:

private async Task LoadMainPageData()
{
  // to make it work a bit longer
  await Task.Delay(TimeSpan.FromSeconds(3));

  NewsCategories= await _newsService.GetNewsCategoriesAsync();
  RaisePropertyChanged("NewsCategories");
}

Then any of the approaches should work. 那么任何一种方法都应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM