2016-12-15 57 views
2

在我的LoginPage.xaml.cs中,我有一個webview wview。我想在我的視圖中觸發Navigated事件時在ViewModel中執行命令。我的命令應該從我的webview中接收到這個url。如何使用Xamarin Forms創建一個在ReactiveUI 7中接收字符串的ReactiveCommand

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); 
} 

如何創建一個命令,可以在此作何反應?下面的代碼無法編譯(代表行爲並不需要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

更明確一些行動的類型參數,它會編譯:

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

[這裏有一個很好的教程] (http://fullstackmark.com/post/5/a-simple-vocabulary-app-using-reactiveui-and-xamarin-forms)恰好覆蓋了ReactiveUI和Xamarin Forms中的命令。 – mmacneil007

+0

偉大的提示! thx @ mmacneil007 – jtourlamain

相關問題