【发布时间】:2018-07-30 05:36:34
【问题描述】:
我有这个模板:
<?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); }
}
}
目前我使用绑定来设置 TextColor、BorderColor 和 BackgroundColor。但我只需要启用和禁用两种状态。有没有一种方法可以将三个绑定值同时设置为仅使用一个绑定参数的一种或其他颜色集?
编辑:
所以我需要的是只有一个参数,例如:
<template:button enabled="true">
BackgroundColor 将是蓝色
边框颜色为灰色
TextColor 将是白色
或者:
<template:button enabled="false">
BackgroundColor 将是白色
边框颜色为黑色
TextColor 将为灰色
【问题讨论】:
标签: xamarin xamarin.forms