2017-07-25 59 views
0

我的代碼有:x:Xamarin Button的雙重含義是什麼?

<Button x:Name="correctButton" HeightRequest="60" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand"> 
    <Button.FontSize> 
     <OnPlatform x:TypeArguments="x:Double" iOS="25" Android="20" /> 
    </Button.FontSize> 
</Button> 

有人能解釋X:雙指

+0

它是按鈕上FontSize的參數 – user2960398

回答

1

我已經在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> 
0

對不起,我英文不好。

在XAML中使用時,此標記需要發送給屬性的值的類型,一旦這是通用方法。我相信XF使用反射和鑄件來將屬性設置爲特定的平臺。

查看更多的this article

我希望它能幫助你。