XAML Windows Phone 8.1 按钮应更改背景图像

XAML Windows Phone 8.1 Button should change backgroundImage

我知道很多人问过这种问题。但是我自己解决不了。

要实现的挑战是一个简单的按钮,除了为多种状态(默认、按下和悬停)更改背景图像外,没有任何样式。

到目前为止我完成的代码在我的 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 代码似乎什么都不做,因为根本没有图像..

干杯,

克里斯

看起来需要一个初始斜杠来准确定位图像的位置,因为图像不是相对于当前xaml路径

改变

Source="Assets/ActionsIcons/like-action-on-pressed.png"

到包的 根目录

Source="/Assets/ActionsIcons/like-action-on-pressed.png"

这些看起来不像是动态数据绑定图像。动态图像需要 ms-appx:/// 因为它们没有在 xaml 中声明并且需要特殊的命名法才能正确定位。

我相信添加 ms-appx:/// 的建议是有效的,因为它提供了一个可以解析的正确路径,但如果只是将 / 添加到路径中,它就是多余的。


为了更好地理解阅读 How to load file resources (XAML) (Windows)