You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WPF UserControl中定义Path触发XDG0012错误:Loaded成员未识别

问题:WPF UserControl中Path的Loaded路由事件未被识别

原本在dictionary.xaml中定义Path时出现编译错误,为实现复用,按@Clemens建议将其移入WPF UserControl后,仍遇错误:路由事件Loaded未被识别。

对应的XAML代码(注:原代码中d命名空间存在拼写错误,已修正):

<UserControl x:Class="my.UI.UC.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200">
    <Grid>
        <Path x:Name="SpinnerPath" Data="M100,10 A90,90 0 0 1 190,100"
              Width="24" Height="24"
              Stroke="Green" StrokeThickness="20"
              StrokeStartLineCap="Round" StrokeEndLineCap="Round"
              RenderTransformOrigin="0.5,0.5">
            <Path.RenderTransform>
                <RotateTransform />
            </Path.RenderTransform>
            <Path.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard TargetProperty="RenderTransform.Angle">
                            <DoubleAnimation By="360" 
                                             Duration="0:0:1"
                                             RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Path.Triggers>
        </Path>
    </Grid>
</UserControl>

编译器抛出错误:

XDG0012 The member "Loaded" is not recognized or not accessible


解答

这个问题并非命名空间缺失导致,核心原因是:Path控件的Triggers集合仅支持绑定控件的默认路由事件(如MouseEnter、MouseLeave这类),而Loaded是UIElement的通用路由事件,无法直接在Path.Triggers中使用。

解决方法1:将EventTrigger移到UserControl的Triggers中

UserControl的Triggers支持Loaded事件,调整代码如下:

<UserControl x:Class="my.UI.UC.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200">
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard TargetName="SpinnerPath" TargetProperty="RenderTransform.Angle">
                    <DoubleAnimation By="360" 
                                     Duration="0:0:1"
                                     RepeatBehavior="Forever"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>
    <Grid>
        <Path x:Name="SpinnerPath" Data="M100,10 A90,90 0 0 1 190,100"
              Width="24" Height="24"
              Stroke="Green" StrokeThickness="20"
              StrokeStartLineCap="Round" StrokeEndLineCap="Round"
              RenderTransformOrigin="0.5,0.5">
            <Path.RenderTransform>
                <RotateTransform />
            </Path.RenderTransform>
        </Path>
    </Grid>
</UserControl>

解决方法2:使用行为库绑定Loaded事件

通过Microsoft.Xaml.Behaviors.Wpf NuGet包提供的行为系统来绑定事件:

  1. 先安装NuGet包:Microsoft.Xaml.Behaviors.Wpf
  2. 修改XAML代码,添加命名空间并使用行为:
<UserControl x:Class="my.UI.UC.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200">
    <Grid>
        <Path x:Name="SpinnerPath" Data="M100,10 A90,90 0 0 1 190,100"
              Width="24" Height="24"
              Stroke="Green" StrokeThickness="20"
              StrokeStartLineCap="Round" StrokeEndLineCap="Round"
              RenderTransformOrigin="0.5,0.5">
            <Path.RenderTransform>
                <RotateTransform />
            </Path.RenderTransform>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <i:BeginStoryboard>
                        <Storyboard TargetProperty="RenderTransform.Angle">
                            <DoubleAnimation By="360" 
                                             Duration="0:0:1"
                                             RepeatBehavior="Forever"/>
                        </Storyboard>
                    </i:BeginStoryboard>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Path>
    </Grid>
</UserControl>

内容的提问来源于stack exchange,提问作者Willy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 07:35:36