wpf 发生“确保事件失败”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33094622/
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
"Ensure Event Failed" error occur
提问by MVK
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomCalc">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<TextBlock Text="welcome" Height="50" Width="150" MouseDown=""/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
In the above program, (in the program i workout in custom control library) I am trying to change the control textblock => button.(textblock act as button functionality) so i try to add a event to the textblock it gives a error message "ensure event failed", And this file name is "Generic.xaml" so i add a class "Generic.xaml.cs" but same error is show on. Please explain why it should occur and how to resolve it, thanks in advance.
在上面的程序中,(在我在自定义控件库中锻炼的程序中)我试图更改控件文本块 => 按钮。(文本块充当按钮功能)所以我尝试向文本块添加一个事件,它给出了一条错误消息“确保事件失败”,并且此文件名为“Generic.xaml”,因此我添加了一个类“Generic.xaml.cs”,但显示相同的错误。请解释它为什么会发生以及如何解决它,提前致谢。
回答by Bahman_Aries
You need to add a x:Classattribute to support event handlers in the XAML file. So your Generic.xamlshould look like this:
您需要添加一个x:Class属性以支持 XAML 文件中的事件处理程序。所以你Generic.xaml应该是这样的:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomCalc"
x:Class="CustomCalc.Generic">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<TextBlock Text="welcome" Height="50" Width="150" MouseDown="TextBlock_MouseDown"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
And as for the Generic.xaml.cs:
至于Generic.xaml.cs:
namespace CustomCalc
{
public partial class Generic : ResourceDictionary
{
private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
}
}
Also don't forget to merge your ResourceDictionaryin the App.Xamlfile:
也不要忘记ResourceDictionary在App.Xaml文件中合并你的:
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CustomCalc;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
回答by myroslav
In my case with Visual Studio 2015and a WPF project, the error went away after the Visual Studio restart.
在我使用Visual Studio 2015和 WPF 项目的情况下,Visual Studio 重新启动后错误消失了。
回答by HoudWarZ
In my case the XAML file (App.xaml to be specific) was in a Shared Project at the time I tried to create the event and got stuck for a few minutes with the error subject of this thread.
在我的情况下,XAML 文件(具体而言是 App.xaml)在我尝试创建事件时位于共享项目中,并因该线程的错误主题而卡住了几分钟。
My solution was to exclude the XAML file along with its XAML.cs file from the Shared Project then linking (not copying !) it in each dependent platform project.
我的解决方案是从共享项目中排除 XAML 文件及其 XAML.cs 文件,然后在每个相关平台项目中链接(而不是复制!)它。
See the following GIF for a graphic visualization of the linking process: Linking files between projects
有关链接过程的图形可视化,请参阅以下 GIF: 链接项目之间的文件
Source: http://geertvanhorrik.com/2015/04/01/fix-for-wpf-images-and-shared-projects/
来源:http: //geertvanhorrik.com/2015/04/01/fix-for-wpf-images-and-shared-projects/
回答by visc
My issue was resolved by closing and reopening the xamldocument causing the error (Visual Studio 2017). My xamlis also in a shared project.
我的问题已通过关闭并重新打开xaml导致错误的文档得到解决(Visual Studio 2017)。我xaml的也在一个共享项目中。
If needed a restart to Visual Studio has helped me also.
如果需要,重新启动 Visual Studio 对我也有帮助。
回答by Charley M
Currently running Xamarin Forms in VS 2017 Enterprise and had this issue.
当前在 VS 2017 Enterprise 中运行 Xamarin Forms 并遇到此问题。
In my case it only occurred when I tried to add an Event Handler to a newly defined xaml Button. Cancelling debugging and trying again fixed this.
就我而言,它仅在我尝试将事件处理程序添加到新定义的 xaml 按钮时发生。取消调试并重试解决了这个问题。

