gpt4 book ai didi

wpf - 用于单按钮事件的 ICommand

转载 作者:行者123 更新时间:2023-12-03 10:23:29 26 4
gpt4 key购买 nike

下面的代码是我的 WPF 应用程序的 MVVM 类。在我的 MainWindow.xaml.cs 文件 MainWindow() 构造函数中,我已经这样做了。

oneWayAuth = new OneWayAuthentication();
DataContext = oneWayAuth

我的主窗口包含多个按钮,我需要使用 ICommand 通过像这样的绑定(bind)来分配事件,

主窗口.xaml
<Grid>
<Button> Command="{Binding ClickCommand}" Content="Button" Grid.Row="1" Name="button1" />
</Grid>

按钮的内部事件,我应该可以访问 oneWayAuth.RandomNumber属性,以便我可以更改它。

我尝试使用下面的方法。但我无法通过返回类型的 Action 委托(delegate)。

OneWayAuthentication.cs
public class OneWayAuthentication : INotifyPropertyChanged
{
private string certData;
private string isVerifiedCert;
private string randomNumber;
private string response;
private string isVerifiedRes;
private string resultAuth;

public string RandomNumber
{
get
{
return randomNumber;
}
set
{
randomNumber = value;
NotifyPropertyChanged("RandomNumber");
}
}

public ICommand ClickCommand
{
get
{
ICommand intfaceCmnd = new CommandHandler(() => Execute(), () => Switch());
return intfaceCmnd;
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion

#region Private Helpers

private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion

public bool Switch()
{
return true;
}

public void Execute()
{
this.RandomNumber = "this is random number";
}
}

命令处理程序.cs
public delegate bool delCanExecute(object parameter); 
public class CommandHandler:ICommand
{
private Action _action;
private Action _canExecute;
public CommandHandler(Action action1, Action action2)
{
_action = action1;
_canExecute = action2;
}

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

public void Execute(object parameter)
{
_action();
}
public event EventHandler CanExecuteChanged;
}

最佳答案

来自 MVVM light 的典型命令处理程序 (RelayCommand)

class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;

/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand"/> class.
/// </summary>
/// <param name="execute">The method to be called when the command is
/// invoked.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{ }

/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand"/> class.
/// </summary>
/// <param name="execute">The method to be called when the command is
/// invoked.</param>
/// <param name="canExecute">the method that determines whether the command
/// can execute in its current state.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

/// <summary>
/// Defines the method that determines whether the command can execute in
/// its current state.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does
/// not require data to be passed, this object can be set to null.</param>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}

/// <summary>
/// Occurs when changes occur that affect whether or not the command should
/// execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does
/// not require data to be passed, this object can be set to null.</param>
public void Execute(object parameter)
{
_execute(parameter);
}
}

对对象的操作意味着您可以将参数传递给您的命令,对于谓词也是如此,但它返回 bool 值。

关于wpf - 用于单按钮事件的 ICommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17871964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com