在Silverlight应用程序无效的跨线程访问

在Silverlight应用程序无效的跨线程访问

本文介绍了在Silverlight应用程序无效的跨线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的吊床框架,使得从Silverlight应用程序以REST服务asyncronous服务电话。在完成'回调我更新绑定到视图中的一个组合框一个ObservableCollection。

I am using the Hammock framework to make asyncronous service calls from a Silverlight application to Rest services. In the 'completed' callback I am updating an ObservableCollection that is bound to a combobox on the view.

这是无效的跨线程访问异常被抛出在OnPropertyChanged事件处理程序。

An 'Invalid cross-thread access' exception is being thrown in the 'OnPropertyChanged' event handler.

难道这becasue吊床不执行的UI线程上的回调?如果不是,为什么不呢?这似乎是功能,该框架应处理。我失去了一些东西?我当然不希望处理用户界面的调用线程自己在每一个完成处理程序。

Is this becasue Hammock is not executing the callback on the UI thread? If not, why not? That would seem to be functionality that the framework should handle. Am I missing something? I sure do not want to handle the invoking of the UI thread myself in each completed handler.

public void LoadMyData()
{
    var request = new RestRequest();
    request.Path = "MyRestUrlText";

    var callback = new RestCallback(
      (restRequest, restResponse, userState) =>
      {
        var visibleData = new ObservableCollection<MyDataType>();

        var myData = JsonConvert.DeserializeObject<MyDataType[]> restResponse.Content);

        foreach (var item in myData)
            visibleData .Add(item);

        this.MyBoundCollection = visibleData;
        OnPropertyChanged("MyBoundCollection");
    });

    var asyncResult = _restClient.BeginRequest(request, callback);
}

感谢

推荐答案

有关绑定属性和属性,是集合(而不是儿童观察的集合),它只是一个需要在UI线程上的OnPropertyChanged。该特性能够更早地改变,但直到OnPropertyChanged就是所谓的UI将不会更改绑定。

For bound properties and properties that are collections (and not children in observable collections) it is only the OnPropertyChanged that needs to be on the UI thread. The properties can change earlier, but the UI will not change bindings until OnPropertyChanged is called.

我们所有的ViewModels从ViewModelBase得到我们创建了一个实现帮手SendPropertyChanged像下面(所以我们从来没有担心跨线程)。

All our ViewModels derive from a ViewModelBase we created that implements a helper SendPropertyChanged like below (so we never have to worry about cross-threading).

我们的所有通知性能调用。

All our notify properties call that instead of calling OnPropertyChanged directly.

这也暴露了一个普遍有用OnUiThread方法,这样你就可以在UI线程上执行任意code:

It also exposes a generally useful OnUiThread method so you can execute arbitrary code on the UI thread:

protected delegate void OnUiThreadDelegate();

public event PropertyChangedEventHandler PropertyChanged;

public void SendPropertyChanged(string propertyName)
{
    if (this.PropertyChanged != null)
    {
        this.OnUiThread(() => this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)));
    }
}

protected void OnUiThread(OnUiThreadDelegate onUiThreadDelegate)
{
    if (Deployment.Current.Dispatcher.CheckAccess())
    {
        onUiThreadDelegate();
    }
    else
    {
        Deployment.Current.Dispatcher.BeginInvoke(onUiThreadDelegate);
    }
}

如果您不使用MVVM,一)道歉和b)可耻的是你:)

这篇关于在Silverlight应用程序无效的跨线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:10