Xamarin.Forms 默认控件模板

标签 xamarin.forms controltemplate

我想调整一个按钮的默认控制模板。在 WPF 中,我只会使用 blend 来查看默认的控制模板,但对于 Xamarin.Forms,我不能使用 blend。

我还在 App.xaml 文件中看到了对

的引用
<ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/>

但我没有找到 StandardStyles.xaml 文件,所以我也不走运。

在 Xamarin 网站上我也没有找到默认的控制模板。 那么我在哪里/如何找到 Xamarin.Forms 控件的默认控件模板?

最佳答案

此时,Xamarin.Forms 不为按钮提供模板支持 - 因此没有默认模板可供引用(就像我们在 WPF 中所做的那样)或 ControlTemplate Button 中的属性。它只是在目标平台上呈现按钮的平台版本。

控件模板通常由具有 Content 属性的控件支持。一个很好的例子是 ContentView .您可以在扩展 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}" />

关于Xamarin.Forms 默认控件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46144606/

相关文章:

android - 在我的 PCL Xamarin Forms 中使用 Mono.Android

c# - Xamarin 表单以编程方式扩展 ListView 高度

WPF:从 ControlTemplate 绑定(bind)到命令

wpf - WPF可重用窗口模型/模板

c# - 如何在 Xamarin.Forms 中制作可绑定(bind)标记扩展

javascript - Xamarin 本地加载 JS 脚本

c# - 样式化 Xamarin.Forms DisplayAlert

WPF 覆盖 setter 属性

c# - 基于 ViewModel 属性的条件 ControlTemplate

c# - 无法在 ControlTemplate 中引用控件