绑定点击命令未触发
Binded TappedCommand is not triggering
谁能指出为什么我的命令没有触发?这是我的自定义控件
public partial class RateControl : Grid
{
public static readonly BindableProperty RateProperty = BindableProperty.Create(nameof(Rating), typeof(int), typeof(RateControl), 1, propertyChanged: OnRatePropertyChanged);
private static void OnRatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if(bindable is RateControl && bindable != null && newValue is int)
{
(bindable as RateControl).OnRateClicked((int)newValue);
}
}
public int Rating
{
set => SetValue(RateProperty, value);
get => (int)GetValue(RateProperty);
}
public ICommand OnRateSelectedCommand { get; set; }
public RateControl()
{
InitializeComponent();
OnRateSelectedCommand = new Command<int>(OnRateClicked);
}
private void OnRateClicked(int rating)
{
Rating = rating;
ChangeStar(Rating);
}
private void ChangeStar(int starcount)
{
for (int i = 1; i <= starcount; i++)
{
(FindByName($"star{i}") as Label).Text = RsrIcon.StarFull;
}
for (int i = starcount + 1; i <= 5; i++)
{
(FindByName($"star{i}") as Label).Text = RsrIcon.StarEmpty;
}
}
}
这是我的 Xaml
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Mobile.Controls.RateControl"
xmlns:common="clr-namespace:Mobile.Common"
xmlns:control="clr-namespace:Mobile.Controls"
RowDefinitions="Auto"
x:Name="component"
ColumnDefinitions="30,30,30,30,30"
ColumnSpacing="10">
<control:TappableLabel FontSize="Large"
x:Name="star1"
TappedCommand="{x:Binding OnRateSelectedCommand, Source={x:Reference component}}"
TappedCommandParameter="1"
Text="{Static common:RsrIcon.StarEmpty}"
TextColor="{StaticResource RsrYellow}"
VerticalOptions="Center"/>
.......
所以 TappableLabel 只是一个自定义的 TappedGesture,点击 RateControl 时,事情是 StarFull 或 StarEmpty。命令的初始化工作正常,但点击后没有任何反应。
我从未见过 {x:Binding ...
与 Xamarin Forms 一起使用。
将其更改为 {Binding ...
:
... TappedCommand="{Binding OnRateSelectedCommand, Source={x:Reference component}}"
可选地, 在构造函数的最后一行添加:
BindingContext = this;
然后你可以简单地用{Binding OnRateSelectedCommand}
进行绑定:
... TappedCommand="{Binding OnRateSelectedCommand}"
Command 的通用类型应该是 string
而不是 int
。
修改你的代码如下
//xaml
TappedCommand="{Binding OnRateSelectedCommand}"
public RateControl()
{
InitializeComponent();
OnRateSelectedCommand = new Command<string>(OnRateClicked);
BindingContext = this;
}
private void OnRateClicked(string rating)
{
}
谁能指出为什么我的命令没有触发?这是我的自定义控件
public partial class RateControl : Grid
{
public static readonly BindableProperty RateProperty = BindableProperty.Create(nameof(Rating), typeof(int), typeof(RateControl), 1, propertyChanged: OnRatePropertyChanged);
private static void OnRatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if(bindable is RateControl && bindable != null && newValue is int)
{
(bindable as RateControl).OnRateClicked((int)newValue);
}
}
public int Rating
{
set => SetValue(RateProperty, value);
get => (int)GetValue(RateProperty);
}
public ICommand OnRateSelectedCommand { get; set; }
public RateControl()
{
InitializeComponent();
OnRateSelectedCommand = new Command<int>(OnRateClicked);
}
private void OnRateClicked(int rating)
{
Rating = rating;
ChangeStar(Rating);
}
private void ChangeStar(int starcount)
{
for (int i = 1; i <= starcount; i++)
{
(FindByName($"star{i}") as Label).Text = RsrIcon.StarFull;
}
for (int i = starcount + 1; i <= 5; i++)
{
(FindByName($"star{i}") as Label).Text = RsrIcon.StarEmpty;
}
}
}
这是我的 Xaml
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Mobile.Controls.RateControl"
xmlns:common="clr-namespace:Mobile.Common"
xmlns:control="clr-namespace:Mobile.Controls"
RowDefinitions="Auto"
x:Name="component"
ColumnDefinitions="30,30,30,30,30"
ColumnSpacing="10">
<control:TappableLabel FontSize="Large"
x:Name="star1"
TappedCommand="{x:Binding OnRateSelectedCommand, Source={x:Reference component}}"
TappedCommandParameter="1"
Text="{Static common:RsrIcon.StarEmpty}"
TextColor="{StaticResource RsrYellow}"
VerticalOptions="Center"/>
.......
所以 TappableLabel 只是一个自定义的 TappedGesture,点击 RateControl 时,事情是 StarFull 或 StarEmpty。命令的初始化工作正常,但点击后没有任何反应。
我从未见过 {x:Binding ...
与 Xamarin Forms 一起使用。
将其更改为 {Binding ...
:
... TappedCommand="{Binding OnRateSelectedCommand, Source={x:Reference component}}"
可选地, 在构造函数的最后一行添加:
BindingContext = this;
然后你可以简单地用{Binding OnRateSelectedCommand}
进行绑定:
... TappedCommand="{Binding OnRateSelectedCommand}"
Command 的通用类型应该是 string
而不是 int
。
修改你的代码如下
//xaml
TappedCommand="{Binding OnRateSelectedCommand}"
public RateControl()
{
InitializeComponent();
OnRateSelectedCommand = new Command<string>(OnRateClicked);
BindingContext = this;
}
private void OnRateClicked(string rating)
{
}