【发布时间】:2019-04-04 08:51:29
【问题描述】:
我创建了一个按钮模板。它基于一个框架,它显示为一个方形按钮,当单击该按钮时,它会调用一个命令,以及一个在短时间内改变颜色的后端方法。
按钮有一个绑定参数(Enabled),可以是真或假。
我希望标签文本颜色为 Color.Red 为 true 时为 Color.Green。
<?xml version="1.0" encoding="UTF-8"?>
<t:ButtonBase xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:t="clr-namespace:Japanese.Templates"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.SquareButton" x:Name="this" >
<t:ButtonBase.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand, Source={x:Reference this}}"
CommandParameter="{Binding TapCommandParam, Source={x:Reference this}}"
NumberOfTapsRequired="1" />
<TapGestureRecognizer Tapped="Tapped" />
</t:ButtonBase.GestureRecognizers>
<Label Text="{Binding Text, Source={x:Reference this}}" x:Name="ButtonLabel"
TextColor="{Binding LabelTextColor, Source={x:Reference this}}" />
</t:ButtonBase>
和后端c#
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Japanese.Templates
{
public partial class SquareButton : Frame
{
public static readonly BindableProperty EnabledProperty = BindableProperty.Create(nameof(Enabled), typeof(bool), typeof(ButtonBase), true);
public static readonly BindableProperty LabelTextColorProperty = BindableProperty.Create(nameof(LabelTextColor), typeof(Color), typeof(ButtonBase), default(Color));
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(ButtonBase), default(string));
public bool Enabled { get { return (bool)GetValue(EnabledProperty); } set { SetValue(EnabledProperty, value); } }
public Color LabelTextColor { get { return (Color)GetValue(LabelTextColorProperty); } set { SetValue(LabelTextColorProperty, value); } }
public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } }
public SquareButton()
{
InitializeComponent();
BackgroundColor = (Color)Application.Current.Resources["SquareButtonBackgroundColor"];
}
protected async void Tapped(Object sender, EventArgs e)
{
BackgroundColor = (Color)Application.Current.Resources["CSquareButtonBackgroundColor"];
await Task.Delay(500);
BackgroundColor = (Color)Application.Current.Resources["SquareButtonBackgroundColor"];
}
}
}
模板使用
<template:SquareButton Grid.Column="1"
Enabled="{Binding Btns[0].IsSelected}"
Text="{Binding Btns[0].Name}"
LabelTextColor="{Binding Btns[0].TextColor}"
TapCommand="{Binding BtnsBtnCmd }"
TapCommandParam="{Binding Btns[0].Name}" />
任何人都可以就如何使启用参数的值在红色和绿色之间更改标签的颜色提供任何建议。如果可能的话,我想在 C# 后端代码中执行此操作。
不确定这是否有帮助,但我正在查看此活动:
公共事件 System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
我能否在后端 C# 中检测到这一点,并在更改时检查启用状态来设置颜色?
【问题讨论】:
标签: xamarin xamarin.forms