如何执行 Silverlight ICommand?
这是一个非常基本的问题,但我不得不问。
在 SL 中,我有这个 XAML:
<UserControl.Resources>
<local:Commands x:Key="MyCommands" />
</UserControl.Resources>
<Button Content="Click Me"
Command="{Binding Path=Click, Source={StaticResource MyCommands}}"
CommandParameter="Hello World" />
后面的代码是:
public class Commands
{
public ClickCommand Click = new ClickCommand();
public sealed class ClickCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
MessageBox.Show(parameter.ToString());
}
}
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
但是当我单击按钮时,命令的 Execute() 永远不会被触发。
有什么窍门吗?
This is such a basic question, but I have to ask.
In SL, I have this XAML:
<UserControl.Resources>
<local:Commands x:Key="MyCommands" />
</UserControl.Resources>
<Button Content="Click Me"
Command="{Binding Path=Click, Source={StaticResource MyCommands}}"
CommandParameter="Hello World" />
And this code behind:
public class Commands
{
public ClickCommand Click = new ClickCommand();
public sealed class ClickCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
MessageBox.Show(parameter.ToString());
}
}
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
But when I click the button, the Command's Execute() is never fired.
Is there a trick to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有什么技巧,您的问题在于 XAML 和 C# 类之间的绑定。您不能仅将字段绑定到属性。
There's no trick, your problem lies in the binding between your XAML and C# class. You can't bind to a field only to a property.