在MVVM中使用Asynchronus WCF调用WPF数据绑定 [英] WPF Databinding with Asynchronus WCF Call in MVVM

查看:146
本文介绍了在MVVM中使用Asynchronus WCF调用WPF数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在用mvvm模式写一个wpf applivation。在我的viewmodel中,我通过wcf服务异步接收数据:

Hi,

i am writing a wpf applivation with the mvvm pattern. in my viewmodel, i receive the data asynchron over a wcf service:

#region Construction
public  AddressViewModel()
{
    this.LoadAddress();
}

private async void LoadAddress()
{
    ServiceClient client = new ServiceClient();
    _address = await client.GetAddressByIdAsync(1);
}
#endregion



我在视图中绑定属性像这样:


I bind the propertys in view like this:

<TextBlock Grid.Column="1" Grid.Row="0"  Text="{Binding address1, IsAsync=True}"/>



在异步加载后,UI不会自动更新。当我通过按钮(例如)更改属性时,UI会更新,因此问题必须依赖于异步WCF调用。我尝试了以下更改并且他们完成了工作,但我认为这不是一个好的做法。


The UI doesnt update automatically after the asynchron load. When i change a property via button (for example) the UI updates, so the problem must rely on the asynchron WCF-Call. I tried the following changes and they do the job but i think this is not good practice.

private async void LoadAddress()
{
   ServiceClient client = new ServiceClient();
   _address = await client.GetAddressByIdAsync(1);
   RaisePropertyChanged("address1"); //Do this for each property?
}



对于我班级的每个属性,我真的需要RaisePropertyChange(propName)还是有更好,更清洁,更优雅的方式?



编辑:

Normaly我在这样的setter中提升了propertychangeevent:


Do i really have to RaisePropertyChange("propName") for each property of my class or is there a better, cleaner, more elegant way?


Normaly i raise the propertychangeevent in the setter like this:

public string address1
{
   get { return Address.address1; }
   set
   {
       if (Address.address1 != value)
       {
           Address.address1 = value;
           RaisePropertyChanged("address1");
       }
   }
}

推荐答案

不幸的是。如果您不使用通知物业编织者 [ ^ ]您需要在每次更改房产时提出通知。



如果你没有走自动路线,我真的建议设置你的属性值,然后在属性设置器而不是方法/ void上提高已更改的事件打电话给你。这有助于在一个地方保持代码的可维护性。只有我的意见,但在方法调用中提升属性事件可能会变得混乱,除非真的需要。
Unfortunately yes. If you do not use something like Notify Property Weaver[^] you will need to raise the notify on each property change.

If you do not go down the automated route, I would really recommend setting your property value and then raising the changed event on the property setter rather than the method / void call your are doing. This helps keep code maintainable and in one place. Only my opinion but having raise property events in method calls can get messy unless really needed.


编辑:

有时您会想到你自己... DOOOH :)

如果有人在寻找这个:

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx [ ^ ]



如果您更新了洞对象,只需执行

Sometimes you think to yourself... DOOOH :)
If someone is looking for this too:
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx[^]

If you update your hole object, just do
RaisePropertyChanged(null);

并且你将更新所有属性。



请求db7uk获得支持。

and you''ll get all propertys updated.

Thx to db7uk for support.


这篇关于在MVVM中使用Asynchronus WCF调用WPF数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆