Silverlight自定义EventTrigger

发布于 2024-12-01 13:40:46 字数 1590 浏览 0 评论 0原文

我正在尝试创建一个自定义事件来触发 Silverlight 中的动画。尽管事件被触发,但动画不起作用。相关代码如下:

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            MyEvent += new ChangedEventHandler(UserControl_MyEventHandler);
            /* Other stuff */
        }
        private void UserControl_MyEventHandler(object sender, RoutedEventArgs e)
        {
                MessageBox.Show("MyEventHandler has been called");
        }

        public delegate void ChangedEventHandler(object sender, RoutedEventArgs e);

        private event ChangedEventHandler MyEvent;

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
                if (MyEvent != null)
                    MyEvent(this, e);
        }
    }
}

XAML代码如下:

<UserControl
... 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d"
x:Class="SilverlightApplication1.MainPage" MouseLeftButtonDown="UserControl_MouseLeftButtonDown">

    <Grid x:Name="LayoutRoot" Background="White">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MyEvent">
                <ei:GoToStateAction StateName="Highlighted"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        ...
    </Grid>
</UserControl>

当前,包含“MouseLeftButtonDown”的消息框正在显示,但动画没有被调用。当 EventTrigger EventName 为 MouseLeftButtonDown 而不是 MyEvent 时,动画确实被调用。请帮帮我。谢谢。

I am trying to create a custom event to trigger an animation in Silverlight. Although the event is getting triggered, the animation is not working. The following is the relevant code:

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            MyEvent += new ChangedEventHandler(UserControl_MyEventHandler);
            /* Other stuff */
        }
        private void UserControl_MyEventHandler(object sender, RoutedEventArgs e)
        {
                MessageBox.Show("MyEventHandler has been called");
        }

        public delegate void ChangedEventHandler(object sender, RoutedEventArgs e);

        private event ChangedEventHandler MyEvent;

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
                if (MyEvent != null)
                    MyEvent(this, e);
        }
    }
}

The XAML code is as follows:

<UserControl
... 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d"
x:Class="SilverlightApplication1.MainPage" MouseLeftButtonDown="UserControl_MouseLeftButtonDown">

    <Grid x:Name="LayoutRoot" Background="White">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MyEvent">
                <ei:GoToStateAction StateName="Highlighted"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        ...
    </Grid>
</UserControl>

Current, the message box containing "MouseLeftButtonDown" is getting displayed but the animation is not getting called. The animation did get called when the EventTrigger EventName was MouseLeftButtonDown instead of MyEvent. Please help me out. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

三生池水覆流年 2024-12-08 13:40:46

如果你想触发某个控件的事件,你可以在EventTrigger中设置SourceName。
我想,在你的情况下,在代码后面:

VisualStateManager.GoToState(this [or some other object with Highlighted state], "Highlighted", false);

You can set SourceName in EventTrigger if you want to trigger on event of some control.
I guess, in your case in code behind:

VisualStateManager.GoToState(this [or some other object with Highlighted state], "Highlighted", false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文