如何使用Xamarin Forms创建在ReactiveUI 7中接收字符串的ReactiveCommand

时间:2016-12-15 10:28:21

标签: xamarin.forms reactiveui

在我的LoginPage.xaml.cs中,我有一个webview wview。我想在我的wview中触发Navigated事件时在我的ViewModel中执行命令。我的命令应该从我的网页视图中收到网址。

LoginPage.xaml.cs:

protected override void OnAppearing()
{
    base.OnAppearing();
    Observable.FromEventPattern<WebNavigatedEventArgs>(
        ev => wview.Navigated += ev,
        ev => wview.Navigated -= ev)
        .Select(x => x.EventArgs.Source.ToString())
        .InvokeCommand(ViewModel.VerifyCallbackUrl);
}

如何创建可以对此作出反应的命令?以下代码无法编译(Delegate操作不带1个参数):

public ReactiveCommand<string,System.Reactive.Unit> VerifyCallbackUrl { get; protected set; }

public LoginViewModel(IScreen hostScreen = null) : base(hostScreen) 
{   
    VerifyCallbackUrl = ReactiveCommand<string, System.Reactive.Unit>
        .Create(xUrl => 
        {
            DoSomethingUseful();
        }); 
}

1 个答案:

答案 0 :(得分:1)

明确关于Action的类型参数,它将编译:

public LoginViewModel(IScreen hostScreen = null) : base(hostScreen)
{
    VerifyCallbackUrl = ReactiveCommand.Create(new Action<string>(xUrl => { DoSomethingUseful(); }));
}