Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

xamarin.forms - XAML - Static resource not set

I have 2 styles in static resources. First ( with name LabelFont SetterProperty="FontSize")

<OnIdiom x:TypeArguments:"x:Double" Phone="15", Tablet "30"/>.

Second is for setting Margin with OnPlatform and is BasedOn="LabelFont" with name MarginLabel and SetterProperty="Margin":

<OnPlatform x:TypeArguments="x:Double">
   <On Platform="Android" Value="20,0,0,0" />
</OnPlatform>

(Sorry I dont have whole code now, I can post later if its necessary...) When I define a Label, where I use Style="{StaticResources MarginLabel}" the font is set, but not Margin. No error. Can anybody help me where is the problem? Thank you.

question from:https://stackoverflow.com/questions/65907932/xaml-static-resource-not-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of creating two styles,you could combine them in one:

<Style x:Key="MarginLabel" TargetType="Label">
      <Setter Property="FontSize">
          <Setter.Value>
              <OnIdiom x:TypeArguments="x:Double">
                  <OnIdiom.Phone>15</OnIdiom.Phone>
                  <OnIdiom.Tablet>30</OnIdiom.Tablet>
              </OnIdiom>
          </Setter.Value>
      </Setter>
      <Setter Property="Margin">
          <Setter.Value>
              <OnPlatform x:TypeArguments="Thickness">
                  <On Platform="Android" Value="20,0,0,0" />
              </OnPlatform>
          </Setter.Value>
      </Setter>
</Style>

then use like:

<Label Text="Hello" Style="{StaticResource MarginLabel}"></Label>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...