【问题标题】:Is it possible to replace/modify Implicit style at run time xamarin forms是否可以在运行时替换/修改隐式样式 xamarin 表单
【发布时间】:2021-05-15 06:42:38
【问题描述】:

我需要在运行时替换位于合并资源字典中的隐式样式

例如,假设我有

<Style TargetType="Label">
      <Setter Property="FontSize" Value="14" />
</Style>

现在我想在运行时将所有样式替换为其他样式。这个可以吗?

以上只是一个示例。与主题或其他任何事情无关,我需要在运行时替换所有隐式样式。

可以根据 TargetType 检测吗?

谢谢

【问题讨论】:

  • 听起来像theming
  • 其实在这种情况下,我需要在运行时根据是否是小型设备来更改一些字体大小

标签: xamarin.forms resourcedictionary


【解决方案1】:

如果我正确理解您的问题,我认为data triggers 可能会对您有所帮助。例如,您可以将FontSize 更改为:

<Style x:Key="labelStyle" TargetType="Label">
   <Setter Property="FontSize" Value="14" />
   <Style.Triggers>
      <DataTrigger TargetType="Label" Binding="{Binding isSmallDevice}" Value="True">
         <Setter Property="FontSize" Value="10" />
      </DataTrigger>
   </Style.Triggers>
</Style>

【讨论】:

  • 感谢您的回复。鉴于“IsSmallDevice”存在于 app.xaml 中,作为 MergedDictionary 一部分的样式是否能够看到它?正如您可以想象的那样,我不希望数据触发器分散在各处
【解决方案2】:

实际上在这种情况下,我需要在运行时根据是否是小型设备来更改一些字体大小

您可以使用OnIdiom markup extension 来检测电话或桌子或桌面。

 <Label Text="Welcome to Xamarin.Forms!">
            <Label.Style>
                <Style TargetType="Label">
                    <Setter Property="FontSize">
                        <Setter.Value>
                            <OnIdiom
                                x:TypeArguments="x:Double"
                                Phone="18"
                                Tablet="50" />
                        </Setter.Value>
                    </Setter>                   
                </Style>
            </Label.Style>
        </Label>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2017-04-26
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多