Silverlight 4.0 异步调用长时间运行方法 (MVVM)

发布于 2024-10-25 22:01:05 字数 2071 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-11-01 22:01:05

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文