我想调整Button的默认controltemplate。在WPF中,我只是使用blend来查看默认的controltemplate但是对于Xamarin.Forms我不能使用blend。
同样在App.xaml文件中,我看到了对
的引用<ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/>
但是我找不到StandardStyles.xaml文件,所以我也不幸。
在Xamarin网站上,我没有找到默认的控制模板。 那么在哪里/如何找到Xamarin.Forms控件的默认控件模板?
答案 0 :(得分:2)
此时,Xamarin.Forms
没有为按钮提供模板支持 - 因此没有默认模板可供参考(如我们在WPF中所做的那样)或ControlTemplate
属性在{ {1}}。它只是在目标平台上呈现按钮的平台版本。
控件模板通常由具有Button
属性的控件支持。一个很好的例子是ContentView
。您可以在扩展Content
的同时编写自定义按钮控件,同时提供模板支持,并使用ContentView
呈现关联按钮。
例如:
ContentPresenter
样本使用:
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); }
}
}