2017-08-10 99 views
-1

我有一個recyclerview顯示項目的視圖有一個開關和一個按鈕。當切換項目的開關時,我需要啓用或禁用該視圖的按鈕。我如何使用MVVMLight和Xamarin.Android來做到這一點?我能得到一些示例代碼嗎?我有我的ViewModel中的RelayCommand的swith.checked綁定。MVVMLight和禁用/啓用按鈕

+0

你可以分享到目前爲止嘗試過的代碼嗎? –

+0

請考慮閱讀[問]如何寫一個好問題。正如你所看到的那樣,它正在下降,甚至接近投票。 原因可能包括: - 要求教程/例子是無題的,如果需要的話,無論如何,例子將包含在答案中。 - 如果有可能的話,包括一個[mcve]或者至少你最好的嘗試是「沒有工作」。 – Fildor

回答

0

假設以下視圖模型:

private bool _switch = true; 

// Bind switch.IsChecked to this property: 
public bool Switch 
{ 
    get => _switch; 
    set => Set(nameof(Switch), ref _switch, value); 
} 

private RelayCommand _btnCmd; 
public RelayCommand ButtonCommand => _btnCmd ?? (_btnCmd = 
         new RelayCommand(ButtonCommand_Execute, ButtonCommand_CanExecute)); 

// What will happen on Button Click  
private void ButtonCommand_Execute(){ /* ...*/ } 

// Sets the Enabled Property of the Button according to the state of the Switch property. 
private bool ButtonCommand_CanExecute() { return Switch; } 

這應該做的伎倆。 在需要明確通知CanExecute值更改的情況下,可以致電ButtonCommand.RaiseCanExecuteChanged()

這就是我在WPF中的做法;不過,我不知道是否有與Xamarin一起的專業。