简体   繁体   English

带有WP8(MVVM)和异步Web服务的进度指示器

[英]Progress indicator with WP8 (MVVM) and asynchronous web service

I'm a newbie in async programming. 我是异步编程的新手。 I'm creating WP8 application. 我正在创建WP8应用程序。

What I would like to have 我想拥有什么

I would like to show list of eg news. 我想显示例如新闻清单。 When button is tapped: 轻按按钮时:

  1. there should appear proggress indicator, 应该出现进度指示器,
  2. data should be downloaded and displayed, 数据应下载并显示,
  3. progress indicator should dissapear. 进度指示器应消失。

What I have so far: 到目前为止,我有:

Method in referenced webservice: 引用的Web服务中的方法:

void GetNewsAsync() 
{
    //...
}

Two methods in viewModel: viewModel中的两种方法:

public void GetNewsFromService()
{
    //call webservice
    //...
    client.GetNewsCompleted += client_GetNewsCompleted;
    client.GetNewsAsync(startDate);
}

void client_GetNewsCompleted(object sender, GetNewsCompletedEventArgs e)
{
//save result from webservice to db
//update viewmodel property
//...
}

OnClick method in view for button "sync": 查看按钮“同步”中的OnClick方法:

void appBarSyncButton_Click(object sender, EventArgs e)
{
    TurnOnProgressBar();
    _viewModel.GetNewsFromService();
    //TurnOffProgressBar();
}

Everything is working (web service is called, result data is saved, list is updating, but... 一切正常(调用Web服务,保存结果数据,更新列表,但是...

How to redesign this code to inform view in order to hide progress indicator (when all work is completed)? 如何重新设计此代码以通知视图以隐藏进度指示器(当所有工作完成时)?

client_GetNewsCompleted should assign a public property like Completed to true. client_GetNewsCompleted应该将诸如Completed类的公共属性分配为true。 Following that, it would be more MVVM, if you had a property like IsInProgress that you manage completely in your ViewModel - set to true in GetNewsFromService and false in client_GetNewsCompleted . 在此之后,它会更MVVM,如果你有你在你的视图模型完全管理像IsInProgress属性-设置为真GetNewsFromService在和假client_GetNewsCompleted Then simply bind ProgressIndicator.Visibility to that property (using converter). 然后只需将ProgressIndicator.Visibility绑定到该属性(使用转换器)。

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

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