Xamarin 如何将模板上的XAML元素绑定到C#后端而不是XAML中?
以下是我现在拥有的: 我有一个简化的问题模板:Xamarin 如何将模板上的XAML元素绑定到C#后端而不是XAML中?,xamarin,xamarin.forms,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:t="clr-namespace:Japanese.Templates"
<?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#back end中添加标签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>
现在,每当更改
LabelTextColor
属性值时,标签的TextColor属性也应该更改,就像使用XAML绑定一样 是否要从模板外部绑定颜色?是否为baseframebuttonemplate
aControlTemplate
子类?
<?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>
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");
}
}
}