wpf 出现“确保事件失败”错误

piwo6bdm  于 2023-06-24  发布在  其他
关注(0)|答案(6)|浏览(175)
<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”但显示相同的错误。请解释为什么会出现这种情况以及如何解决它,提前感谢。

xu3bshqb

xu3bshqb1#

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

knpiaxh12#

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

k10s72fa

k10s72fa3#

在我的例子中,XAML文件(具体为App.xaml)在我试图创建事件时位于共享项目中,并在这个线程的错误主题中卡住了几分钟。
我的解决方案是从共享项目中排除XAML文件沿着XAML.cs文件,然后链接(而不是复制!))它在每个依赖平台项目中。
有关链接过程的图形可视化,请参见以下GIF:Linking files between projects
来源:http://geertvanhorrik.com/2015/04/01/fix-for-wpf-images-and-shared-projects/

ssm49v7z

ssm49v7z4#

我的问题已通过关闭并重新打开导致错误的xaml文档(Visual Studio 2017)得到解决。我的xaml也在一个共享项目中。
如果需要重新启动Visual Studio也对我有帮助。

jv2fixgn

jv2fixgn5#

目前在VS 2017 Enterprise中运行Xamarin Forms,并出现此问题。
在我的例子中,只有当我尝试向新定义的xaml按钮添加事件处理程序时,才会出现这种情况。取消调试并重试修复了此问题。

iq3niunx

iq3niunx6#

在我的例子中,我为XAML使用了不同的命名空间:
<Window x:Class="NameSpace1.MyClass"
代码文件:

namespace NameSpace2
{
    public partial class MyClass: Window
    {
        public WpfODAttributeQuery()
        {
            InitializeComponent();
        }
    }
}

只是更正了命名空间,错误就消失了。

相关问题