【问题标题】:Xamarin.Forms - Label FontSize OnPlatform - XAML errorXamarin.Forms - 标签字体大小 OnPlatform - XAML 错误
【发布时间】:2017-09-18 07:42:41
【问题描述】:

我有这个代码:

  <Label x:Name="questionGroupHintInfoLabel" FontAttributes="Bold"  Text="Folgende Hinweismeldung wurde für die aktuelle Fragengruppe hinterlegt:">
      <Label.FontSize>
        <OnPlatform x:TypeArguments="NamedSize"
                    iOS="Small"
                    Android="Small" />
      </Label.FontSize>
    </Label>

...并得到这个错误:

No property, bindable property, or event found for FontSize

我做错了什么?

谢谢。

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms xamarin.android


    【解决方案1】:

    通常当我们设置FontSize="value"时,FontSizeConverter会转换为expected type(即double)来设置值。

    但是当我们使用OnPlatform 时,似乎没有使用这个转换器。所以我们有两个选择:

    1. 使用OnPlatformx:Double 作为类型参数。

      <OnPlatform x:TypeArguments="x:Double"
          iOS="20"
          Android="25" />
      
    2. 或者,欺骗 XAML 处理器为我们进行转换 - 我们可以通过使用 StaticResource 标记扩展来做到这一点。 注意: 这仅适用于未应用 XAMLC 的情况。

      <!-- App.Resources or ContentPage.Resources -->
      <ResourceDictionary>
          <OnPlatform x:Key="FontNamedSize" x:TypeArguments="x:String"
              iOS="Small"
              Android="Large" />
      </ResourceDictionary>
      
      <!-- now you can use static-resource extension to use above defined value -->
      <Label x:Name="questionGroupHintInfoLabel" 
             FontAttributes="Bold"  
             Text="Folgende Hinweismeldung wurde für die aktuelle Fragengruppe hinterlegt:" 
             FontSize="{StaticResource FontNamedSize}" />
      
    3. 推荐使用NamedSize可绑定属性扩展Label并转换为FontSize(基本上是FontSizeConverter所做的)。

      public class ExLabel : Label
      {
          public static readonly BindableProperty FontNamedSizeProperty =
              BindableProperty.Create(
                  "FontNamedSize", typeof(NamedSize), typeof(ExLabel),
                  defaultValue: default(NamedSize), propertyChanged: OnFontNamedSizeChanged);
      
          public NamedSize FontNamedSize
          {
              get { return (NamedSize)GetValue(FontNamedSizeProperty); }
              set { SetValue(FontNamedSizeProperty, value); }
          }
      
          private static void OnFontNamedSizeChanged(BindableObject bindable, object oldValue, object newValue)
          {
              ((ExLabel)bindable).OnFontNamedSizeChangedImpl((NamedSize)oldValue, (NamedSize)newValue);
          }
      
          protected virtual void OnFontNamedSizeChangedImpl(NamedSize oldValue, NamedSize newValue)
          {
              FontSize = Device.GetNamedSize(FontNamedSize, typeof(Label));
          }
      }
      
      <!-- Usage -->
      <local:ExLabel HorizontalOptions="Center" VerticalOptions="Center" Text="This is a custom label">
          <local:ExLabel.FontNamedSize>
              <OnPlatform x:TypeArguments="NamedSize"
                  iOS="Large"
                  Android="Medium" />
          </local:ExLabel.FontNamedSize>
      </local:ExLabel>
      

    编辑 1: 选项 2 仅在未应用 XAMLC 时有效。

    编辑 2: 添加选项 3。

    注意:有一个bug fix available in pre-release versions 也可以被视为替代修复。但我无法确认它是否在最新版本中得到修复。

    【讨论】:

    • 谢谢,但第二种方法在运行时给了我这个错误:System.InvalidCastException: Specified cast is not valid.
    • @OPunktSchmidt :用另一个选项更新了答案。希望对您有所帮助!
    • 他们将此问题标记为已解决,但它仍然是一个问题。现在github上有一个新问题github.com/xamarin/Xamarin.Forms/issues/2077
    【解决方案2】:

    旧线程,但是当我找到这个语法时,我已经接近实现上面答案中的第 3 项了

    <Style TargetType="Button" x:Key="ListButtonStyle">
           <Setter Property="FontSize" Value="{OnIdiom Phone={OnPlatform Android=Header, iOS=Micro} , Tablet=Large, Desktop=Default}" />
    </Style>
    

    用带有 ios 和 android 的手机进行了检查,它运行良好。 决定把它留在这里。 xamarin.forms 4.8.0.1687

    【讨论】:

    • 这比 Sharada 提出的 3 个选项要好得多,并且在我的 5.0 Forms 应用程序上运行。
    【解决方案3】:

    您应该为 OnPlatform 尝试新的 XAML:

    <!--Old XAML On Platform-->
    <StackLayout>
      <StackLayout.Padding>
        <OnPlatform x:TypeArguments="Thickness" 
                    Android="0, 0, 0, 0" 
                    WinPhone="0, 0, 0, 0" 
                    iOS="0, 20, 0, 0"/>
      </StackLayout.Padding>
    </StackLayout>
    
    <!--New XAML On Platform-->
    <StackLayout>
      <StackLayout.Padding>
       <OnPlatform x:TypeArguments="Thickness">
         <On Platform="Android" Value="0, 0, 0, 0"/>
         <On Platform="WinPhone" Value="0, 0, 0, 0"/>
         <On Platform="iOS" Value="0, 20, 0, 0"/>
        </OnPlatform>
      </StackLayout.Padding>
    </StackLayout>
    
    <!--Better new XAML On Platform-->
    <StackLayout>
      <StackLayout.Padding>
       <OnPlatform x:TypeArguments="Thickness">
         <On Platform="Android, WinPhone">0</On>
         <On Platform="iOS">0,20,0,0</On>
        </OnPlatform>
      </StackLayout.Padding>
    </StackLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2017-02-27
      • 1970-01-01
      • 2020-05-17
      • 2016-11-11
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多