如何在单击并再次在 Windows Phone 中释放时更改按钮图像

Posted

技术标签:

【中文标题】如何在单击并再次在 Windows Phone 中释放时更改按钮图像【英文标题】:How to change button image on clicking and again on releasing in windows phone 【发布时间】:2016-06-19 09:24:27 【问题描述】:

我想在单击按钮时将 image1(比如说)更改为 image2,然后在释放时再次将其更改回 image1。

如果有人知道,请回答我。我是 Windows Phone 开发的新手。

【问题讨论】:

【参考方案1】:
<Button x:Name="MyButton" IsPressed="Binding MyButtonIsPressed"></Button>

C#

private bool _myButtonIsPressed;
public bool MyButtonIsPressed 
  get return _myButtonIsPressed;
  set
    _myButtonIsPressed = value;
    if(value==false)
      MyButton.Content = ImageWhenReleasing;
    
    else
      MyButton.Content = ImageWhenClicking;
    
  

希望对你有帮助。

【讨论】:

在 windows phone 中没有 mouseRelease 和 mousePressed 所以我应该用什么来代替它。 我认为你可以在 WP 中使用IsPressed 属性。我现在没有那个 SDK,但我正在用一些代码编辑帖子。【参考方案2】:

在按钮的视觉状态下更改图像的可见性。

按钮的样式:

<Style x:Key="ButtonStyle2"
           TargetType="Button">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="BorderBrush"
                Value="ThemeResource PhoneForegroundBrush" />
        <Setter Property="Foreground"
                Value="ThemeResource PhoneForegroundBrush" />
        <Setter Property="BorderThickness"
                Value="ThemeResource PhoneBorderThickness" />
        <Setter Property="FontFamily"
                Value="ThemeResource PhoneFontFamilyNormal" />
        <Setter Property="FontWeight"
                Value="ThemeResource PhoneButtonFontWeight" />
        <Setter Property="FontSize"
                Value="ThemeResource TextStyleLargeFontSize" />
        <Setter Property="Padding"
                Value="ThemeResource PhoneButtonContentPadding" />
        <Setter Property="MinHeight"
                Value="ThemeResource PhoneButtonMinHeight" />
        <Setter Property="MinWidth"
                Value="ThemeResource PhoneButtonMinWidth" />
        <Setter Property="HorizontalAlignment"
                Value="Left" />
        <Setter Property="VerticalAlignment"
                Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="Grid"
                          Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="Pressed"
                                                      To="PointerOver">
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="Grid" />
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="PointerOver"
                                                      To="Normal">
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="Grid" />
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition From="Pressed"
                                                      To="Normal">
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="Grid" />
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="PointerOver" />
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <PointerDownThemeAnimation Storyboard.TargetName="Grid" />
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
                                                                       Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="ThemeResource ButtonPressedForegroundThemeBrush" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                       Storyboard.TargetName="image1">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                       Storyboard.TargetName="image2">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground"
                                                                       Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="ThemeResource ButtonDisabledForegroundThemeBrush" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush"
                                                                       Storyboard.TargetName="Border">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="ThemeResource ButtonDisabledBorderThemeBrush" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background"
                                                                       Storyboard.TargetName="Border">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="ThemeResource ButtonDisabledBackgroundThemeBrush" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Border"
                                BorderBrush="TemplateBinding BorderBrush"
                                BorderThickness="TemplateBinding BorderThickness"
                                Background="TemplateBinding Background"
                                Margin="ThemeResource PhoneTouchTargetOverhang">
                            <Grid>

                                <BitmapIcon x:Name="image2"
                                            UriSource="/Assets/Square71x71Logo.scale-240.png"
                                            Foreground="Red"
                                            Height="130" Visibility="Collapsed" />
                                <BitmapIcon x:Name="image1"
                                            UriSource="/Assets/Square71x71Logo.scale-240.png"
                                            Foreground="Yellow"
                                            Height="150" />

                                <ContentPresenter x:Name="ContentPresenter"
                                              AutomationProperties.AccessibilityView="Raw"
                                              ContentTemplate="TemplateBinding ContentTemplate"
                                              Content="TemplateBinding Content"
                                              Foreground="TemplateBinding Foreground"
                                              HorizontalAlignment="TemplateBinding HorizontalContentAlignment"
                                              Margin="TemplateBinding Padding"
                                              VerticalAlignment="TemplateBinding VerticalContentAlignment" />
                            </Grid>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【讨论】:

如果您明确地用图片展示它,我将非常感激。谢谢

以上是关于如何在单击并再次在 Windows Phone 中释放时更改按钮图像的主要内容,如果未能解决你的问题,请参考以下文章

如何填充文本框-Windows Phone 8.1

如何更改 windows phone 应用程序中按钮的背景颜色?

Sencha Touch - Windows Phone 8 Phonegap 应用程序栏不隐藏在后退按钮单击

windows phone 8.1 中的独立存储

对 Windows Phone 中的 WCF 类的引用

如何在 Windows 中取消“系统键关闭”状态