我的Windows 8 OS开发机器中运行的WPF应用程序没有任何问题。但是,当我尝试在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,我可以获得异常。
现在有人可以帮忙解决这个问题吗?
答案 0 :(得分:0)
尝试将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
交换,在我看来这是一种更好的方法。让我知道它是否有效:))