【发布时间】:2016-04-04 21:38:05
【问题描述】:
我知道这种问题被问了很多。但是我自己解决不了。
要实现的挑战是一个没有任何样式的简单按钮,除了用于几种状态(默认、按下和悬停)的不断变化的背景图像。
到目前为止我所做的代码在我的 App.xaml 文件中:
<Style x:Key="likeActionButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
</VisualState>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="HoverBackground"
Storyboard.TargetProperty = "Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="PressedBackground"
Storyboard.TargetProperty = "Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border>
<Grid>
<Image Source="Assets/ActionsIcons/like-action.png"></Image>
<Image x:Name="HoverBackground" Source="Assets/ActionsIcons/like-action-onHover.png" Visibility="Collapsed"></Image>
<Image x:Name="PressedBackground" Source="Assets/ActionsIcons/like-action-on-pressed.png" Visibility="Collapsed"></Image>
</Grid>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
[从某处调用按钮样式]
<Button Style="{StaticResource likeActionButton}"/>
所以这个 XAML 代码似乎什么都不做,因为根本没有图像..
干杯,
克里斯
【问题讨论】:
-
在有按钮的页面上是否找到按钮样式不清楚。我建议您将其移出 app.xaml 并将其与该页面上的按钮放在一起。还可以在调试模式下运行程序并监视输出窗口...您可能会看到绑定问题或其他 xaml 故障项。
-
你给源的路径是错误的,添加 ms-appx:/// 到路径 Source="ms-appx:///Assets/ActionsIcons/like-action.png"
-
@OmegaMan 输出窗口不显示任何错误或警告。并且将 XAML 代码移动到当前页面也无法正常工作。 @Archana我真的认为可能是这样,但按钮仍然不存在。我试图添加一个背景属性以使按钮至少以某种方式可见。我不明白为什么这也不起作用? ` `
-
好的。我设法使按钮可见。源的正确路径是:ms-appx:///Assets\ActionIcons\like-action.png。为什么必须是反斜杠?我不明白。但是,hover 和 pressed 的不同状态仍然不起作用