Silverlight MVVM App의 ViewModel 서비스 라이브러리 (WCF 서비스 아님)에서 가져온 속성.Silverlight 4.0 비동기식 (MVVM) 비동기 호출 메서드
라이브러리 메서드는 완료하는 데 약간의 시간이 걸릴 수있는 일부 작업을 수행합니다. 작업이 완료되면 반환 값이 ViewModel 속성에 할당됩니다.
View는 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 });
}
}
}
뷰 모델 :
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
메서드를 비동기 적으로 수행하는 방법은 무엇입니까?
감사합니다. 나는 2 개의 기사를 따르는 것의 도움으로 그것을 할 수 있었다. [SmartDispatcher] (http://www.jeff.wilcox.name/2010/04/propertychangedbase-crossthread/) [스레딩 샘플] (http://www.andrewshough.com/development/silverlight/asynchronous-tasks - in-silverlight /) – Zuhaib