事件 'foo' 不是 RoutedEvent
The event 'foo' is not a RoutedEvent
我尝试在现有项目的用户控件中创建路由事件,但收到错误消息 The event 'foo' is not a RoutedEvent.
为了确保我没有发疯,我用 只是 有问题的代码 (a Minimal, Complete, and Verifiable example") 创建了一个示例项目。
只有我在示例项目中没有收到任何错误消息。
但这是完全相同的代码!
有人知道发生了什么事吗?
即使出现错误消息,项目也会编译并且 RoutedEvent 会正常工作!
我尝试清理项目、重建、重新启动 VS 和我的 PC,但无论我做什么,此错误消息仍然存在。
我相信这只是一个 IDE 问题。
我正在使用 Microsoft Visual Studio Professional 2015 版本 14.0.25431.01 更新 3
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); }
}
}
}
UserControl1.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 in-between 清理解决方案并构建。
- Delete all obj, bin, release, and debug folders
- Delete
Project_Folder\.vs\Project_Name\v14\.suo
- Open the solution in Visual Studio
- Clean Solution
- Exit Visual Studio
- Open the solution again
- Build, build, build...
- Confirm it's fixed and go get coffee
我尝试在现有项目的用户控件中创建路由事件,但收到错误消息 The event 'foo' is not a RoutedEvent.
为了确保我没有发疯,我用 只是 有问题的代码 (a Minimal, Complete, and Verifiable example") 创建了一个示例项目。
只有我在示例项目中没有收到任何错误消息。 但这是完全相同的代码!
有人知道发生了什么事吗?
即使出现错误消息,项目也会编译并且 RoutedEvent 会正常工作!
我尝试清理项目、重建、重新启动 VS 和我的 PC,但无论我做什么,此错误消息仍然存在。
我相信这只是一个 IDE 问题。
我正在使用 Microsoft Visual Studio Professional 2015 版本 14.0.25431.01 更新 3
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); }
}
}
}
UserControl1.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 in-between 清理解决方案并构建。
- Delete all obj, bin, release, and debug folders
- Delete
Project_Folder\.vs\Project_Name\v14\.suo
- Open the solution in Visual Studio
- Clean Solution
- Exit Visual Studio
- Open the solution again
- Build, build, build...
- Confirm it's fixed and go get coffee