如何在VisualStateManager的XAML中创建资源样式?

时间:2019-01-28 16:11:48

标签: xaml xamarin xamarin.forms visualstatemanager

您好,我创建了一个ImagenButton,如下所示:

<ImageButton Source="articulos.png" Clicked="ImageButton_Clicked"/>

这是动画代码:

<VisualStateManager.VisualStateGroups>
     <VisualStateGroup x:Name="CommonStates">
          <VisualState x:Name="Normal">
              <VisualState.Setters>
                  <Setter Property="Scale" Value="1"/>
              </VisualState.Setters>
          </VisualState>
          <VisualState x:Name="Pressed">
              <VisualState.Setters>
                  <Setter Property="Scale" Value="0.8"/>
              </VisualState.Setters>
           </VisualState>
       </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

但是我想创建许多具有相同样式的按钮,我不想重复代码。

这是完整的代码:

<ImageButton Source="articulos.png" Clicked="ImageButton_Clicked">               
     <VisualStateManager.VisualStateGroups>
          <VisualStateGroup x:Name="CommonStates">
             <VisualState x:Name="Normal">
                 <VisualState.Setters>
                     <Setter Property="Scale" Value="1"/>
                 </VisualState.Setters>
             </VisualState>
             <VisualState x:Name="Pressed">
                 <VisualState.Setters>
                     <Setter Property="Scale" Value="0.8"/>
                 </VisualState.Setters>
              </VisualState>
          </VisualStateGroup>
     </VisualStateManager.VisualStateGroups>
 </ImageButton>

请帮助。

1 个答案:

答案 0 :(得分:2)

您可以在页面中定义样式,然后将其应用于控件:

<StackLayout.Resources>
    <Style TargetType="ImageButton">
        <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="Scale" Value="1"/>
                            </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Pressed">
                        <VisualState.Setters>
                            <Setter Property="Scale" Value="0.8"/>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</StackLayout.Resources

查看官方文档,那里有an example