xamarin - 我可以使用一个参数同时在模板上设置多个属性吗?

标签 xamarin xamarin.forms

我有这个模板:

<?xml version="1.0" encoding="utf-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
       x:Class="Japanese.Templates.ButtonTemplate" 
       x:Name="this" CornerRadius="5" 
       BackgroundColor="{Binding FrameBackgroundColor, Source={x:Reference this}}"
       BorderColor="{Binding FrameBorderColor, Source={x:Reference this}}"
       HorizontalOptions="FillAndExpand" HasShadow="false" 
       HeightRequest="35" Padding="0">
    <StackLayout Orientation="Horizontal" Padding="10,5" HorizontalOptions="FillAndExpand">
        <Label Text="{Binding Text,  Source={x:Reference this}}" FontSize="16" 
               TextColor="{Binding LabelTextColor, Source={x:Reference this}}"
               x:Name="label1"
               HorizontalOptions="CenterAndExpand"
               VerticalOptions="Center" HorizontalTextAlignment="Center" />
        <Frame.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding TapButtonPressed, Source={x:Reference this}}" CommandParameter="{Binding Param, Source={x:Reference this}}" NumberOfTapsRequired="1" />
        </Frame.GestureRecognizers>
    </StackLayout>
</Frame>

还有这个支持CS:

    

public partial class ButtonTemplate : Frame
{
    public event EventHandler Action;

    public ButtonTemplate()
    {
        InitializeComponent();
    }

    public ICommand TapButtonPressed
    {
        get
        {
            return new Command((object componentIdentifier) =>
            {
                this.Action?.Invoke(this, new EventArgs());
            });
         }
     }

     public static readonly BindableProperty EnabledProperty =
            BindableProperty.Create(
                nameof(Enabled),
                typeof(bool),
                typeof(ButtonTemplate),
                default(bool));
                        
     public bool Enabled { get; set; }

     public static readonly BindableProperty FrameBackgroundColorProperty =
            BindableProperty.Create(
                nameof(FrameBackgroundColor),
                typeof(Color),
                typeof(ButtonTemplate),
                default(Color));
        
     public static readonly BindableProperty FrameBorderColorProperty =
            BindableProperty.Create(
                nameof(FrameBorderColor),
                typeof(Color),
                typeof(ButtonTemplate),
                default(Color));
        
     public static readonly BindableProperty ParamProperty =
            BindableProperty.Create(
                nameof(Param),
                typeof(string),
                typeof(ButtonTemplate),
                default(string));
        
     public static readonly BindableProperty LabelTextColorProperty =
            BindableProperty.Create(
                nameof(LabelTextColor),
                typeof(Color),
                typeof(ButtonTemplate),
                default(Color));
        
     public static readonly BindableProperty TextProperty =
            BindableProperty.Create(
                nameof(Text),
                typeof(string),
                typeof(ButtonTemplate),
                default(string));
        
     public Color FrameBackgroundColor
     {
         get { return (Color)GetValue(FrameBackgroundColorProperty); }
         set { SetValue(FrameBackgroundColorProperty, value); }
     }

     public Color FrameBorderColor
     {
         get { return (Color)GetValue(FrameBorderColorProperty); }
         set { SetValue(FrameBorderColorProperty, value); }
     }

     public Color LabelTextColor
     {
         get { return (Color)GetValue(LabelTextColorProperty); }
         set { SetValue(LabelTextColorProperty, value); }
     }

     public string Param
     {
         get { return (string)GetValue(ParamProperty); }
         set { SetValue(ParamProperty, value); }
     }

     public string Text
     {
         get { return (string)GetValue(TextProperty); }
         set { SetValue(TextProperty, value); }
     }
 }

目前我使用绑定(bind)来设置 TextColor、BorderColor 和 BackgroundColor。但我所需要的只是启用和禁用两种状态。有没有一种方法可以仅使用一个绑定(bind)参数将三个绑定(bind)值同时设置为一种或其他颜色集?

编辑:

所以我需要的是只有一个参数,例如:

<template:button enabled="true">

背景颜色为蓝色

边框颜色将为灰色

文本颜色将为白色

或者:

<template:button enabled="false">

背景颜色为白色

边框颜色将为黑色

文本颜色将为灰色

最佳答案

基本上,您正在尝试创建一个可重用的控件。

最简单的方法就是添加 Enabled代码隐藏中的属性。这还允许您从 XAML 进行设置。

只需添加:public bool Enabled { get; set; } .

然后,您可以在 setter 中通过名称引用模板中的控件并设置类似的属性。您必须添加 x:Key属性到每个控件。

看到您已经有了数据绑定(bind),您应该能够从 setter 中更新要绑定(bind)到的属性。

如果您还希望能够绑定(bind)到新的 Enabled属性,您必须将其创建为 BindableProperty (docs)。添加此:

public static readonly BindableProperty EnabledProperty =
  BindableProperty.Create (nameof(Enabled), typeof(bool), typeof(ButtonTemplate), null, propertyChanged: OnEnabledChanged);

public bool Enabled { get; set; }

private static void OnEnabledChanged (BindableObject bindable, object oldValue, object newValue)
{
  // Set the color properties here
}

BindableProperty有一个属性更改方法,您可以在其中设置颜色的属性。通过这样实现,您还可以绑定(bind)到 Enabled属性:<template:button enabled="{Binding IsValid}">

编辑:

我所说的设置属性的意思是这样的。但从您的新代码中,我发现您此处没有数据绑定(bind)。您确实已经命名了控件,因此您只需引用它们并设置它们的属性,如下所示:

private static void OnEnabledChanged (BindableObject bindable, object oldValue, object newValue)
{
    // Referencing controls
    if ((bool)newValue)
    {
        BorderColor = Color.Red;
        label1.BackgroundColor = Color.Red;
    }
    else
    {
        BorderColor = Color.Green;
        label1.BackgroundColor = Color.Green;
    }

    // Using bindings
    if ((bool)newValue)
    {
        FrameBackgroundColor = Color.Red;
        FrameBorderColor = Color.Red;
    }
    else
    {
        FrameBackgroundColor = Color.Green;
        FrameBorderColor = Color.Green;
    }
}

我看到您已将您的名称命名为 Frame this 。这可能会导致问题,因为 this是.NET 中的保留关键字。您可能想改变这一点。

关于xamarin - 我可以使用一个参数同时在模板上设置多个属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587540/

相关文章:

xamarin.forms - 如何创建溢出菜单(不在工具栏上)- Xamarin.Forms

c# - MvvmCross Android转换器导致光标跳转

c# - 将参数从 Detail 传递到 MVVMCross 中的 MainViewModel

android - 在 Xamarin 中处理按钮单击事件

android - fragment 是否可以扩展 fragment ?

visual-studio - 更新 Xamarin 后,我得到引用的组件 *** 无法找到

Xamarin.forms 无需在 Android 中使用拨号器即可调用电话

android - 如何在 Visual Studio 2017 中禁用 "Use Mono Shared Runtime"

.net - 如何在 .NET MAUI 中为多个按钮创建单击事件

c# - 在 xamarin 表单上动态绑定(bind)图像源