【问题标题】:XamlParseException, Set connectionId threw an exceptionXamlParseException,设置 connectionId 引发异常
【发布时间】:2019-09-21 18:54:33
【问题描述】:

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

System.Windows.Markup.XamlParseException: ''Set connectionId throw an 例外。'行号“18”和行位置“14”。'

内部异常 1:InvalidCastException:无法转换 'System.Windows.Style' 对象到 'System.Windows.Controls.TreeView' 输入。

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

<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>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void ButtonEx_ToolTipClosing(object sender, ToolTipEventArgs e)
    {
    }

    private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
    }
}

class ButtonEx : Button
{
}

如果我删除:ToolTipService.ToolTipClosing="ButtonEx_ToolTipClosing",或者删除:EventSetter Event="MouseDoubleClick" Handler="TreeViewItem_MouseDoubleClick",或者删除x:Name="treeViewBookmarks" - 没有错误。

如果我使用Button 而不是ButtonEx - 没有错误。

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    这是个好问题。

    看起来这是 WPF PresentationBuildTasks 组件中的一个错误。

    如您所知,构建 WPF 项目会导致将大量编译器生成的代码注入您的 UI 类中。

    例如您的 MainWindow 课程还将获得类似以下内容:

    [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);
        }
    }
    

    那些Connect方法生成不正确。

    如果将DataTemplate 中的ButtonEx 替换为Button,则Connect 方法将如下所示:

    [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;
        }
        }
    }
    

    看,connectionId 不同。

    您应该将此问题报告给 Microsoft。对于您的情况,PresentationBuildTasks 出了点问题。

    【讨论】:

    • 我在哪里可以报告这个问题?
    猜你喜欢
    • 2019-01-26
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多