通过 caliburn micro Message.Attach 使用附加事件

发布于 2024-12-19 19:25:01 字数 4012 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗼ふ静 2024-12-26 19:25:01

我终于明白了问题和解决方案。
问题是 system.windows.interactiviy.EventTrigger 不支持附加事件。
Caliburn micro 使用它来执行操作,因此我附加的事件不起作用。
解决方案是编写自定义事件触发器并以显式方式使用 calibburn 微操作。
自定义事件触发器取自该帖子:http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1323674885597#c8041424175408473805

 public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
    RoutedEvent _routedEvent; 
    public RoutedEvent RoutedEvent
    {
        get { return _routedEvent; } 
        set { _routedEvent = value; }
    } 

    public RoutedEventTrigger() { } 
    protected override void OnAttached()
    {
        Behavior behavior = base.AssociatedObject as Behavior; 
        FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement; 
        if (behavior != null)
        {
            associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
        } 
        if (associatedElement == null)
        {
            throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
        } 
        if (RoutedEvent != null) 
        { associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent)); }
    } 
    void OnRoutedEvent(object sender, RoutedEventArgs args)
    {
        base.OnEvent(args);
    } 
    protected override string GetEventName() { return RoutedEvent.Name; }
}

然后当你想使用校准器动作:

<i:Interaction.Triggers>
                <!--in the routed event property you need to put the full name space and event name-->
                <Helpers:RoutedEventTrigger RoutedEvent="Helpers:DataChanging.Changing">
                    <cal:ActionMessage MethodName="SelectedDataChanged">
                        <cal:Parameter Value="$eventargs" />
                    </cal:ActionMessage>
                </Helpers:RoutedEventTrigger>
 </i:Interaction.Triggers>

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

 public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
    RoutedEvent _routedEvent; 
    public RoutedEvent RoutedEvent
    {
        get { return _routedEvent; } 
        set { _routedEvent = value; }
    } 

    public RoutedEventTrigger() { } 
    protected override void OnAttached()
    {
        Behavior behavior = base.AssociatedObject as Behavior; 
        FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement; 
        if (behavior != null)
        {
            associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
        } 
        if (associatedElement == null)
        {
            throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
        } 
        if (RoutedEvent != null) 
        { associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent)); }
    } 
    void OnRoutedEvent(object sender, RoutedEventArgs args)
    {
        base.OnEvent(args);
    } 
    protected override string GetEventName() { return RoutedEvent.Name; }
}

and then when you want to use caliburn action:

<i:Interaction.Triggers>
                <!--in the routed event property you need to put the full name space and event name-->
                <Helpers:RoutedEventTrigger RoutedEvent="Helpers:DataChanging.Changing">
                    <cal:ActionMessage MethodName="SelectedDataChanged">
                        <cal:Parameter Value="$eventargs" />
                    </cal:ActionMessage>
                </Helpers:RoutedEventTrigger>
 </i:Interaction.Triggers>
美煞众生 2024-12-26 19:25:01

我认为缩短的 Message.Attach 语法的解析器不支持附加事件。但是为什么不直接将 ActionMessage 添加到 EventTrigger 的 Actions 中呢?

<EventTrigger RoutedEvent="Helpers:DataChanging.Changing">
    <EventTrigger.Actions>
        <!-- new part -->
        <cal:ActionMessage MethodName="SelectedDataChanged">  
            <cal:Parameter Value="$eventargs" />  
        </cal:ActionMessage>  
        <!-- /new part -->
        <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>

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?

<EventTrigger RoutedEvent="Helpers:DataChanging.Changing">
    <EventTrigger.Actions>
        <!-- new part -->
        <cal:ActionMessage MethodName="SelectedDataChanged">  
            <cal:Parameter Value="$eventargs" />  
        </cal:ActionMessage>  
        <!-- /new part -->
        <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>
心碎的声音 2024-12-26 19:25:01

你试过这个吗?

cal:Message.Attach="[Event Changing] = [Action SelectedDataChanged($eventArgs)]"

我需要将事件从子控件发送到父视图模型,这对我来说效果很好。我会发布一些示例代码也许会对某人有所帮助!

子控件代码隐藏:

public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    #region Routed Events

    public static readonly RoutedEvent ControlClosedEvent = EventManager.RegisterRoutedEvent(
        "ControlClosed",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(MyControl));

    public event RoutedEventHandler ControlClosed
    {
        add { AddHandler(ControlClosedEvent, value); }
        remove { RemoveHandler(ControlClosedEvent, value); }
    }

    #endregion Routed Events

    private void Close(object sender, RoutedEventArgs e)
    {
        var rea = new RoutedEventArgs(ControlClosedEvent);
        RaiseEvent(rea);
    }
}

子控件 XAML:

<Button Grid.Row="1" Grid.Column="0"
        Content="Close Me!" Height="50"
        Click="Close" />

父视图:

<userControls:MyControl cal:Message.Attach="[Event ControlClosed] = [Action ClosePopup]" />

Have you tried this?

cal:Message.Attach="[Event Changing] = [Action SelectedDataChanged($eventArgs)]"

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:

public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }

    #region Routed Events

    public static readonly RoutedEvent ControlClosedEvent = EventManager.RegisterRoutedEvent(
        "ControlClosed",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(MyControl));

    public event RoutedEventHandler ControlClosed
    {
        add { AddHandler(ControlClosedEvent, value); }
        remove { RemoveHandler(ControlClosedEvent, value); }
    }

    #endregion Routed Events

    private void Close(object sender, RoutedEventArgs e)
    {
        var rea = new RoutedEventArgs(ControlClosedEvent);
        RaiseEvent(rea);
    }
}

Child control XAML:

<Button Grid.Row="1" Grid.Column="0"
        Content="Close Me!" Height="50"
        Click="Close" />

Parent view:

<userControls:MyControl cal:Message.Attach="[Event ControlClosed] = [Action ClosePopup]" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文