설치
가정하자 다음과 같습니다. 우리는 다음과 같은 이론적 뷰 모델 클래스는 응용 프로그램 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)
}
또한 다음 사항에 주의합니다.
- 현재 클래스를 구현하는 올바
INotifyPropertyChanged
- 모든 클래스에서 구성원의 액세스 체인에 제대로 구현
INotifyPropertyChanged
(예를 들어. 문서 뷰 모델에서 액세스ActiveDocument
성) ActiveDocument
수null
.ActiveDocument.Highlighting
수도 있습 null 입니다.
문제
나는 다음과 같은 명령하는 경우에만 사용하는 조건에 코멘트를 충족시킬 수 있습니다.
지 않고 옵션 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)
}
또는-만약 당신이 원하는 코드를 조금 더 읽기 쉬운,그래서 그 부분 조건을 다시 사용할 수 있습니다-다음과 같다:
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
클래스)가:
- 그것은 유의 인스턴스를 모두 구현하는
INotifyPropertyChanged
와 손잡이 변경(예를 들어. 면ActiveDocument
변경 또는ActiveDocument.Highlighting
변경 또는ActiveDocument.Highlighting.Type
변경 등) - 그것은 유지의 추적 가능
null
s 길에 어떤 경우에는 반환 기본값(이 경우에,false
) - 그것은 자동으로 보고서의 변화(하지만 변경)의 가용성하도록 명령 UI 될 수 있습 새로 고침할 때 필요합니다.
질문
어떻게 하나 구현 시나리오를 사용하면 위에서 설명 System.Reactive
C#? 그것은 그것을 쉽게 유지하면서 모든 요구 사항에 대해 INotifyPropertyChanged
null 방법에 기본값? 당신이 할 수 있는 모든 온전한 가정을 경우 필요합니다.