这是我现在所拥有的:
我有这个问题的简化模板:
<?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:t="clr-namespace:Japanese.Templates"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.RoundButtonText" x:Name="this">
<Label Text="ABC"
TextColor="{Binding LabelTextColor, Source={x:Reference this}}"
/>
</Frame>
和这个 C#
using Xamarin.Forms;
namespace Japanese.Templates
{
public partial class RoundButtonText : BaseFrameButtonTemplate
{
public RoundButtonText()
{
InitializeComponent();
// I would like to put the Label TextColor binding here instead of in the XAML
}
}
}
有人可以通过告诉我如何在 C# 后端添加标签 TextColor 的绑定来帮助我,以便它以与当前编写的方式完全相同的方式更改:
TextColor="{Binding LabelTextColor, Source={x:Reference this}}"
在 XAML 中?
从 XAML 中删除绑定并为 Label 控件提供一个 x:Name:
<?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:t="clr-namespace:Japanese.Templates"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.RoundButtonText" x:Name="this">
<Label x:Name="label"
Text="ABC" />
</Frame>
在后面的 C# 代码中设置绑定和要绑定到的属性:
using Xamarin.Forms;
namespace Japanese.Templates
{
public partial class RoundButtonText : BaseFrameButtonTemplate
{
Color _labelTextColor;
public Color LabelTextColor {
get {
return _labelTextColor;
}
set {
if (_labelTextColor != value) {
_labelTextColor = value;
OnPropertyChanged("LabelTextColor");
}
}
}
public RoundButtonText()
{
InitializeComponent();
label.BindingContext = this;
label.SetBinding(Label.TextColorProperty, "LabelTextColor");
}
}
}
现在,无论何时LabelTextColor
更改属性值,标签的 TextColor 属性也应该更改,就像使用 XAML 绑定一样。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句