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

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

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

【问题讨论】:

    标签: c# windows xaml windows-phone-8 windows-phone-8.1


    【解决方案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>
    

    【讨论】:

    • 如果您明确地用图片展示它,我将非常感激。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2014-06-15
    相关资源
    最近更新 更多