我已經在XAML中爲您評論了它。現在
<!-- Here is the button declared, note how you also see the x:Name here -->
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<!-- Besides giving properties to a control, in this case a button, as attributes, you can also set them by adding them as child nodes. That is what happens with the FontSize here -->
<Button.FontSize>
<!-- We are not just setting the FontSize here, we are also letting the value be dependent on which platform we are running on. For iOS the value will be 25, for Android 20. -->
<OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" />
</Button.FontSize>
</Button>
,該x:Double
需要告訴OnPlatform
標籤哪種類型,我們應該提供給它,這也爲x:如果需要的Int32或任何其他類型。因爲我們將始終在OnPlatform標籤中提供字符串值,所以需要知道它需要使用哪種類型來轉換值。
請記住,我還指出x:Name
屬性。 x
是用於查找類型的名稱空間的簡寫。如果你看看你的頁面聲明,它可能會有一個屬性,如:xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
,看看x
是如何在這裏聲明的?這就是爲什麼在前面需要x
,以告訴XAML,Double
類型可以在http://schemas.microsoft.com/winfx/2009/xaml
名稱空間中找到,默認情況下縮寫爲x
。
請注意,以這種方式標記的OnPlatform
已從Xamarin.Forms 2.3.4開始棄用。您現在應該這樣使用它:
<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<Button.FontSize>
<OnPlatform x:TypeArguments="x:Double" x:Key="WindowBackgroundTable">
<On Platform="Android" Value="20" />
<On Platform="iOS" Value="25" />
</OnPlatform>
</Button.FontSize>
</Button>
它是按鈕上FontSize的參數 – user2960398