通过 caliburn micro Message.Attach 使用附加事件
我正在尝试使用 caliburn 微消息来触发我创建的附加事件:
public static class DataChanging
{
public delegate void DataChangingEventHandler(object sender, DataChangingEventArgs e);
public static readonly RoutedEvent ChangingEvent =
EventManager.RegisterRoutedEvent("Changing",
RoutingStrategy.Bubble,
typeof(DataChangingEventHandler),
typeof(DataChanging));
public static void AddChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).AddHandler(DataChanging.ChangingEvent, handler);
}
public static void RemoveChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).RemoveHandler(DataChanging.ChangingEvent, handler);
}
public static bool GetActivationMode(DependencyObject obj)
{
return (bool)obj.GetValue(ActivationModeProperty);
}
public static void SetActivationMode(DependencyObject obj, bool value)
{
obj.SetValue(ActivationModeProperty, value);
}
public static readonly DependencyProperty ActivationModeProperty =
DependencyProperty.RegisterAttached("ActivationMode",
typeof(bool),
typeof(DataChanging),
new FrameworkPropertyMetadata(false,
HandleActivationModeChanged));
private static void HandleActivationModeChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var dataGrid = target as XamDataGrid;
if (dataGrid == null) // if trying to attach to something else than a datagrid, just ignore
return;
if ((bool)e.NewValue)
{
dataGrid.RecordDeactivating += selector_RecordDeactivating;
}
else
{
dataGrid.RecordDeactivating -= selector_RecordDeactivating;
}
}
static void selector_RecordDeactivating(object sender, RecordDeactivatingEventArgs e)
{
var args = new DataChangingEventArgs(DataChanging.ChangingEvent,sender)
{
Data = ((DataRecord) e.Record).DataItem,
ShouldCancelChange = false
};
(sender as UIElement).RaiseEvent(args);
e.Cancel = args.ShouldCancelChange;
}
}
在 XAML 本身中,我添加了以下行:
cal:Message.Attach="[Helpers:DataChanging.Changing] = [Action SelectedDataChanged($eventArgs)]"
Helpers 引用正确的命名空间。 我还尝试了其他失败的版本(完整命名空间):
cal:Message.Attach="[clr-namespace:RTF.Client.UI.Helpers.DataChanging.Changing] = [Action SelectedDataChanged($eventArgs)]"
尝试自己设置交互事件:
当我尝试添加正常事件触发器时,一切正常,所以这不是我附加的事件声明:
<EventTrigger RoutedEvent="Helpers:DataChanging.Changing">
<EventTrigger.Actions>
<BeginStoryboard x:Name="sb">
<Storyboard x:Name="dsf">
<Storyboard x:Name="myStoryboard">
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="SSS" Storyboard.TargetProperty="IsChecked">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
我在这里做错了什么? 没有办法附加附加事件并使用 caliburn micro 调用它吗?
I'm trying to use caliburn micro message to trigger an attached event that I created:
public static class DataChanging
{
public delegate void DataChangingEventHandler(object sender, DataChangingEventArgs e);
public static readonly RoutedEvent ChangingEvent =
EventManager.RegisterRoutedEvent("Changing",
RoutingStrategy.Bubble,
typeof(DataChangingEventHandler),
typeof(DataChanging));
public static void AddChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).AddHandler(DataChanging.ChangingEvent, handler);
}
public static void RemoveChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).RemoveHandler(DataChanging.ChangingEvent, handler);
}
public static bool GetActivationMode(DependencyObject obj)
{
return (bool)obj.GetValue(ActivationModeProperty);
}
public static void SetActivationMode(DependencyObject obj, bool value)
{
obj.SetValue(ActivationModeProperty, value);
}
public static readonly DependencyProperty ActivationModeProperty =
DependencyProperty.RegisterAttached("ActivationMode",
typeof(bool),
typeof(DataChanging),
new FrameworkPropertyMetadata(false,
HandleActivationModeChanged));
private static void HandleActivationModeChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var dataGrid = target as XamDataGrid;
if (dataGrid == null) // if trying to attach to something else than a datagrid, just ignore
return;
if ((bool)e.NewValue)
{
dataGrid.RecordDeactivating += selector_RecordDeactivating;
}
else
{
dataGrid.RecordDeactivating -= selector_RecordDeactivating;
}
}
static void selector_RecordDeactivating(object sender, RecordDeactivatingEventArgs e)
{
var args = new DataChangingEventArgs(DataChanging.ChangingEvent,sender)
{
Data = ((DataRecord) e.Record).DataItem,
ShouldCancelChange = false
};
(sender as UIElement).RaiseEvent(args);
e.Cancel = args.ShouldCancelChange;
}
}
In the XAML itself I added the following line:
cal:Message.Attach="[Helpers:DataChanging.Changing] = [Action SelectedDataChanged($eventArgs)]"
Helpers refer to the right namespace.
I also tried other versions that failed (full namespace):
cal:Message.Attach="[clr-namespace:RTF.Client.UI.Helpers.DataChanging.Changing] = [Action SelectedDataChanged($eventArgs)]"
tried to set the interaction event by myself:
When I tried adding a normal event trigger everything worked well, so it's not my attached event declaration :
<EventTrigger RoutedEvent="Helpers:DataChanging.Changing">
<EventTrigger.Actions>
<BeginStoryboard x:Name="sb">
<Storyboard x:Name="dsf">
<Storyboard x:Name="myStoryboard">
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="SSS" Storyboard.TargetProperty="IsChecked">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
What am I doing wrong here?
There is no way to attach an attached event and invoke it using caliburn micro?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于明白了问题和解决方案。
问题是 system.windows.interactiviy.EventTrigger 不支持附加事件。
Caliburn micro 使用它来执行操作,因此我附加的事件不起作用。
解决方案是编写自定义事件触发器并以显式方式使用 calibburn 微操作。
自定义事件触发器取自该帖子:http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1323674885597#c8041424175408473805
然后当你想使用校准器动作:
finally I understand the problem and the solution.
The problem is that system.windows.interactiviy.EventTrigger doesn't support attached events.
Caliburn micro uses it for actions thus my attached event didn't work.
The solution was to write a customized event trigger and use caliburn micro action in a explicit manner.
the customized event trigger was taken from that post:http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1323674885597#c8041424175408473805
and then when you want to use caliburn action:
我认为缩短的 Message.Attach 语法的解析器不支持附加事件。但是为什么不直接将 ActionMessage 添加到 EventTrigger 的 Actions 中呢?
I dont think that the parser for the shortened Message.Attach syntax supports attached events. But why don't you just add the ActionMessage directly to the Actions of the EventTrigger?
你试过这个吗?
我需要将事件从子控件发送到父视图模型,这对我来说效果很好。我会发布一些示例代码也许会对某人有所帮助!
子控件代码隐藏:
子控件 XAML:
父视图:
Have you tried this?
I needed to send an event from a child control to the parents ViewModel, and it worked fine for me. I'll post some example code maybe it'll help someone out!
Child Control codebehind:
Child control XAML:
Parent view: