Silverlight 4.0 异步调用长时间运行方法 (MVVM)
我的 Silverlight MVVM 应用程序的 ViewModel 属性是从服务库(不是 WCF 服务)填充的。
库方法执行一些可能需要一些时间才能完成的操作。操作完成后,返回值将分配给 ViewModel 属性。
由于视图绑定到 ViewModel 属性,因此当属性值更改时,它会自动刷新。但是,在预期的操作过程中,UI 由于同步操作而变得无响应。
如何异步执行以下操作?
服务合同和实现:
public class ItemsLoadedEventArgs : EventArgs
{
public List<string> Items { get; set; }
}
public interface ISomeService
{
event EventHandler<ItemsLoadedEventArgs> GetItemsCompleted;
void GetItemsAsync();
}
public class SomeService : ISomeService
{
public event EventHandler<ItemsLoadedEventArgs> GetItemsCompleted;
public void GetItemsAsync()
{
// do something long here
// how to do this long running operation Asynchronously?
// and then notify the subscriber of the Event?
// when the operation is completed fire the event
if(this.GetItemsCompleted != null)
{
this.GetItemsCompleted(this, new ItemsLoadedEventArgs { Items = resulthere });
}
}
}
ViewModel:
public class HomeViewModel : ViewModel
{
private ISomeService service;
private ObservableCollection<string> _items;
// Items property is bound to UI
public ObservableCollection<string> Items
{
get { return this._items; }
set
{
this._items = value;
this.RaisePropertyChanged(() => this.Items);
}
}
// DI
public HomeViewModel(ISomeService service)
{
...
this.service = service;
// load items
this.LoadItems();
}
private void LoadItems()
{
this.service.GetItemsCompleted += (s, ea) =>
{
this.Items = new ObservableCollection<string>(ea.Items);
};
this.service.GetItemsAsync();
}
}
问题:
由于数据是在构造函数中加载的,并且操作是同步的,因此导致 UI 无响应。
如何异步执行SomeService
类的GetItemsAsync
方法内部的操作?
My Silverlight MVVM App's ViewModel Properties that are poupulated from a service library (not WCF Service).
The library method does some operation that may take some time to complete. After the operation is completed the return value is assigned to the ViewModel property.
Since the View is bound to the ViewModel property it would refresh automatically when the Property Value changes. But, during the operation as expected the UI becomes non responsive because its synchronous operation.
How to do the following operation Asynchronously?
Service Contract & Implementation:
public class ItemsLoadedEventArgs : EventArgs
{
public List<string> Items { get; set; }
}
public interface ISomeService
{
event EventHandler<ItemsLoadedEventArgs> GetItemsCompleted;
void GetItemsAsync();
}
public class SomeService : ISomeService
{
public event EventHandler<ItemsLoadedEventArgs> GetItemsCompleted;
public void GetItemsAsync()
{
// do something long here
// how to do this long running operation Asynchronously?
// and then notify the subscriber of the Event?
// when the operation is completed fire the event
if(this.GetItemsCompleted != null)
{
this.GetItemsCompleted(this, new ItemsLoadedEventArgs { Items = resulthere });
}
}
}
ViewModel:
public class HomeViewModel : ViewModel
{
private ISomeService service;
private ObservableCollection<string> _items;
// Items property is bound to UI
public ObservableCollection<string> Items
{
get { return this._items; }
set
{
this._items = value;
this.RaisePropertyChanged(() => this.Items);
}
}
// DI
public HomeViewModel(ISomeService service)
{
...
this.service = service;
// load items
this.LoadItems();
}
private void LoadItems()
{
this.service.GetItemsCompleted += (s, ea) =>
{
this.Items = new ObservableCollection<string>(ea.Items);
};
this.service.GetItemsAsync();
}
}
Problem:
Since the data is loaded in the constructor, and the operation is synchronous it makes the UI unresponsive.
How to perform the operation inside the GetItemsAsync
method of the SomeService
class Asynchronously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Dispatcher.BeginInvoke 调用您的库方法。完成后,您必须返回 UI 线程来更新 ViewModel。有关这方面的信息,请参阅此问题。
Use Dispatcher.BeginInvoke to call your library method. You have to go back to the UI thread to update your ViewModel when it is done. See this question for info on this.