簡體   English   中英

將自定義方法與ViewModel中的參數綁定

[英]Bind custom method with parameters in ViewModel

我正在創建一個UWP MVVM應用程序。 我已經創建了ViewModel和View,並使其成為DataContext,並且綁定一切正常。

我能夠調用沒有參數的方法。 這是我在View中的XAML代碼:

<Button VerticalAlignment="Top" HorizontalAlignment="Right" Margin="50" Width="50" Height="50" Background="Transparent" BorderBrush="Transparent" Content="OK" FontSize="32" FontWeight="Bold" Foreground="White" Click="{x:Bind Path=ViewModel.ButtonMainClick, Mode=OneWay}"/> 

在ViewModel中,我是這樣的:

public void ButtonMainClick()
{
    // TO DO
}

這樣就可以了。

現在,我想調用一些具有參數的方法。 但是我無法做到這一點。 我在互聯網上看到有一個EventTriggerBehavior。 但是我不確定如何使用它並傳遞一些參數。

任何想法?

假設UWP和WPF綁定的工作原理相同,則應將按鈕綁定到ViewModel ICommand類型的屬性。 ICommand的常見實現是RelayCommand ,它看起來可能像這樣:

 public class RelayCommand : ICommand
    {
        private readonly Action _targetExecuteMethod;
        private readonly Func<bool> _targetCanExecuteMethod;
        public RelayCommand(Action executeMethod) => _targetExecuteMethod = executeMethod;

        public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
        {
            _targetExecuteMethod = executeMethod;
            _targetCanExecuteMethod = canExecuteMethod;
        }
        public void RaiseCanExecuteChanged() => CanExecuteChanged(this, EventArgs.Empty);
        bool ICommand.CanExecute(object parameter) => _targetCanExecuteMethod?.Invoke() ?? _targetExecuteMethod != null;
        public event EventHandler CanExecuteChanged = delegate { };
        void ICommand.Execute(object parameter) => _targetExecuteMethod?.Invoke();
    }
    public class RelayCommand<T> : ICommand
    {
        private readonly Action<T> _targetExecuteMethod;
        private readonly Func<T, bool> _targetCanExecuteMethod;
        public RelayCommand(Action<T> executeMethod) => _targetExecuteMethod = executeMethod;
        public RelayCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
        {
            _targetExecuteMethod = executeMethod;
            _targetCanExecuteMethod = canExecuteMethod;
        }
        public void RaiseCanExecuteChanged() => CanExecuteChanged(this, EventArgs.Empty);
        bool ICommand.CanExecute(object parameter)
        {
            if (_targetCanExecuteMethod != null)
            {
                var tparm = (T)parameter;
                return _targetCanExecuteMethod(tparm);
            }
            return _targetExecuteMethod != null;
        }
        public event EventHandler CanExecuteChanged = delegate { };
        void ICommand.Execute(object parameter) => _targetExecuteMethod?.Invoke((T)parameter);
    }

對於View和ViewModel,請看以下示例: https : //stackoverflow.com/a/53045098/9889260

如果要使用參數。 你只需要使用普通的RelayCommand<T>其中T是要作為參數傳遞,也給你的類型ExcecuteCanExcecute (如果有的話)的方法T作為參數。

親切的問候,錯

使用x:Bind事件處理程序不能接受任意參數。

一個簡單的解決方案是公開另一個為您調用的方法。 綁定到Click事件的方法沒有參數,但僅調用您需要執行的方法。

[編輯]如果需要從生成事件的控件中傳遞上下文,則可以通過添加事件簽名並訪問senderDataContext並強制轉換ViewModel類型來實現。

void FuncThatDoesSomething(T1 arg1, T2 arg2,...)
{
  // do it
}

// function has to match the event handler signature 
void FuncThatIsBoundInXaml(object sender, RoutedEventArgs e)
{
  var vm = sender.DataContext as YourViewModelType;
  // call method with required args
  FuncThatDoesSomething(vm.SomeProperty, 42);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM