Caliburn Micro:在我的计算器应用程序中,我想要一种方法来处理所有 "number" 按钮 (0-9),每个按钮传递不同的值
CaliburnMicro: In my calculator app, I'd like one method to handle all of the "number" buttons (0-9) with each button passing a different value
目前,我的 calculator 上的每个按钮在 ShellViewmodel 中都有一个方法:
外壳视图:
<Button x:Name="Btn1" Content="1"/>
<Button x:Name="Btn2" Content="2"/>
<Button x:Name="Btn3" Content="3"/>
ShellViewmodel:
private string _input = string.Empty;
public string Input
{
get { return _input; }
set
{
_input = value;
NotifyOfPropertyChange(() => Input);
}
}
public void Btn1()
{
Input += "1";
}
public void Btn2()
{
Input += "2";
}
public void Btn3()
{
Input += "3";
}
我意识到这是对我的计算器进行编程的一种天真的方法,更好的解决方案是让每个按钮调用相同的方法并传递一个值(Btn1
将传递一个值 1,等等) .下面的代码不起作用;当我 运行 它并点击按钮时,没有任何反应。因为我是初学者,所以我可能会在绑定方面犯非常愚蠢的错误。将多个按钮绑定到一个方法,每个按钮传递自己的值的方法是什么?谢谢。
外壳视图:
<Button x:Name="Btn1" Command="{Binding NumberInput}" CommandParameter="1" Content="1"/>
<Button x:Name="Btn2" Command="{Binding NumberInput}" CommandParameter="2" Content="2"/>
<Button x:Name="Btn3" Command="{Binding NumberInput}" CommandParameter="3" Content="3"/>
ShellViewmodel:
private string _input = string.Empty;
public string Input
{
get { return _input; }
set
{
_display = value;
NotifyOfPropertyChange(() => Display);
}
}
public void NumberInput(string valuePassedFromButton)
{
Input += valuePassedFromButton;
}
根据您的 XAML,它尝试绑定名为 NumberInput
的命令作为默认 WPF 命令绑定样式,而不依赖于 Caliburn 的自动装配。但是,在您的 ViewModel 中,NumberInput
是方法的名称,而不是 ICommand
。
我猜你必须将 ICommand
的实现实例(如 RelayCommand
或 DelegateCommand
分配给 public ICommand NumberInput{ get; private set;}
,如 non-Caliburn 样式。
- 如果您实施了
RelayCommand
或 DelegateCommand
的示例:
...
public ICommand NumberInput{ get; private set;}
...
public ShellViewModel{
NumberInput=new RelayCommand<string>((valuePassedFromButton)=>{Input += valuePassedFromButton;});
}
...
built-in Command
属性 应该绑定到 ICommand
来源 属性。
在 Caliburn.Micro 中,您可以使用 Message.Attach
属性 将 Click
事件绑定到您的方法:
<Button cal:Message.Attach="[Event Click] = [Action NumberInput('1')]" Content="1"/>
<Button cal:Message.Attach="[Event Click] = [Action NumberInput('2')]" Content="2"/>
<Button cal:Message.Attach="[Event Click] = [Action NumberInput('3')]" Content="3"/>
目前,我的 calculator 上的每个按钮在 ShellViewmodel 中都有一个方法:
外壳视图:
<Button x:Name="Btn1" Content="1"/>
<Button x:Name="Btn2" Content="2"/>
<Button x:Name="Btn3" Content="3"/>
ShellViewmodel:
private string _input = string.Empty;
public string Input
{
get { return _input; }
set
{
_input = value;
NotifyOfPropertyChange(() => Input);
}
}
public void Btn1()
{
Input += "1";
}
public void Btn2()
{
Input += "2";
}
public void Btn3()
{
Input += "3";
}
我意识到这是对我的计算器进行编程的一种天真的方法,更好的解决方案是让每个按钮调用相同的方法并传递一个值(Btn1
将传递一个值 1,等等) .下面的代码不起作用;当我 运行 它并点击按钮时,没有任何反应。因为我是初学者,所以我可能会在绑定方面犯非常愚蠢的错误。将多个按钮绑定到一个方法,每个按钮传递自己的值的方法是什么?谢谢。
外壳视图:
<Button x:Name="Btn1" Command="{Binding NumberInput}" CommandParameter="1" Content="1"/>
<Button x:Name="Btn2" Command="{Binding NumberInput}" CommandParameter="2" Content="2"/>
<Button x:Name="Btn3" Command="{Binding NumberInput}" CommandParameter="3" Content="3"/>
ShellViewmodel:
private string _input = string.Empty;
public string Input
{
get { return _input; }
set
{
_display = value;
NotifyOfPropertyChange(() => Display);
}
}
public void NumberInput(string valuePassedFromButton)
{
Input += valuePassedFromButton;
}
根据您的 XAML,它尝试绑定名为 NumberInput
的命令作为默认 WPF 命令绑定样式,而不依赖于 Caliburn 的自动装配。但是,在您的 ViewModel 中,NumberInput
是方法的名称,而不是 ICommand
。
我猜你必须将 ICommand
的实现实例(如 RelayCommand
或 DelegateCommand
分配给 public ICommand NumberInput{ get; private set;}
,如 non-Caliburn 样式。
- 如果您实施了
RelayCommand
或DelegateCommand
的示例:
...
public ICommand NumberInput{ get; private set;}
...
public ShellViewModel{
NumberInput=new RelayCommand<string>((valuePassedFromButton)=>{Input += valuePassedFromButton;});
}
...
built-in Command
属性 应该绑定到 ICommand
来源 属性。
在 Caliburn.Micro 中,您可以使用 Message.Attach
属性 将 Click
事件绑定到您的方法:
<Button cal:Message.Attach="[Event Click] = [Action NumberInput('1')]" Content="1"/>
<Button cal:Message.Attach="[Event Click] = [Action NumberInput('2')]" Content="2"/>
<Button cal:Message.Attach="[Event Click] = [Action NumberInput('3')]" Content="3"/>