【问题标题】:How define style in App.xaml Xamarin Forms for CustomButton?如何在 App.xaml Xamarin Forms 中为 CustomButton 定义样式?
【发布时间】:2017-12-06 07:30:24
【问题描述】:

如何在 App.xaml 中为 CustomButton 定义样式?

App.xaml

 <Style x:Key="CustomButtonSmall" TargetType="CustomButton">
     <Setter Property="FontSize" Value="14" />
 </Style>

MyPage.xaml

<local:CustomButton Text="{i18n:Translate CreateAccountButton}"  
      Grid.Column="0" Command="{Binding CreateAccountCommand}" 
      Type="Normal" Style="{StaticResource CustomButtonSmall}" />

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

    您在例如 Window.xaml 中定义样式:

    <Window>
    <Window.Resources>
        <Style x:Key="myStyle" TargetType="Button">
           <Setter Property="Background" Value="Orange" />
           <Setter Property="FontStyle" Value="Italic" />
        </Style>
    </Window.Resources>
    

    然后你用这个来定位你的按钮:

     <Button Style="{StaticResource myStyle}">Buttontext</Button>
    

    【讨论】:

    • 但我的 TargetType 是 CutomButton.class,而不是 Button
    【解决方案2】:

    在 App.xaml 中

    给 CustomButton 整个路径,例如 TargetType="Control:CustomButton" 并在顶部定义 Control,如

     xmlns:Control="clr-namespace:xyz" 
    
     <Style x:Key="CustomButtonSmall" TargetType="Control:CustomButton">
                <Setter Property="FontSize" Value="14" />
            </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多