【问题标题】:"Ensure Event Failed" error occur发生“确保事件失败”错误
【发布时间】:2015-10-13 05:14:33
【问题描述】:
<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>

在上面的程序中,(在我在自定义控件库中锻炼的程序中) 我正在尝试更改控件文本块 => 按钮。(文本块充当按钮功能) 所以我尝试向文本块添加一个事件,它会给出一条错误消息“确保事件失败”, 这个文件名是“Generic.xaml”,所以我添加了一个类“Generic.xaml.cs”,但同样的错误显示在上面。 请解释为什么会发生它以及如何解决它,在此先感谢。

【问题讨论】:

    标签: c# wpf custom-controls


    【解决方案1】:

    您需要添加 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>
    

    【讨论】:

    • 我遇到了类似的问题,但我定义了x:Class。原来我命名它的方式是错误的。约定是x:Class="NAMESPACE.CLASSNAME"我在重命名文件后从未更改过名称。
    【解决方案2】:

    在我使用 Visual Studio 2015 和 WPF 项目的情况下,Visual Studio 重新启动后错误消失了。

    【讨论】:

      【解决方案3】:

      在我的情况下,XAML 文件(具体来说是 App.xaml)在我尝试创建事件时位于共享项目中,并被此线程的错误主题卡住了几分钟。

      我的解决方案是从共享项目中排除 XAML 文件及其 XAML.cs 文件,然后在每个相关平台项目中链接(而不是复制!)它。

      有关链接过程的图形可视化,请参见以下 GIF: Linking files between projects

      来源: http://geertvanhorrik.com/2015/04/01/fix-for-wpf-images-and-shared-projects/

      【讨论】:

        【解决方案4】:

        通过关闭并重新打开导致错误的 xaml 文档(Visual Studio 2017)解决了我的问题。我的xaml 也在一个共享项目中。

        如果需要重新启动 Visual Studio 对我也有帮助。

        【讨论】:

          【解决方案5】:

          目前在 VS 2017 Enterprise 中运行 Xamarin Forms 并遇到此问题。

          在我的情况下,它仅在我尝试将事件处理程序添加到新定义的 xaml 按钮时发生。取消调试并重试解决此问题。

          【讨论】:

            猜你喜欢
            • 2015-07-01
            • 1970-01-01
            • 2022-08-17
            • 1970-01-01
            • 2014-07-22
            • 2018-07-09
            • 1970-01-01
            • 1970-01-01
            • 2019-05-21
            相关资源
            最近更新 更多