事件'foo'不是RoutedEvent [英] The event 'foo' is not a RoutedEvent
问题描述
我尝试在现有项目的用户控件中创建路由事件,但收到错误消息The event 'foo' is not a RoutedEvent.
I tried making a Routed Event in a user control of an existing project and I'm getting the error message The event 'foo' is not a RoutedEvent.
为确保我没有发疯,我使用了 just 令人反感的代码(最小,完整且可验证的示例" ).
To make sure I wasn't crazy, I made an example project with just the offending code (a Minimal, Complete, and Verifiable example").
在我的示例项目中,只有我没有收到任何错误消息. 但这是完全相同的代码!
Only I don't get any error message in my example project. But it's the exact same code!
即使出现错误消息,项目也可以编译,并且RoutedEvent可以正常工作!
Even with the error message, the project compiles and the RoutedEvent works!
我尝试清理项目,重建,重新启动VS和我的PC,但是无论我做什么,此错误消息仍然存在.
I tried cleaning the project, rebuilding, restarting VS and my PC, but no matter what I do this error message persists.
我相信这只是一个IDE问题.
I beleive this is just an IDE issue.
我正在使用Microsoft Visual Studio Professional 2015版本14.0.25431.01更新3
I'm using Microsoft Visual Studio Professional 2015 Version 14.0.25431.01 Update 3
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也会显示错误
This is an IDE bug.
Possible duplicate: Visual Studio displaying errors even if projects build
我从此答案中得到了提示,并确保在清理解决方案和构建之间退出VS./p>
I took a hint from this answer and made sure to exit VS in-between cleaning the solution and building.
- 删除所有obj,bin,release和debug文件夹
- 删除
Project_Folder\.vs\Project_Name\v14\.suo
- 在Visual Studio中打开解决方案
- 清洁溶液
- 退出Visual Studio
- 再次打开解决方案
- 构建,构建,构建...
- 确认它已经固定,可以去喝咖啡
- 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
这篇关于事件'foo'不是RoutedEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!