2017-07-10 17 views
14

Trong Xamarin XAML của tôi, tôi sử dụng điều này nhiều lần:Có cách nào tôi có thể tùy chỉnh nhãn mà tôi thường sử dụng không?

<Label Text="{x:Static local:FontAwesome.FACheck}" FontFamily="FontAwesome" 
    XAlign="Center" FontSize="13" 
    TextColor="#1E90FF" /> 

Có cách nào sử dụng C# mà tôi có thể tạo ra một phiên bản tùy chỉnh của Label và sử dụng vì vậy tôi không cần phải giữ cách xác định Font và khác nhiều thứ?

Trả lời

3

Để tạo một phong cách trong C# xin vui lòng xem link to a xamarin developer guide for global styles này.

Ví dụ về mã C#: (thiết lập một từ điển tài nguyên trong lớp ứng dụng của bạn)

public class App : Application 
{ 
    public App() 
    { 
     var buttonStyle = new Style (typeof(Button)) { 
      Setters = { 
       ... 
       new Setter { Property = Button.TextColorProperty, Value = Color.Teal } 
       new Setter { Property = Button.BackgroundColor, Value = Color.White } 

       // add more setters for the properties that you want to set here 
      } 
     }; 

     // add this style into your resource dictionary. 
     Resources = new ResourceDictionary(); 
     Resources.Add ("buttonStyle", buttonStyle); 
     ... 
    } 
    ... 
} 

Bạn có thể tạo điều khiển với những phong cách trong lớp học C# của bạn:

public class ApplicationStylesPageCS : ContentPage 
{ 
    public ApplicationStylesPageCS() 
    { 
     ... 
     Content = new StackLayout { 
      Children = { 
       new Button { Text = "These buttons", Style = (Style)Application.Current.Resources ["buttonStyle"] }, 
       new Button { Text = "are demonstrating", Style = (Style)Application.Current.Resources ["buttonStyle"] }, 
       new Button { Text = "application styles", Style = (Style)Application.Current.Resources ["buttonStyle"] 
       } 
      } 
     }; 
    } 
} 

Hoặc cách khác truy cập nó trong xaml như một tài nguyên tĩnh:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.ApplicationStylesPage" Title="Application" Icon="xaml.png"> 
     <ContentPage.Content> 
     <StackLayout Padding="0,20,0,0"> 
      <Button Text="These buttons" Style="{StaticResource buttonStyle}" /> 
      <Button Text="are demonstrating" Style="{StaticResource buttonStyle}" /> 
      <Button Text="application style overrides" Style="{StaticResource buttonStyle}" /> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 
13

Có thể, bạn có thể sử dụng Style trong App.xaml của mình.

<Application xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="YourAPp.App"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <Style x:Key="FACheck" TargetType="Label"> 
       <Setter Property="Text" Value="{x:Static local:FontAwesome.FACheck}"/> 
       <Setter Property="FontFamily" Value="FontAwesome"/> 
       <Setter Property="XAlign" Value="Center"/> 
       <Setter Property="FontSize" Value="13"/> 
       <Setter Property="TextColor" Value="#1E90FF"/> 
      </Style> 
     <ResourceDictionary> 
    </Application.Resources> 
</Application> 

Và sau đó trong các trang của bạn, bạn chỉ cần sử dụng nó bất cứ nơi nào bạn cần phải đặt nhãn này.

<Label Style="{StaticResource FACheck}"/> 

Trong trường hợp bạn muốn xác định nguồn lực của bạn trong C#

public class App : Application 
{ 
    public App() 
    { 

      //Begin - Style code 

      var faCheckStyle = new Style(typeof(Label)) 
      { 
       Setters = { 
        new Setter { Property = Label.TextProperty, Value = FontAwesome.FAChcek }, 
        new Setter { Property = Label.FontFamilyProperty, Value = "FontAwesome" }, 
        new Setter { Property = Label.XAlignProperty, Value = "FontAwesome" }, 
        new Setter { Property = Label.FontSizeProperty, Value = 13 }, 
        new Setter { Property = Label.TextColorProperty, Value = Color.FromHex("#1E90FF") } 
       } 
      }; 
      Resources = new ResourceDictionary(); 
      Resources.Add("FACheck", faCheckStyle);  

      //End Style code 
    } 
    ... 
} 
+1

Cảm ơn Chúa Giêsu, đề xuất của bạn trông thú vị. Tôi upvoted nhưng chưa bấm vào chấp nhận. Tôi có thể tạo tài nguyên này trong C# không? – Alan2

+0

Vâng, bạn có thể tạo điều này trong C#, hãy nhớ rằng XAML chỉ là cú pháp đường trên mã C#. –

+0

@Alan bạn có thể sử dụng mã thay thế :). Tôi đã cập nhật câu trả lời của mình –

6

Tạo lớp học của bạn và sử dụng nó everyw tại đây

public class MyAwesomeLabel : Xamarin.Forms.Label 
{ 
    public MyAwesomeLabel() 
    { 
     FontFamily = "FontAwesome"; 
     XAlign = Xamarin.Forms.TextAlignment.Center; //deprecated BTW 
     FontSize = 13; 
     TextColor = Color.FromHex(0x1E90FF); 
     //etc 
    } 
} 
Các vấn đề liên quan