wpf RoutedEvent“成员无法识别或无法访问”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21033509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RoutedEvent "the member is not recognized or is not accessible"
提问by Scott Solmer
Rather than use an event generated by an input device, I want to use a custom event that will be raised programatically in the code-behind as an EventTrigger in my xaml.
This should be laughably easy but I can't find an example anywhere.
我不想使用由输入设备生成的事件,而是想使用将在代码隐藏中以编程方式引发的自定义事件作为我的 xaml 中的 EventTrigger。
这应该很容易,但我在任何地方都找不到示例。
Here's what I've come up with from studying WPF4 Unleashed Chapter 6, Routed Event Implementation, EventTrigger.RoutedEvent Property, Custom RoutedEvent as EventTrigger, and many others:
这是我在学习 WPF4 Unleashed 第 6 章,路由事件实现,EventTrigger.RoutedEvent Property,Custom RoutedEvent as EventTrigger以及许多其他内容中得出的结论:
MainWindow.xaml.cs:
主窗口.xaml.cs:
namespace RoutedEventTrigger
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
RaiseEvent(new RoutedEventArgs(fooEvent, this));
}
public static readonly RoutedEvent fooEvent = EventManager.RegisterRoutedEvent(
"foo", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MainWindow));
// Provide CLR accessors for the event
public event RoutedEventHandler foo
{
add { AddHandler(fooEvent, value); }
remove { RemoveHandler(fooEvent, value); }
}
}
}
MainWindow.xaml:
主窗口.xaml:


P.S. Please be gentle, I am relatively new to WPF.
PS 请温柔点,我对 WPF 比较陌生。
回答by Nico
Okuma Scott,
大熊斯科特,
Have you tried to build (rebuild) the project. WPF requires you to build the project in order for the project changes to be visible to the XAML parser. Using the code below builds just fine.
您是否尝试过构建(重建)项目。WPF 要求您构建项目,以便 XAML 解析器可以看到项目更改。使用下面的代码构建就好了。
Code
代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static readonly RoutedEvent fooEvent = EventManager.RegisterRoutedEvent("foo", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MainWindow));
// Provide CLR accessors for the event
public event RoutedEventHandler foo
{
add { AddHandler(fooEvent, value); }
remove { RemoveHandler(fooEvent, value); }
}
}
XAML.
XAML。
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Window.Triggers>
<EventTrigger RoutedEvent="local:MainWindow.foo" />
</Window.Triggers>
</Window>
Edit:The same parser error was displayed until the project was re-built.
编辑:在重新构建项目之前显示相同的解析器错误。
回答by Taelia
<Window.Triggers>
<EventTrigger RoutedEvent="{x:Static local:MainWindow.foo}" />
</Window.Triggers>
I ran into the same problem, but all your solutions didn't work for me. This snippet above did solve the issue for me.
我遇到了同样的问题,但是您的所有解决方案都不适用于我。上面的这个片段确实为我解决了这个问题。
回答by Phillip
I had the same problem but rebuilding didn't work for me untill i restardet my Studio. Closed it and reopened the Projekt and it worked fine.
我遇到了同样的问题,但在我重新启动我的工作室之前,重建对我不起作用。关闭它并重新打开 Projekt,它运行良好。

