<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”但显示相同的错误。请解释为什么会出现这种情况以及如何解决它,提前感谢。
6条答案
按热度按时间xu3bshqb1#
您需要添加
x:Class
属性以支持XAML文件中的事件处理程序。所以你的Generic.xaml
应该看起来像这样:关于
Generic.xaml.cs
:另外,不要忘记将
ResourceDictionary
合并到App.Xaml
文件中:knpiaxh12#
在我使用Visual Studio 2015和WPF项目的情况下,在Visual Studio重新启动后,错误消失了。
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/
ssm49v7z4#
我的问题已通过关闭并重新打开导致错误的
xaml
文档(Visual Studio 2017)得到解决。我的xaml
也在一个共享项目中。如果需要重新启动Visual Studio也对我有帮助。
jv2fixgn5#
目前在VS 2017 Enterprise中运行Xamarin Forms,并出现此问题。
在我的例子中,只有当我尝试向新定义的xaml按钮添加事件处理程序时,才会出现这种情况。取消调试并重试修复了此问题。
iq3niunx6#
在我的例子中,我为XAML使用了不同的命名空间:
<Window x:Class="NameSpace1.MyClass"
代码文件:
只是更正了命名空间,错误就消失了。