2016-12-28 99 views
0

我爲我的xamarin ios項目使用MVVMCross。在我的自定義單元格視圖中,我想將參數傳遞給其視圖模型。有可能使用MVVMCross?Xamarin.IOS:MVVMCross從視圖傳遞參數到ViewModel

我試過使用CommandParameter屬性,但它沒有工作。是否有可能將參數從視圖傳遞到視圖模型,如果有人可以提供一個片段?

謝謝

更新

我的手機有按鈕,按鈕點擊我想知道按鈕的小區索引被點擊進行操作。我正在使用下面的代碼來做到這一點。

this.DelayBind(() => 
{ 
    var bSet = this.CreateBindingSet<MyeCell, SomeViewModel>(); 
    bSet.Bind(cellIndex).To(vm => vm.index); 
    bSet.Bind(UserPostBtn).To(vm => vm.EditPhotoCommand); 
    bSet.Apply(); 
}); 

我試着用delaybind連接視圖,視圖模型,但按鈕clickm我收到以下錯誤:

invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

+0

你想在單擊單元格時傳遞參數嗎?你能否提供一些代碼,以便我們可以看到你想傳遞給視圖模型的內容。 – Plac3Hold3r

+0

我更新了我的問題@ Plac3Hold3r – TheDeveloper

回答

3

您應該能夠通過使用MvxCommandParameterValueConverter的命令將參數傳遞到ViewModel

字符串名稱的方法:

bSet 
    .Bind(UserPostBtn) 
    .To(vm => vm.EditPhotoCommand) 
    .WithConversion("CommandParameter", cellIndex); 

鍵入的名稱的方法:

bSet 
    .Bind(UserPostBtn) 
    .To(vm => vm.EditPhotoCommand) 
    .WithConversion(new MvxCommandParameterValueConverter(), cellIndex); 

擴展方法的方法:

bSet 
    .Bind(MainButton) 
    .To(vm => vm.EditPhotoCommand) 
    .CommandParameter(cellIndex); 

然後在你的ViewModel中接收傳入的參數。

IMvxCommand _editPhotoCommand; 
public IMvxCommand EditPhotoCommand => 
    _editPhotoCommand ?? (_editPhotoCommand = new MvxCommand<int>(EditPhotoExecution)); 

private void EditPhotoExecution(int index) 
{ 
    // do stuff 
} 

更新:

MvvmCross還提供了一個擴展方法,CommandParameter,它允許你只是傳遞的命令參數,它會處理MvxCommandParameterValueConverter的創建。

1

請看看在official documentation for MvvmCross,以及如何與UITableViews和UITableViewCells工作。

基本上,您的MvxTableViewSource子類會獲得有關選定單元格的通知。現在它只是一個檢查您的模型和匹配索引的問題...

如果您需要知道輸入的文本或切換在您單元格中翻轉,您應該考慮使用您的單元格子類和您的細胞來源。確保在你的UITableViewCell子類中使用DelayBind

+0

我使用'DelayBind',但是當我點擊按鈕時,我得到了一個'invalid mode'kCFRunLoopCommonModes'提供給CFRunLoopRunSpecific的錯誤 - break在_CFRunLoopError_RunCalledWithInvalidMode中進行調試。此消息只會在每次執行時出現一次。「# – TheDeveloper

+0

您不應將其直接綁定到視圖模型,而應將其與源代碼綁定。 –