简体   繁体   English

发生“确保事件失败”错误

[英]“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>

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. 在上面的程序中,(在自定义控件库中的我锻炼的程序中)我试图更改控件textblock => button。(textblock充当按钮功能),因此我尝试向该textblock添加一个事件,它给出错误消息“确保事件失败”,并且此文件名为“ Generic.xaml”,因此我添加了一个类“ Generic.xaml.cs”,但仍显示相同的错误。 Please explain why it should occur and how to resolve it, thanks in advance. 在此先感谢您解释为什么会发生这种情况以及如何解决它。

You need to add a x:Class attribute to support event handlers in the XAML file. 您需要添加x:Class属性以支持XAML文件中的事件处理程序。 So your Generic.xaml should look like this: 因此,您的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 ResourceDictionary in the App.Xaml file: 另外,不要忘记将您的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重新启动后错误消失了。

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/

My issue was resolved by closing and reopening the xaml document causing the error (Visual Studio 2017). 我的问题已通过关闭并重新打开导致错误的xaml文档得到解决(Visual Studio 2017)。 My xaml is also in a shared project. 我的xaml也处于共享项目中。

If needed a restart to Visual Studio has helped me also. 如果需要,重新启动Visual Studio也对我有所帮助。

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. 就我而言,它仅在我尝试将事件处理程序添加到新定义的xaml Button时发生。 Cancelling debugging and trying again fixed this. 取消调试并再次尝试解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM