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>
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
Tak, możesz to zrobić w języku C#, pamiętaj, że XAML to po prostu cukier z syntaktycznego kodu C#. –
@Alan możesz użyć kodu zamiast :). Zaktualizowałem swoją odpowiedź –