我在使用 XAML 设计器时遇到一些问题。
我有一个代表自定义进度条的用户控件:只是一个带有一些基于状态的颜色的旋转圆圈。 它扩展了 UserControl,我已将本地命名空间添加到 XAML 文件并将其命名为“local”。
<UserControl x:Class="MyControls.ProgressControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyControls">
在代码隐藏中,我创建了一个自定义事件,如下所示:
public static readonly RoutedEvent NotStartedEvent = EventManager.RegisterRoutedEvent("NotStartedEvent",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(ProgressControl));
// .NET wrapper
public event RoutedEventHandler NotStarted
{
add { AddHandler(NotStartedEvent, value); }
remove { RemoveHandler(NotStartedEvent, value); }
}
在我的 XAML 文件中,我有一个 EventTrigger 来连接一些动画:
<UserControl.Triggers>
<EventTrigger RoutedEvent="local:ProgressControl.NotStartedEvent">
<StopStoryboard BeginStoryboardName="rotateAnimation"/>
<BeginStoryboard Storyboard="{StaticResource notStartedStateAnimation}"/>
</EventTrigger>
</UserControl.Triggers>
但这给了我一个错误:在“UserControl”类型中找不到事件“NotStartedEvent”。
我在网上搜索过,但找不到我做错了什么,这一定是一些愚蠢的细节。例如,是因为 EventTrigger 位于 UserControl.Triggers 下吗?
我已经多次尝试清理和重建项目,效果很好。只有 XAML 设计器给我这个错误。
最佳答案
试试这个:
<UserControl.Triggers>
<EventTrigger RoutedEvent="{x:Static local:ProgressControl.NotStartedEvent}">
<StopStoryboard BeginStoryboardName="rotateAnimation"/>
<BeginStoryboard Storyboard="{StaticResource notStartedStateAnimation}"/>
</EventTrigger>
</UserControl.Triggers>
关于c# - 未在 UserControl 中找到自定义 RoutedEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10482572/