Xamarin.forms 表单默认控件模板
我想调整按钮的默认控件模板。在WPF中,我只使用blend查看默认的controltemplate,但对于Xamarin.Forms,我不能使用blend 在App.xaml文件中,我还看到了对Xamarin.forms 表单默认控件模板,xamarin.forms,controltemplate,Xamarin.forms,Controltemplate,我想调整按钮的默认控件模板。在WPF中,我只使用blend查看默认的controltemplate,但对于Xamarin.Forms,我不能使用blend 在App.xaml文件中,我还看到了对 <ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/> 但是我没有找到StandardStyles.xaml文件,所以我在那里也不走运 在Xamarin站点上,我也找不到默认的controltem
<ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/>
但是我没有找到StandardStyles.xaml文件,所以我在那里也不走运
在Xamarin站点上,我也找不到默认的controltemplates。
那么在哪里/如何找到Xamarin.Forms控件的默认controltemplates呢?此时,
Xamarin.Forms
没有为按钮提供模板支持-因此没有默认模板可供参考(就像我们在WPF中所做的那样)或按钮中的ControlTemplate
属性。它只是在目标平台中呈现button的平台版本
控件模板通常由具有Content
属性的控件支持。一个很好的例子是。您可以在扩展ContentView
的同时编写自定义按钮控件,同时提供模板支持,并使用ContentPresenter
呈现关联的按钮
编辑1-具有模板化支持的自定义按钮控件
例如:
public class TemplatedButton : ContentView
{
public TemplatedButton()
{
var button = new Button();
button.SetBinding(Button.TextColorProperty, new Binding(nameof(TextColor), source: this));
button.SetBinding(BackgroundColorProperty, new Binding(nameof(BackgroundColor), source: this));
button.SetBinding(IsEnabledProperty, new Binding(nameof(IsEnabled), source: this));
button.SetBinding(Button.TextProperty, new Binding(nameof(Text), source: this));
button.SetBinding(Button.CommandProperty, new Binding(nameof(Command), source: this));
button.SetBinding(Button.CommandParameterProperty, new Binding(nameof(CommandParameter), source: this));
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, new Binding(nameof(Command), source: this));
tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding(nameof(CommandParameter), source: this));
GestureRecognizers.Add(tapGestureRecognizer);
Content = button;
}
public static readonly BindableProperty TextProperty =
BindableProperty.Create(
"Text", typeof(string), typeof(TemplatedButton),
defaultValue: default(string));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(
"Command", typeof(ICommand), typeof(TemplatedButton),
defaultValue: new Command((obj) => System.Diagnostics.Debug.WriteLine("TemplatedButton Tapped")));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(
"CommandParameter", typeof(object), typeof(TemplatedButton),
defaultValue: default(object));
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly BindableProperty TextColorProperty =
BindableProperty.Create(
"TextColor", typeof(Color), typeof(TemplatedButton),
defaultValue: default(Color));
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
}
<!-- App/Page resources -->
<ResourceDictionary>
<ControlTemplate x:Key="ThreeBorderBtn">
<Grid RowSpacing="0" ColumnSpacing="0" Margin="0">
<BoxView Margin="5" BackgroundColor="Purple" />
<BoxView Margin="10" BackgroundColor="Green" />
<BoxView Margin="15" BackgroundColor="Red" />
<ContentPresenter Margin="20" />
</Grid>
</ControlTemplate>
</ResourceDictionary>
<!-- Control usage -->
<!-- make sure to register xmlns:local namespace -->
<local:TemplatedButton
HeightRequest="100"
Text="Button Caption!"
TextColor="Teal"
Command="{Binding ClickCommand}"
BackgroundColor="White"
ControlTemplate="{StaticResource ThreeBorderBtn}" />
示例用法:
public class TemplatedButton : ContentView
{
public TemplatedButton()
{
var button = new Button();
button.SetBinding(Button.TextColorProperty, new Binding(nameof(TextColor), source: this));
button.SetBinding(BackgroundColorProperty, new Binding(nameof(BackgroundColor), source: this));
button.SetBinding(IsEnabledProperty, new Binding(nameof(IsEnabled), source: this));
button.SetBinding(Button.TextProperty, new Binding(nameof(Text), source: this));
button.SetBinding(Button.CommandProperty, new Binding(nameof(Command), source: this));
button.SetBinding(Button.CommandParameterProperty, new Binding(nameof(CommandParameter), source: this));
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, new Binding(nameof(Command), source: this));
tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding(nameof(CommandParameter), source: this));
GestureRecognizers.Add(tapGestureRecognizer);
Content = button;
}
public static readonly BindableProperty TextProperty =
BindableProperty.Create(
"Text", typeof(string), typeof(TemplatedButton),
defaultValue: default(string));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(
"Command", typeof(ICommand), typeof(TemplatedButton),
defaultValue: new Command((obj) => System.Diagnostics.Debug.WriteLine("TemplatedButton Tapped")));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create(
"CommandParameter", typeof(object), typeof(TemplatedButton),
defaultValue: default(object));
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly BindableProperty TextColorProperty =
BindableProperty.Create(
"TextColor", typeof(Color), typeof(TemplatedButton),
defaultValue: default(Color));
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
}
<!-- App/Page resources -->
<ResourceDictionary>
<ControlTemplate x:Key="ThreeBorderBtn">
<Grid RowSpacing="0" ColumnSpacing="0" Margin="0">
<BoxView Margin="5" BackgroundColor="Purple" />
<BoxView Margin="10" BackgroundColor="Green" />
<BoxView Margin="15" BackgroundColor="Red" />
<ContentPresenter Margin="20" />
</Grid>
</ControlTemplate>
</ResourceDictionary>
<!-- Control usage -->
<!-- make sure to register xmlns:local namespace -->
<local:TemplatedButton
HeightRequest="100"
Text="Button Caption!"
TextColor="Teal"
Command="{Binding ClickCommand}"
BackgroundColor="White"
ControlTemplate="{StaticResource ThreeBorderBtn}" />