【问题标题】:System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first message on windows 7 machineSystem.InvalidOperationException:指定元素已经是另一个元素的逻辑子元素。在 Windows 7 机器上断开第一条消息
【发布时间】:2014-10-09 09:35:36
【问题描述】:

我有一个 WPF 应用程序在我的 Windows 8 操作系统开发机器上运行,没有任何问题。但是当我尝试在 Windows 7 机器上运行相同的程序时,我收到了错误

System.InvalidOperationException:指定元素已经是另一个元素的逻辑子元素。先断开连接。

详细的错误日志是

        [CDATA[Set property 'System.Windows.FrameworkElement.Style' threw an exception.
            LoadBaml at offset 481 in file:line:column <filename unknown>:0:0

            System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first.
               at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
               at System.Windows.FrameworkElement.AddLogicalChild(Object child)
               at System.Windows.Controls.ContentControl.OnContentChanged(Object oldContent, Object newContent)
               at System.Windows.Controls.ContentControl.OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
               at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
               at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
               at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
               at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
               at System.Windows.StyleHelper.ApplyStyleOrTemplateValue(FrameworkObject fo, DependencyProperty dp)
               at System.Windows.StyleHelper.InvalidateContainerDependents(DependencyObject container, FrugalStructList`1& exclusionContainerDependents, FrugalStructList`1& oldContainerDependents, FrugalStructList`1& newContainerDependents)
               at System.Windows.StyleHelper.DoStyleInvalidations(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle)
               at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
               at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
               at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
               at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
               at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
               at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
               at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
               at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
               at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
               at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
               at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)]]

我不知道是哪个元素 XAML 或背后的代码导致了这个问题。任何帮助找出这个奇怪问题的原因?

终于能找到出错的原因了 我在应用程序中有一个 ToggleButton 样式,它像这样在 App.xaml 中定义

  <Style x:Key="ONStateLabelLib" TargetType="Label">
        <Setter Property="Background" Value="#A1A1A1"></Setter>
        <Setter Property="Foreground" Value="Black"></Setter>

        <Setter Property="Margin" Value="0"></Setter>
        <Setter Property="Padding" Value="0"></Setter>

        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Width" Value="45"></Setter>


        <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
        <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>

        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontFamily" Value="Arial"/>
    </Style>
    <Style x:Key="OFFStateLabelLib" TargetType="Label">

        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Margin" Value="0"></Setter>
        <Setter Property="Padding" Value="0"></Setter>

        <Setter Property="FontSize" Value="14"/>
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontWeight" Value="ExtraLight"/>

        <Setter Property="Foreground" Value="#515151"></Setter>
        <Setter Property="Background" Value="#393939"></Setter>


        <Setter Property="Width" Value="45"></Setter>

        <Setter Property="BorderBrush" Value="#A1A1A1"></Setter>
        <Setter Property="BorderThickness" Value="2"></Setter>


        <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
        <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
    </Style>

    <Style x:Key="OnOffToggleStyleLib" TargetType="ToggleButton" >
        <Setter Property="VerticalAlignment" Value="Center"></Setter>
        <Setter Property="Width" Value="90"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <ContentPresenter  VerticalAlignment="Center" />
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Transparent">
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Content">
                    <Setter.Value>
                        <DockPanel Margin="0">
                            <Label DockPanel.Dock="Left" Style="{StaticResource ResourceKey=ONStateLabelLib}" >YES</Label>
                            <Label Style="{StaticResource ResourceKey=OFFStateLabelLib}" >NO</Label>
                        </DockPanel>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="Content">
                    <Setter.Value>
                        <DockPanel Margin="0">
                            <Label DockPanel.Dock="Left" Style="{StaticResource ResourceKey=OFFStateLabelLib}">YES</Label>
                            <Label Style="{StaticResource ResourceKey=ONStateLabelLib}" >NO</Label>
                        </DockPanel>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

我在 2 页 Page1.xaml 和 Page2.xaml 中使用切换按钮,就像这样

  <ToggleButton Margin="5" Grid.Row="0" 
     ToolTip="{Binding Path=localResource.reader_tooltip_settings_fullscreen,Source={StaticResource      DbookLanguageManagerDynamic}}"  Grid.Column="1"
     Name="btn_fullScreen" Click="btn_fullScreen_Clicked"
      Style="{StaticResource ResourceKey=OnOffToggleStyleLib}" />   

应用程序首先加载 Page1.xaml 并在导航到 Page2.xaml 时遇到异常。

现在有人可以帮忙解决这个问题吗?

【问题讨论】:

  • 在我看来,您正在设置一个属性,该属性在您的应用程序某处的样式中保存控件。您可以尝试设置ContentTemplate,以防它是导致此错误的Content 属性。至于问题的根源,该错误根本没有提供足够的信息让我们知道它的来源,因为它是一个编译的 XAML。无法在 W7 机器上调试?
  • 在开发机器以外的机器上,确保你已经安装了所有的先决条件。此外,在该机器上安装 Visual Studio。然后尝试在该机器上运行您的 exe。现在,如果您遇到任何错误,那么您可以选择使用 Visual Studio 进行调试,我认为您可能会在很短的时间内发现该错误。祝你好运…………
  • 是的,我能够在 Windows 7 机器上进行调试。但没有更多信息可用,例如哪个 xaml 导致它或错误行等
  • @Vishal 在客户端部署项目时要求客户安装 Visual Studio 是否是一种好方法?
  • 不,这不是要求客户安装 Visual Studio 的好方法。但这应该是您的测试期,并在您的安装文件中提供一些程序集(dll),这样客户就不需要安装 Visual Studio。我不是任何专家,但您仍然应该等待其他回复。

标签: .net wpf xaml windows-runtime


【解决方案1】:

尝试将Content 属性设置器更改为ContentTemplate,并使用DataTemplate 作为值:

<Style
    x:Key="OnOffToggleStyleLib"
    TargetType="ToggleButton">
    <Setter
        Property="VerticalAlignment"
        Value="Center"></Setter>
    <Setter
        Property="Width"
        Value="90"></Setter>
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="ToggleButton">
                <ContentPresenter
                    VerticalAlignment="Center" />
                <ControlTemplate.Triggers>
                    <Trigger
                        Property="IsMouseOver"
                        Value="True">
                        <Setter
                            Property="Background"
                            Value="Transparent">
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger
            Property="IsChecked"
            Value="True">
            <Setter
                Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel
                            Orientation="Horizontal">
                            <Label
                                DockPanel.Dock="Left"
                                Style="{StaticResource ResourceKey=ONStateLabelLib}">YES</Label>
                            <Label
                                Style="{StaticResource ResourceKey=OFFStateLabelLib}">NO</Label>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger
            Property="IsChecked"
            Value="False">
            <Setter
                Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel
                            Orientation="Horizontal">
                            <Label
                                DockPanel.Dock="Left"
                                Style="{StaticResource ResourceKey=OFFStateLabelLib}">YES</Label>
                            <Label
                                Style="{StaticResource ResourceKey=ONStateLabelLib}">NO</Label>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

我还将DockPanel 换成了StackPanel,在我看来这是一个更好的方法。让我知道它是否有效:)

【讨论】:

  • 是的,它有效。但是我只是想知道更多关于为什么它只对几个版本的 Windows 失败?你能帮忙解释一下错误的原因吗??
  • 这也是我很想知道的事情......我前段时间也遇到过同样的问题,但是应用程序没有抛出异常(就像我在网上阅读的那样),而是拒绝显示控件的内容.不幸的是我只能给你一个解决方案,也许其他人可以告诉我们问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2012-02-20
  • 2012-08-23
  • 2012-11-17
  • 2011-04-30
  • 1970-01-01
相关资源
最近更新 更多