[英]“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>
在上面的程序中,(在自定義控件庫中的我鍛煉的程序中)我試圖更改控件textblock => button。(textblock充當按鈕功能),因此我嘗試向該textblock添加一個事件,它給出錯誤消息“確保事件失敗”,並且此文件名為“ Generic.xaml”,因此我添加了一個類“ Generic.xaml.cs”,但仍顯示相同的錯誤。 在此先感謝您解釋為什么會發生這種情況以及如何解決它。
您需要添加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>
在我使用Visual Studio 2015和WPF項目的情況下,Visual Studio重新啟動后錯誤消失了。
在我的情況下,我嘗試創建事件時,XAML文件(具體來說是App.xaml)在一個共享項目中,並且由於該線程的錯誤主題而停留了幾分鍾。
我的解決方案是從共享項目中排除XAML文件及其XAML.cs文件,然后在每個從屬平台項目中鏈接(而不是復制!)它。
有關鏈接過程的圖形可視化,請參見以下GIF: 在項目之間鏈接文件
資料來源: http : //geertvanhorrik.com/2015/04/01/fix-for-wpf-images-and-shared-projects/
我的問題已通過關閉並重新打開導致錯誤的xaml
文檔得到解決(Visual Studio 2017)。 我的xaml
也處於共享項目中。
如果需要,重新啟動Visual Studio也對我有所幫助。
當前在VS 2017 Enterprise中運行Xamarin Forms並存在此問題。
就我而言,它僅在我嘗試將事件處理程序添加到新定義的xaml Button時發生。 取消調試並再次嘗試解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.