Windows phone 7 WP7将样式应用于多个按钮
我正在尝试对我所有的按钮应用一种样式。当我将我的样式分配给第二个按钮并尝试运行我的应用程序时,我收到一个错误:Windows phone 7 WP7将样式应用于多个按钮,windows-phone-7,xaml,Windows Phone 7,Xaml,我正在尝试对我所有的按钮应用一种样式。当我将我的样式分配给第二个按钮并尝试运行我的应用程序时,我收到一个错误: A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.dll A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
A first chance exception of type 'MS.Internal.WrappedException' occurred in System.Windows.dll
将我的风格应用于一个按钮,将起作用
以下是我的风格:
<phone:PhoneApplicationPage.Resources>
<Style x:Key="ButtonStyle1" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<!--Define the states for the common states. The states in a
VisualStateGroup are mutually exclusive to each other.-->
<VisualStateGroup x:Name="CommonStates">
<!--Define the VisualStates in this VistualStateGroup.-->
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ColorAnimation Duration="0" To="White" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<!--Define the states for the focus states. The states in a
VisualStateGroup are mutually exclusive to each other.-->
<VisualStateGroup x:Name="FocusStates">
<!--Define the VisualStates in this VistualStateGroup.-->
<VisualState x:Name="Focused" />
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="{TemplateBinding Background}">
<ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>
<!--The parts of the button control will be defined here.-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
这是我的按钮:
<Button Height="121" HorizontalAlignment="Left" Margin="14,61,0,0" Name="but1" VerticalAlignment="Top" Width="197" Background="#FF7D0000" BorderBrush="Black" Foreground="Red" BorderThickness="0" Padding="0" Style="{StaticResource ButtonStyle1}" />
<Button Height="121" HorizontalAlignment="Left" Margin="217,61,0,0" Name="but2" VerticalAlignment="Top" Width="200" Background="#FF7D0000" BorderBrush="Black" Foreground="Red" BorderThickness="0" Padding="0" Style="{StaticResource ButtonStyle1}" />
解决方案
我将其更改为反映“ContentContainer”,这是我要在样式中修改的对象的名称
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
如果它刚好在代码上方,它应该可以正常工作,但是如果您将任何按钮设置为禁用(
IsEnabled=false
),它将失败,因为在样式的禁用状态下,故事板正在寻找名为DisabledVisualElement
的元素来设置动画
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
但是,范围中不存在此类元素
删除此情节提要或添加具有正确名称的元素都可以解决您的问题。如果它正好位于代码上方,则应该可以正常工作,但如果您将任何按钮设置为禁用(
IsEnabled=false
),它将在样式的禁用状态下失败,故事板正在寻找名为DisabledVisualElement
的元素来制作动画
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</VisualState>
但是,范围中不存在此类元素
删除此情节提要或添加具有正确名称的元素都可以解决您的问题。谢谢您的提示。我认为DisabledVisualElement是某种“常量”。。。事实并非如此。我对它进行了修改,以反映对小费表示感谢的名称。我认为DisabledVisualElement是某种“常量”。。。事实并非如此。我修改了它以反映