2017-07-10 10 views

Odpowiedz

3

Aby utworzyć styl w C# można znaleźć w tym link to a xamarin developer guide for global styles.

Przykład kodu C# (ustawia słownika zasobu w swojej klasie APP)

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); 
     ... 
    } 
    ... 
} 

Można tworzyć kontrolki z tych stylów w swoim # klas C:

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"] 
       } 
      } 
     }; 
    } 
} 

lub alternatywnie dostęp to w Xaml jako statyczny zasób:

<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

Może, możesz użyć Style w swoim App.xaml.

<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> 

A potem w swoich stronach, po prostu trzeba go używać wszędzie tam, gdzie trzeba umieścić tę etykietę.

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

W przypadku, gdy chcesz określić swoje zasoby w 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

Dzięki Jezu Twoja sugestia wygląda interesująco. Przegłosowałem, ale jeszcze nie kliknąłem, by zaakceptować. Czy mogę również utworzyć ten zasób w języku C#? – Alan2

+0

Tak, możesz to zrobić w języku C#, pamiętaj, że XAML to po prostu cukier z syntaktycznego kodu C#. –

+0

@Alan możesz użyć kodu zamiast :). Zaktualizowałem swoją odpowiedź –

6

Stwórz swoją klasę i używaj jej tutaj

public class MyAwesomeLabel : Xamarin.Forms.Label 
{ 
    public MyAwesomeLabel() 
    { 
     FontFamily = "FontAwesome"; 
     XAlign = Xamarin.Forms.TextAlignment.Center; //deprecated BTW 
     FontSize = 13; 
     TextColor = Color.FromHex(0x1E90FF); 
     //etc 
    } 
} 
Powiązane problemy