简体   繁体   中英

“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. 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. So your Generic.xaml should look like this:

<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 :

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:

<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.

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.

See the following GIF for a graphic visualization of the linking process: Linking files between projects

Source: 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). My xaml is also in a shared project.

If needed a restart to Visual Studio has helped me also.

Currently running Xamarin Forms in VS 2017 Enterprise and had this issue.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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