基于默认样式 MAUI 的风格

huangapple go评论74阅读模式
英文:

Style based on default style MAUI

问题

我有一个所有Label默认样式

<Style TargetType="Label">
    <Setter ...
</Style>

然后我有一个带有x:KeyLabel样式

<Style x:Key="notForAll" TargetType="Label">
    <Setter ...
</Style>

所以,如果我这样做

<Label Style="{StaticResource notForAll}"/>

这个标签将不包括默认的标签样式,只有notForAll样式。

如何以default标签样式为基础创建notForAll样式?我不能将默认样式放入BasedOn

<Style x:Key="notForAll" TargetType="Label" BasedOn="{StaticResource ????????????}">
    <Setter ...
</Style>

我找到了WPF的解决方案

<Style x:Key="NamedStyle" TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
    <Setter ...
</Style>

但这不适用于MAUI

英文:

I have default style for all Label

<Style TargetType="Label">
    <Setter ...
</Style>

Then I have style with x:Key for Label

<Style x:Key="notForAll" TargetType="Label">
    <Setter ...
</Style>

So, if i do this

<Label Style="{StaticResource notForAll}"/>

This label will not include default label style, only notForAll style.

How can I base notForAll style on default label style? I can`t put default style into BasedOn

<Style x:Key="notForAll" TargetType="Label" BasedOn="{StaticResource ????????????}">
    <Setter ...
</Style>

I found solution for WPF

<Style x:Key="NamedStyle" TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
    <Setter ...
</Style>

But it doesn't work for MAUI.

答案1

得分: 3

样式继承说:

> 隐式样式可以从显式样式派生,但显式样式不能从隐式样式派生。

我认为你需要这样做:

<Style x:Key="commonLabelStyle" TargetType="Label">
    <Setter ...
</Style>

<Style TargetType="Label" BasedOn="{StaticResource commonLabelStyle}">
    <Setter ...
</Style>

<Style x:Key="notForAll" TargetType="Label" BasedOn="{StaticResource commonLabelStyle}">
    <Setter ...
</Style>
英文:

Style inheritance says:

> An implicit style can be derived from an explicit style, but an explicit style can't be derived from an implicit style.

I think that you have to do something like this:

&lt;Style x:Key=&quot;commonLabelStyle&quot; TargetType=&quot;Label&quot;&gt;
    &lt;Setter ...
&lt;/Style&gt;

&lt;Style TargetType=&quot;Label&quot; BasedOn=&quot;{StaticResource commonLabelStyle}&quot;&gt;
    &lt;Setter ...
&lt;/Style&gt;

&lt;Style x:Key=&quot;notForAll&quot; TargetType=&quot;Label&quot; BasedOn=&quot;{StaticResource commonLabelStyle}&quot;&gt;
    &lt;Setter ...
&lt;/Style&gt;

huangapple
  • 本文由 发表于 2023年6月13日 01:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定