Skip to content

Instantly share code, notes, and snippets.

@pensacola1989
Forked from waynebaby/vm_property_logic.cs
Created November 23, 2012 07:29
Show Gist options
  • Save pensacola1989/4134372 to your computer and use it in GitHub Desktop.
Save pensacola1989/4134372 to your computer and use it in GitHub Desktop.
MVVMSideKick 用Rx来配置属性之间的关系
namespace TableGameSidekick_Metro.Games.DefaultTradeGame.Models
{
[DataContract]
public class ResourceConfig : BindableBase<ResourceConfig>
{
// If you have install the code sniplets, use "propvm + [tab] +[tab]" create a property。
// 如果您已经安装了 MVVMSidekick 代码片段,请用 propvm +tab +tab 输入属性
public ResourceConfig(int players)
{
ValidateModel =
_ =>
{
if (CheckError(() => TotalAmount < 0, "ERROR_TotalAmount_LESS_THAN_ZERO")) return;
if (CheckError(() => EachPlayerAmount < 0, "ERROR_EachPlayerAmount_LESS_THAN_ZERO")) return;
if (HasLimitition)
{
if (CheckError(() => EachPlayerAmount * Players > TotalAmount, "ERROR_EACH_PLAYER_AMOUNT_OVERFLOW")) return;
}
};
this.GetValueContainer(X => X.TotalAmount)
.GetValueChangedObservableWithoutArgs()
.Merge (
this
.GetValueContainer(x => x.HasLimitition)
.GetValueChangedObservableWithoutArgs())
.Subscribe(
x =>
{
this.MaxPerPlayer = (this.HasLimitition) ? TotalAmount / players : 1000000;
}
)
.DisposeWith(this);
this.GetValueContainer(x => x.MaxPerPlayer)
.GetValueChangedObservableWithoutArgs()
.Where(_ => this.MaxPerPlayer > this.EachPlayerAmount)
.Subscribe(_ => this.EachPlayerAmount = MaxPerPlayer)
.DisposeWith(this);
}
....
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment