wpf - 事件 'foo' 不是 RoutedEvent

标签 wpf xaml visual-studio-2015 routed-events

我尝试在现有项目的用户控件中创建路由事件,但收到错误消息事件“foo”不是 RoutedEvent。

为了确保我没有发疯,我制作了一个示例项目,只是有问题的代码 ( a Minimal, Complete, and Verifiable example" )。

只有我在我的示例项目中没有收到任何错误消息。 但它是完全相同的代码!

有人知道发生了什么事吗?

即使出现错误消息,项目也会编译并且 RoutedEvent 会正常工作!

我尝试清理项目、重建、重新启动 VS 和我的 PC,但无论我做什么,此错误消息仍然存在。

我相信这只是一个 IDE 问题。

我使用的是 Microsoft Visual Studio Professional 2015 版本 14.0.25431.01 更新 3

IDE=VS2015Pro

UserControl1.xaml.cs

namespace Okuma_FFP
{
    using System.Windows;
    using System.Windows.Controls;

    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            RaiseEvent(new RoutedEventArgs(fooEvent, this));
        }

        public static readonly RoutedEvent fooEvent = EventManager.RegisterRoutedEvent(
            "foo", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(UserControl1));

        // Provide CLR accessors for the event 
        public event RoutedEventHandler foo
        {
            add { AddHandler(fooEvent, value); }
            remove { RemoveHandler(fooEvent, value); }
        }
    }
}

用户控件1.xaml

<UserControl x:Class="Okuma_FFP.UserControl1"
             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:local="clr-namespace:Okuma_FFP"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="local:UserControl1.foo">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation  
                        To="Blue"
                        Duration="0:0:5" 
                        Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>
    <Grid>  </Grid>
</UserControl>

最佳答案

这是一个 IDE 错误。
可能重复:Visual Studio displaying errors even if projects build

我从 this answer 得到了提示并确保在清理解决方案和构建之间退出 VS。

  1. Delete all obj, bin, release, and debug folders
  2. Delete Project_Folder\.vs\Project_Name\v14\.suo
  3. Open the solution in Visual Studio
  4. Clean Solution
  5. Exit Visual Studio
  6. Open the solution again
  7. Build, build, build...
  8. Confirm it's fixed and go get coffee

关于wpf - 事件 'foo' 不是 RoutedEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41730378/

相关文章:

c# - 防止文本在 Avalon 中更新编辑

visual-studio - Visual Studio 2015 更新 3 (KB3165756) 安装程序被阻止

visual-studio-2015 - 如何使用project.json引用另一个文件夹中的本地csproj?

wpf - 如何在 WPF 应用程序中嵌入 nancy

c# - WPF、SQLite 和延迟加载

c# - WPF按钮单击事件不起作用

c# - Windows 通用应用程序实时加速度计数据图表

c# - 如何在 WPF LiveCharts 中添加图表标题

xaml - Silverlight 4.0表单初始化问题

c# - 错误 - 使用 sdkToolsPath 找不到 al.exe