2015-12-17 17 views
0

以下行抛出一个运行时异常:ReactiveCommand.Create抛出“NotSupportedException”:“索引表达式仅支持常量。”

Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.Canexecute())); 

下面的代码:

public class InstructionsViewModel : ReactiveObject 
{ 
    public InstructionsViewModel() 
    { 
     Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.CanExecute)); 
     Accept.Subscribe(x => 
      { 
      Debug.Write("Hello World"); 
      }); 
    } 

     public ReactiveCommand<object> Accept { get; } 

    bool _canExecute; 
    public bool CanExecute { get { return _canExecute; } set { this.RaiseAndSetIfChanged(ref _canExecute, value); } } 
} 

错误:

Cannot convert lambda expression to type 'IObserver' because it is not a delegate type

我也试过如下:

public InstructionsViewModel() 
    { 
     Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.Canexecute())); 
     Accept.Subscribe(x => 
     { 
      Debug.Write("Hello World"); 
     }); 
    } 

    public ReactiveCommand<object> Accept { get; } 


    public bool Canexecute() => true; 

我收到以下错误:

An exception of type 'System.NotSupportedException' occurred in ReactiveUI.dll but was not handled in user code

Additional information: Index expressions are only supported with constants.

这甚至支持在Windows Phone上10?

回答

1

我想你的问题不是ReactiveCommand,而是WhenAnyValue

WhenAnyValue接受一个属性,而你用一个方法提供它,这会导致运行时异常(see the sourcecode)

检查,如果这个工程(我改变CanExecute是一个属性,而不是一种方法):

public InstructionsViewModel() 
{ 
    Accept = ReactiveCommand.Create(this.WhenAnyValue(x => x.CanExecute)); 
    Accept.Subscribe(x => 
    { 
     Debug.Write("Hello World"); 
    }); 
} 

public ReactiveCommand<object> Accept { get; } 

private bool _canExecute; 
public bool CanExecute { get { return _canExecute; } set { this.RaiseAndSetIfChanged(ref _canExecute, value); } } 

此外,作为一般的建议 - 不要窝您的来电,这使得调试困难。你应该将创建命令分成两行:

var canExecute = this.WhenAnyValue(x => x.CanExecute) 
Accept = ReactiveCommand.Create(canExecute); 
+0

我试过了你的建议。但是,我收到以下错误:无法将lambda表达式转换为键入'IObserver ',因为它不是委托类型。 –

+0

UWP(即Windows Phone 10)支持此功能吗? –

+0

我不得不承认,我很困惑,根据网站,这也应该在UW​​P上工作。你能用新版本的代码和错误细节来编辑你的问题吗? – pmbanka

相关问题