Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XamlParseException,Set connectionId引发异常_C#_.net_Wpf_Xaml - Fatal编程技术网

C# XamlParseException,Set connectionId引发异常

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

当我在Xaml下运行时,出现错误:

System.Windows.Markup.XamlParseException:“”Set connectionId引发了 例外。“行号“18”和行位置“14”

内部异常1:InvalidCastException:无法转换 'System.Windows.Style'object to'System.Windows.Controls.TreeView' 类型

请不要将此问题标记为重复,因为我在中检查了所有类似的问题,但找不到有效的答案

<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
-没有错误。

这是个好问题

看起来这是WPF
PresentationBuildTasks
组件中的一个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
出现问题。

我可以在哪里报告此问题?