路由命令问题。 [英] Problem with Routed Commands.
问题描述
Winforms(XAML)
执行路由命令(我在路由命令执行时放置了一个消息框并更改了表单标题 - 和它工作了很多次 - 我有10个按钮并按下它们,每一个都改变了表单标题,然后把消息框放好了)但是当我卸载表单并重新加载它时虽然路由命令把消息箱放了 - 它不会改变表单标题就在消息框之前的代码中!我尝试用文本框做类似的事情并且它做了同样的事情,它似乎通过执行代码 - 我可以在调试器中看到它,但是在UI中没有任何变化 - 尽管它在表单的初始加载时也是如此。它只在我卸载表格并重新加载后才停止发生。
有人会有任何想法吗?
谢谢
单独路由命令类
private static readonly RoutedUICommand convPadEditButton = 新 RoutedUICommand( 编辑转换插入, ConvPadEditButton, typeof (RoutedCommands));
public static RoutedUICommand ConvPadEditButton
{
< span class =code-keyword> get { return convPadEditButton; }
}
路由命令 - (在表单构造函数中设置)
var convpadeditbinding = new CommandBinding(RoutedCommands.ConvPadEditButton,ConvPadEditButton_Execute,ConvPadEditButton_CanExecute);
CommandManager.RegisterClassCommandBinding( typeof (Window),convpadeditbinding);
可以执行和执行
<前lang =cs> 私人 < span class =code-keyword> void ConvPadEditButton_Execute( object sender,ExecutedRoutedEventArgs e)
{
string butName =((Button)e.OriginalSource).Name;
this .Title = butName;
MessageBox.Show(butName);
e.Handled = true ;
}
private void ConvPadEditButton_CanExecute( object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = true ;
}
触发路由命令(来自表单中加载的用户控件) < br $>
私人 void Button_Click( object sender,RoutedEventArgs e)
{
Button but =(Button)e.OriginalSource;
RoutedCommands.ConvPadEditButton.Execute(PadID,but);
}
好的,我似乎已经解决了这个问题 - 这就是我做的...... >
我更改了CommandManager.RegisterClassCommandBinding(.....)
TO
this.CommandBindings.Add(......)
如下图所示 - 这是有效的 - 现在我可以在重新加载时正常卸载表格和
。
// CommandBinding convpadeditbinding = new CommandBinding(RoutedCommands.ConvPadEditButton,ConvPadEditButton_Execute,ConvPadEditButton_CanExecute);
// CommandManager.RegisterClassCommandBinding(typeof(ConvButtonEditWindow),convpadeditbinding);
this .CommandBindings.Add(新 CommandBinding(RoutedCommands.ConvPadEditButton,ConvPadEditButton_Execute ,ConvPadEditButton_CanExecute));
Winforms (XAML)
A Routed command executes (I put a messagebox up and alter the form title on routed command execute - and it works many times - I have 10 buttons and press them all and every one alters the form title and THEN puts up the messagebox) but when I unload the form and load it again although the routed command puts the messagebox up - it will not alter the form title which is just in the code before the messagebox! I tried doing similar with a textbox and it did the same, it seems to go through the Execute code - I can see it in the debugger, but then nothing changes in the UI - although it does on initial load of the form. It only stops happening after I unload the form and reload it.
Would anyone have any ideas?
Thanks
Separate Routed Command class
private static readonly RoutedUICommand convPadEditButton = new RoutedUICommand("Editing Conversion Insert", "ConvPadEditButton", typeof(RoutedCommands));
public static RoutedUICommand ConvPadEditButton
{
get { return convPadEditButton; }
}
Routed Command - (set in Form Constructor)
var convpadeditbinding = new CommandBinding(RoutedCommands.ConvPadEditButton, ConvPadEditButton_Execute, ConvPadEditButton_CanExecute);
CommandManager.RegisterClassCommandBinding(typeof(Window), convpadeditbinding);
Can Execute and Execute
private void ConvPadEditButton_Execute(object sender, ExecutedRoutedEventArgs e)
{
string butName = ((Button)e.OriginalSource).Name;
this.Title = butName;
MessageBox.Show(butName);
e.Handled = true;
}
private void ConvPadEditButton_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
Firing the Routed Command (from a user control loaded in the form)
private void Button_Click(object sender, RoutedEventArgs e)
{
Button but = (Button)e.OriginalSource;
RoutedCommands.ConvPadEditButton.Execute(PadID, but);
}
Okay, I seem to have fixed this - here is what I did....
I changed the CommandManager.RegisterClassCommandBinding(.....)
TO
this.CommandBindings.Add(......)
As seen below - and this works - now I can unload the form and
when reloading it works correctly.
//CommandBinding convpadeditbinding = new CommandBinding(RoutedCommands.ConvPadEditButton, ConvPadEditButton_Execute, ConvPadEditButton_CanExecute); //CommandManager.RegisterClassCommandBinding(typeof(ConvButtonEditWindow), convpadeditbinding); this.CommandBindings.Add(new CommandBinding(RoutedCommands.ConvPadEditButton, ConvPadEditButton_Execute, ConvPadEditButton_CanExecute));
这篇关于路由命令问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!