[英]“Ensure Event Failed” error occur
<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>
在上面的程序中,(在自定义控件库中的我锻炼的程序中)我试图更改控件textblock => button。(textblock充当按钮功能),因此我尝试向该textblock添加一个事件,它给出错误消息“确保事件失败”,并且此文件名为“ Generic.xaml”,因此我添加了一个类“ Generic.xaml.cs”,但仍显示相同的错误。 在此先感谢您解释为什么会发生这种情况以及如何解决它。
您需要添加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>
至于Generic.xaml.cs
:
namespace CustomCalc
{
public partial class Generic : ResourceDictionary
{
private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}
}
}
另外,不要忘记将您的ResourceDictionary
合并到App.Xaml
文件中:
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/CustomCalc;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在我使用Visual Studio 2015和WPF项目的情况下,Visual Studio重新启动后错误消失了。
在我的情况下,我尝试创建事件时,XAML文件(具体来说是App.xaml)在一个共享项目中,并且由于该线程的错误主题而停留了几分钟。
我的解决方案是从共享项目中排除XAML文件及其XAML.cs文件,然后在每个从属平台项目中链接(而不是复制!)它。
有关链接过程的图形可视化,请参见以下GIF: 在项目之间链接文件
资料来源: http : //geertvanhorrik.com/2015/04/01/fix-for-wpf-images-and-shared-projects/
我的问题已通过关闭并重新打开导致错误的xaml
文档得到解决(Visual Studio 2017)。 我的xaml
也处于共享项目中。
如果需要,重新启动Visual Studio也对我有所帮助。
当前在VS 2017 Enterprise中运行Xamarin Forms并存在此问题。
就我而言,它仅在我尝试将事件处理程序添加到新定义的xaml Button时发生。 取消调试并再次尝试解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.