C# XamlParseException,Set connectionId引发异常
当我在Xaml下运行时,出现错误: System.Windows.Markup.XamlParseException:“”Set connectionId引发了 例外。“行号“18”和行位置“14” 内部异常1:InvalidCastException:无法转换 'System.Windows.Style'object to'System.Windows.Controls.TreeView' 类型 请不要将此问题标记为重复,因为我在中检查了所有类似的问题,但找不到有效的答案C# XamlParseException,Set connectionId引发异常,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,当我在Xaml下运行时,出现错误: System.Windows.Markup.XamlParseException:“”Set connectionId引发了 例外。“行号“18”和行位置“14” 内部异常1:InvalidCastException:无法转换 'System.Windows.Style'object to'System.Windows.Controls.TreeView' 类型 请不要将此问题标记为重复,因为我在中检查了所有类似的问题,但找不到有效的答案 <Window
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<DataTemplate x:Key="xxx">
<Grid>
<local:ButtonEx ToolTipService.ToolTipClosing="ButtonEx_ToolTipClosing"/>
</Grid>
</DataTemplate>
<Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem" >
<EventSetter Event="MouseDoubleClick" Handler="TreeViewItem_MouseDoubleClick"/>
</Style>
</Window.Resources>
<Grid>
<TreeView x:Name="treeViewBookmarks" ItemContainerStyle="{StaticResource TreeViewItemStyle}"/>
</Grid>
如果删除:ToolTipService.ToolTipClosing=“ButtonEx\u ToolTipClosing”
,或删除:EventSetter Event=“MouseDoubleClick”Handler=“TreeViewItem\u MouseDoubleClick”
,或删除x:Name=“treeViewBookmarks”
,则无错误
如果我使用
按钮
而不是按钮nex
-没有错误。这是个好问题
看起来这是WPFPresentationBuildTasks
组件中的一个bug
正如您所知,构建WPF项目会导致大量编译器生成的代码被注入到UI类中
例如,您的main窗口
类将另外获得如下内容:
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent()
{
if (!_contentLoaded)
{
_contentLoaded = true;
Uri resourceLocater = new Uri("/WpfApp1;component/mainwindow.xaml", UriKind.Relative);
Application.LoadComponent(this, resourceLocater);
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IComponentConnector.Connect(int connectionId, object target)
{
if (connectionId == 2)
{
treeViewBookmarks = (TreeView)target;
}
else
{
_contentLoaded = true;
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IStyleConnector.Connect(int connectionId, object target)
{
if (connectionId == 1)
{
EventSetter eventSetter = new EventSetter();
eventSetter.Event = Control.MouseDoubleClickEvent;
eventSetter.Handler = new MouseButtonEventHandler(TreeViewItem_MouseDoubleClick);
((Style)target).Setters.Add(eventSetter);
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IComponentConnector.Connect(int connectionId, object target)
{
if (connectionId == 3)
{
treeViewBookmarks = (TreeView)target;
}
else
{
_contentLoaded = true;
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IStyleConnector.Connect(int connectionId, object target)
{
switch (connectionId)
{
case 1:
((Button)target).AddHandler(ToolTipService.ToolTipClosingEvent, new ToolTipEventHandler(ButtonEx_ToolTipClosing));
break;
case 2:
{
EventSetter eventSetter = new EventSetter();
eventSetter.Event = Control.MouseDoubleClickEvent;
eventSetter.Handler = new MouseButtonEventHandler(TreeViewItem_MouseDoubleClick);
((Style)target).Setters.Add(eventSetter);
break;
}
}
}
那些Connect
方法生成不正确
如果将数据模板
中的按钮
替换为按钮
,这些连接
方法将如下所示:
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent()
{
if (!_contentLoaded)
{
_contentLoaded = true;
Uri resourceLocater = new Uri("/WpfApp1;component/mainwindow.xaml", UriKind.Relative);
Application.LoadComponent(this, resourceLocater);
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IComponentConnector.Connect(int connectionId, object target)
{
if (connectionId == 2)
{
treeViewBookmarks = (TreeView)target;
}
else
{
_contentLoaded = true;
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IStyleConnector.Connect(int connectionId, object target)
{
if (connectionId == 1)
{
EventSetter eventSetter = new EventSetter();
eventSetter.Event = Control.MouseDoubleClickEvent;
eventSetter.Handler = new MouseButtonEventHandler(TreeViewItem_MouseDoubleClick);
((Style)target).Setters.Add(eventSetter);
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IComponentConnector.Connect(int connectionId, object target)
{
if (connectionId == 3)
{
treeViewBookmarks = (TreeView)target;
}
else
{
_contentLoaded = true;
}
}
[DebuggerNonUserCode]
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
[EditorBrowsable(EditorBrowsableState.Never)]
void IStyleConnector.Connect(int connectionId, object target)
{
switch (connectionId)
{
case 1:
((Button)target).AddHandler(ToolTipService.ToolTipClosingEvent, new ToolTipEventHandler(ButtonEx_ToolTipClosing));
break;
case 2:
{
EventSetter eventSetter = new EventSetter();
eventSetter.Event = Control.MouseDoubleClickEvent;
eventSetter.Handler = new MouseButtonEventHandler(TreeViewItem_MouseDoubleClick);
((Style)target).Setters.Add(eventSetter);
break;
}
}
}
请参见,连接ID
不同
您应该向Microsoft报告此问题。您案例中的
PresentationBuildTasks
出现问题。我可以在哪里报告此问题?