ที่ตั้งค่า
ลองสันนิษฐานว่าเออออตามหรอกนะ เรามีคนติดตาม ทฤษฎีนั้น viewmodel ชั้นเรียนสำหรับ WPF องโปรแกรม:
public MyViewModel
{
public MyViewModel()
{
// Condition under which this command may be executed is:
// this.ActiveDocument.Highlighting.Type == Highlighting.Xml &&
// !this.ActiveDocument.IsReadOnly &&
// (this.License.Kind == LicenseKind.Full || this.License.TrialDay < 30)
MyCommand = new Command(obj => DoSomething());
}
public ICommand MyCommand { get; }
// (all other required properties)
}
ที่เพิ่มมานอกเหนือจาก:
- ปัจจุบันเรียน implements เหมาะสม
INotifyPropertyChanged
- คลาสทั้งหมดในสมาชิกเข้าใช้งานล่ามโซ่ implements เหมาะสม
INotifyPropertyChanged
(eg. เอกสาร viewmodel สามารถเข้าใช้จากActiveDocument
ทรัพย์สิน) ActiveDocument
อาจจะเป็นnull
.ActiveDocument.Highlighting
อาจจะเป็นโพรโทคอล aimcomment.
มีปัญหา
ฉันอยากคำสั่งที่จะถูกเปิดใช้งานเดียวตอนนี้อาการป่วยในความเห็นคือพบกัน
ตัวเลือกโดยไม่มี RX
ฉันเขียน ของตัวเองสมุด สำหรับการจัดการสถานการณ์เช่นนี้. ทางออกที่จะเป็นเหมือนกั:
public MyViewModel
{
private readonly Condition commandAvailableCondition;
public MyViewModel()
{
commandAvailableCondition = new LambdaCondition(this,
vm => m.ActiveDocument.Highlighting.Type == Highlighting.Xml &&
!vm.ActiveDocument.IsReadOnly &&
(vm.License.Kind == LicenseKind.Full || vm.License.TrialDay < 30),
false);
MyCommand = new AppCommand(obj => DoSomething(), commandAvailableCondition);
}
public ICommand MyCommand { get; }
// (all other required properties)
}
หรือถ้าคุณต้องการรหัสเป็นมากกว่านี้หน่อสามารถอ่านได้ดังนั้นบางส่วนเงื่อนไขอาจเป็น reused-เหมือนว่า:
public MyViewModel
{
private readonly Condition commandAvailableCondition;
public MyViewModel()
{
var highlightingIsXml = new LambdaCondition(this,
vm => vm.ActiveDocument.Highlighting.Type == Highlighting.Xml,
false);
var documentIsReadonly = new LambdaCondition(this,
vm => vm.ActiveDocument.IsReadOnly,
false);
var appIsLicensed = new LambdaCondition(this,
vm => vm.License.Kind == LicenseKind.Full || this.License.TrialDay < 30,
false);
commandAvailableCondition = highlightingIsXml & !documentIsReadonly & appIsLicensed;
MyCommand = new AppCommand(obj => DoSomething(), commandAvailableCondition);
}
public ICommand MyCommand { get; }
// (all other required properties)
}
อะไรของฉันห้องสมุด(หรือมากกว่าถูกต้อง, LambdaCondition
ชั้นเรียน)มันคือ:
- มันทำให้ตามรของโห implementing
INotifyPropertyChanged
และจัดการเปลี่ยนแปลง(eg. ตอนที่ActiveDocument
เปลี่ยนแปลงหรือActiveDocument.Highlighting
เปลี่ยนแปลงหรือActiveDocument.Highlighting.Type
เปลี่ยนแปลงเป็นต้น) - มันทำให้ตามความเป็นไปได้ว่าเขาจะเป็น
null
เออยู่บนทางในกรณีนั้นมันจะต้องกลับมาค่าปริยาย(ในกรณีนี้false
) - มันจะโดยอัตโนมัติรายงานเปลี่ยนแปลง(แต่เดียวที่เปลี่ยนแปลง)ของตั้งค่าโครงการจะบังคับบัญชา"ดังนั้นส่วนติดต่อผู้ใช้ขออาจจะปรับปรุงข้อมูลแล้วตอนที่ต้องการ
คำถาม
ว่าเป็นเพียงคนเดียด้วยอะไรซักอย่างที่สร้างสถานการณ์จำลอเรียกเหนือการใช้ System.Reactive
ใน C#? มันเป็นไปได้ที่จะทำมันอย่างง่ายดายขณะที่ชอบเก็บทุกอย่างเรื่องความต้องการ INotifyPropertyChanged
,nulls ระหว่างทางและค่าปริยาย? คุณอาจจะทำให้มีสติดีทุกตั้งสมมติฐานตอนที่ต้องการ