[英]Xamarin.Forms - Label FontSize OnPlatform - XAML error
我有這個代碼:
<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
我在做什么錯?
謝謝。
通常,當我們設置FontSize="value"
, FontSizeConverter
會轉換為預期類型(即double
)以設置值。
但是看起來當我們使用OnPlatform
時沒有使用這個轉換器。 所以我們有兩個選擇:
使用OnPlatform
和x:Double
作為類型參數。
<OnPlatform x:TypeArguments="x:Double" iOS="20" Android="25" />
或者,欺騙 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}" />
推薦使用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。
舊線程,但是當我找到此語法時,我已接近實現上面答案中的第 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
您應該為 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.