Silverlight 3 RIA 服务和属性正在“扁平化”?
我有一个大致如下所示的模型:
private bool IsProduct {get; set;}
private decimal ProductPrice {get; set;}
private decimal TimedRate {get; set;}
public decimal SingularAmount {
get {
if (this.IsProduct) {
return ProductPrice;
}
else {
return TimedRate;
}
}
set {
if (this.IsProduct) {
this.ProductPrice = value;
}
else {
this.TimedRate = value;
}
}
}
我通过 RIA 服务绑定到此 SingularAmount 属性到 Silverlight 3 DataGrid。 我发现,当我更改属性时,模型上的相应属性不会更新。 当我单步执行代码时,我可以在客户端看到 SingularAmount 设置为 5,例如,其他属性不会更新。
似乎当 RIA 制作类的客户端版本时,这种功能没有被移植。 关于如何解决这个问题有什么想法吗?
更新
这是 RIA 为该属性生成的代码:
[DataMember()]
public decimal SingularAmount
{
get
{
return this._singularAmount;
}
set
{
if ((this._singularAmount != value))
{
this.ValidateProperty("SingularAmount", value);
this.OnSingularAmountChanging(value);
this.RaiseDataMemberChanging("SingularAmount");
this._singularAmount = value;
this.RaiseDataMemberChanged("SingularAmount");
this.OnSingularAmountChanged();
}
}
}
显然,这看起来不太像原始的服务器端属性。
I have a model that looks roughly like this:
private bool IsProduct {get; set;}
private decimal ProductPrice {get; set;}
private decimal TimedRate {get; set;}
public decimal SingularAmount {
get {
if (this.IsProduct) {
return ProductPrice;
}
else {
return TimedRate;
}
}
set {
if (this.IsProduct) {
this.ProductPrice = value;
}
else {
this.TimedRate = value;
}
}
}
I'm binding to this SingularAmount property via RIA Services to Silverlight 3 DataGrid. What I'm finding is that, when I change the property - the respective properties on the model do not get updated. When I step through the code, I can see on the client side, that SingularAmount is set to say 5 for example, the other properties do not get updated.
Seems like when RIA makes the client side version of the classes, this sort of functionality isn't ported over. Any ideas on how to tackle this?
Update
Here is the RIA generated code for that property:
[DataMember()]
public decimal SingularAmount
{
get
{
return this._singularAmount;
}
set
{
if ((this._singularAmount != value))
{
this.ValidateProperty("SingularAmount", value);
this.OnSingularAmountChanging(value);
this.RaiseDataMemberChanging("SingularAmount");
this._singularAmount = value;
this.RaiseDataMemberChanged("SingularAmount");
this.OnSingularAmountChanged();
}
}
}
Obviously, that doesn't look much like the original server side property.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1) 如果实体模型与 DomainServices 位于同一项目内:
In order to get this to work you need to create a new file with the extension
.shared.cs (or vb).
This extension tells RIA Services to copy the file to the client side.
Within the file you can extend the entity (using partial) and add the
new property.
Don't forget to call "this.RaisePropertyChanged("SingularAmount")" so
that any control bound to this property will get notification of the change.
Because you are using partial classes, and the way RIA shared code works, the
new file has to be within the same project/assembly as the original entity.
...
2) If the entity model is in a different project:
Create a partial class in the client side project with the new property.
Same concept as above but new property will only be visible in the client side.
...
You can find more information on sharing code on the overview document here. Page 34.
第 3 章是关于客户端代码生成的。 值得了解的好信息。
Also, chapter 12 (page 97-103) on how to share code accross tiers, chapter 17
(pages 122-125) Code generation hook points, and chapter 18 (pages 126-128) How to add
how to add computed properties.
如果您认真使用 .NET RIA 服务,您可能应该知道这一点
熟记文档。 :-)
1) If the entity model is within the same project as the DomainServices:
In order to get this to work you need to create a new file with the extension
.shared.cs (or vb).
This extension tells RIA Services to copy the file to the client side.
Within the file you can extend the entity (using partial) and add the
new property.
Don't forget to call "this.RaisePropertyChanged("SingularAmount")" so
that any control bound to this property will get notification of the change.
Because you are using partial classes, and the way RIA shared code works, the
new file has to be within the same project/assembly as the original entity.
...
2) If the entity model is in a different project:
Create a partial class in the client side project with the new property.
Same concept as above but new property will only be visible in the client side.
...
You can find more information on sharing code on the overview document here. Page 34.
Chapter 3 is about client side code generation. Good info to know.
Also, chapter 12 (page 97-103) on how to share code accross tiers, chapter 17
(pages 122-125) Code generation hook points, and chapter 18 (pages 126-128) How to add
how to add computed properties.
If you are serious on using .NET RIA Services, you probably should know this
document by heart. :-)
当您在 Silverlight 中对标准 CLR 属性(而不是依赖属性)进行任何数据绑定时,绑定对象会查看该对象是否实现 INotifyPropertyChanged 并侦听 PropertyChanged 事件。
在您的模型上实现此操作将导致 UI 中的绑定属性正确更新。
但有一点需要注意,由于您的属性 SingularAmount 的实现方式(根据其他值计算),您还需要通知 UI 有关该属性的更改。
因此,在 ProductPrice 的设置器中,您需要引发 ProductPrice 以及 Singular Amount 的 PropertyChanged 事件。
此 http://msdn.microsoft.com/en- us/library/cc278072(VS.95).aspx 讨论 Silverlight 中的数据绑定以及通知的工作原理。
When you're doing any data binding in Silverlight on to standard CLR properties (as opposed to Dependency Properties) then the binding object looks to see if the object implements INotifyPropertyChanged and listens for PropertyChanged events.
Implementing this on your model will cause the binding properties to update in the UI correctly.
There is one caveat though, due to the way your property SingularAmount is implemented (calculated from other values) you'll need to notify the UI about changes to that property as well.
So in the setter of ProductPrice you'll need to raise the PropertyChanged event for ProductPrice as well as Singular Amount.
This http://msdn.microsoft.com/en-us/library/cc278072(VS.95).aspx discusses data binding in Silverlight and how notifications work.
我自己刚刚学习 RIA 服务,想知道在这种情况下是否可以覆盖默认构造函数? 虽然,我相信这种情况的推荐解决方案是使用 Setter 方法(如 {obj}.SetPrice(decimal Price); 中所示),而不是做你正在做的事情......只是我的 0.02 美元
I'm just learning RIA Services myself, and am wondering if you can override the default constructor in this case? Although, I believe the recommended solution for this scenario is to have a Setter method (as in {obj}.SetPrice(decimal price); instead of doing what you are doing... Just my $0.02