2016-02-12 15 views
6

Có ai biết cách chuyển các tham số đến Command bằng cách sử dụng CommandHandler không? Giả sử tôi muốn chuyển giá trị mã hóa chuỗi cứng từ XAML. Tôi biết làm thế nào để vượt qua từ XAML, nhưng không phải làm thế nào để xử lý nó trong mã MVVM phía sau. Mã bên dưới hoạt động tốt nếu không cần phải chuyển bất kỳ thông số nào.Truyền tham số cho Lệnh MVVM

public ICommand AttachmentChecked 
{ 
    get 
    { 
     return _attachmentChecked ?? (_attachmentChecked = new CommandHandler(() => ExecuteAttachmentChecked(), CanExecuteAttachmentChecked())); 
    } 
} 

private void ExecuteAttachmentChecked() 
{   
} 

private bool CanExecuteAttachmentChecked() 
{ 
    return true; 
} 

CommandHandler:

public class CommandHandler : ICommand 
{ 
    private Action _action; 
    private bool _canExecute; 

    public CommandHandler(Action action, bool canExecute) 
    { 
     _action = action; 
     _canExecute = canExecute; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return _canExecute; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     _action(); 
    } 
} 

Trả lời

7

Bạn cần thay đổi hai điều

1.Change bạn Commandhandler chấp nhận tham số

public class CommandHandler : ICommand 
{ 
    private Action<object> _action; 
    private bool _canExecute; 
    public CommandHandler(Action<object> action, bool canExecute) 
    { 
     _action = action; 
     _canExecute = canExecute; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return _canExecute; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     _action(parameter); 
    } 
} 

2.Thay đổi phương thức để chấp nhận CommandParameter:

public ICommand AttachmentChecked 
{ 
    get 
    { 
     return _attachmentChecked ?? (_attachmentChecked = new CommandHandler(param => ExecuteAttachmentChecked(param), CanExecuteAttachmentChecked())); 
    } 
} 

private void ExecuteAttachmentChecked(object param) 
{ 
//param will the value of `CommandParameter` sent from Binding 
} 

private bool CanExecuteAttachmentChecked() 
{ 
    return true; 
} 
5

Tham số được truyền trong phương pháp ICommand.Execute, bạn chỉ cần vượt qua nó thông qua để xử lý của bạn.

public class CommandHandler : ICommand 
{ 
    private Action<object> _action; 
    private bool _canExecute; 
    public CommandHandler(Action<object> action, bool canExecute) 
    { 
     _action = action; 
     _canExecute = canExecute; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return _canExecute; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     _action(parameter); 
    } 
} 
7

Bạn chỉ nên viết chuỗi của bạn vào CommandParameter:

<Button Command="{Binding NextCommand}" 
CommandParameter="Hello" 
Content="Next" /> 

và thay đổi từ:

private Action _action; 
private bool _canExecute; 

cho phép chấp nhận các thông số:

readonly Action<object> _execute;   
readonly Predicate<object> _canExecute; 

Giả sử rằng bạn đang sử dụng RelayCommand trong tham số của phương pháp objDoSmth sẽ là "Xin chào":

public RelayCommand YourCommand { get; set; } 
public YourViewModel() 
{ 
    NextCommand = new RelayCommand(DoSmth); 
} 

private void DoSmth(object obj) 
{ 
    Message.Box(obj.ToString()); 
} 
3

này đã được trả lời nhưng bạn có thể không biết rằng có một số Khung xung quanh để tránh mã boilerplate liên quan đến lệnh (và INotifyPropertyChanged) trong MVVM.

Một trong những nổi tiếng nhất là MVVM Light sẽ xử lý một vài điều cho bạn.

Sử dụng khuôn khổ này, đây là cách bạn xử lý một lệnh với các thông số:

(Lưu ý: Chúng ta sẽ tạo ra một lệnh làm thay đổi UserControl hiển thị, như UserControl là "phần chính" của ứng dụng)

1) trong ViewModel của bạn, xác định một RelayCommand với kiểu lập luận (trong trường hợp này, một usercontrol)

public RelayCommand<UserControl> changePageCommand { get; private set; } 

2) xác định phương pháp được binded đến lệnh (giản thể ở đây)

public void navigate(UserControl nextPage) 
{ 
    currentUserControl = nextPage; 
} 

3) Trói lệnh của bạn để phương pháp mà bạn chỉ định

changePageCommand = new RelayCommand<UserControl>((page) => navigate(page)); 

lệnh của bạn bây giờ tất cả các thiết lập và sẵn sàng để sử dụng trong XAML của bạn, giả sử bạn muốn chuyển sang trang với các lựa chọn thực đơn cho Ví dụ ...

4) Ràng buộc lệnh với tham số để Controls (XAML)

<Menu> 
    <MenuItem Header="_Pages"> 
     <MenuItem Header="_Account" 
        Command="{Binding changePageCommand}" 
        CommandParameter="{Binding pageManager.accountPage}" /> 
     <MenuItem Header="_Profile" 
        Command="{Binding changePageCommand}" 
        CommandParameter="{Binding pageManager.profilePage}" /> 
    </MenuItem> 
</Menu> 

Lưu ý: PageManager chỉ đơn giản là một lớp học mà INI tializes tất cả các trang của tôi để trạng thái mặc định của họ, có rõ ràng là một tham chiếu đến nó trong ViewModel của tôi.

Hy vọng điều này sẽ giúp bạn hoặc bất kỳ ai đi ngang qua

Các vấn đề liên quan