2017-06-15 73 views
0

我在我的應用程序中使用自定義字體,並且需要將字體屬性更改爲粗體,據我所知可能並不簡單。在.ttf文件之間動態更改Xamarin表單

我已經成功地動態地更改顏色,並使用資源字典默認字體屬性:

<Color x:Key="LabelColor">White</Color> 
<FontAttributes x:Key="LabelFontAtt">None</FontAttributes> 

,然後使用MVVM改變我的視圖模型的顏色和屬性:

App.Current.Resources["LabelColor"] = Xamarin.Forms.Color.FromHex("#01bf89"); 
App.Current.Resources["LabelFontAtt"] = FontAttributes.Bold; 

從以前的問題中,我已經學會了如何將我的自定義字體設置爲所有標籤的默認字體:

<Style TargetType="Label"> 
    <Setter Property="FontFamily"> 
     <Setter.Value> 
      <OnPlatform x:TypeArguments="x:String"> 
       <OnPlatform.Android>JosefinSlab-Regular.ttf#JosefinSlab-Regular</OnPlatform.Android> 
      </OnPlatform> 
     </Setter.Value> 
    </Setter> 
</Style> 

我想在JosefinSlab-Regular.ttfJosefinSlab-Bold.ttf之間切換,但我這樣做沒有多大成功。是否有可能通過我已經完成的屬性和顏色的變體來完成它,還是應該以另一種方式來完成?

回答

1

在當前的設置,您可以創建第二個樣式像這樣:

<Style TargetType="Label" x:Key="BoldLabel"> 
    <Setter Property="FontFamily"> 
     <Setter.Value> 
      <OnPlatform x:TypeArguments="x:String"> 
       <OnPlatform.Android>JosefinSlab-Bold.ttf#JosefinSlab-Bold</OnPlatform.Android> 
      </OnPlatform> 
     </Setter.Value> 
    </Setter> 
</Style> 

然後換出你基於Style鑰匙掉出TextColorStyle以同樣的方式。

我顯然不知道你想要實現什麼功能,但是從你所說的話看來,你似乎在你的ViewModel中放置了UI邏輯(比如顏色),這與MVVM應該如何使用在Xamarin形式。如果您想根據ViewModel中的數據更改顏色和樣式,則還可以查看Triggers,它只存在於UI端,因此您可以清晰地分離ViewModel邏輯和UI邏輯。

+0

關於MVVM,您當然是非常正確的,我會牢記重構 –