【问题标题】:Xamarin Boolean Bindable PropertyXamarin 布尔可绑定属性
【发布时间】:2020-09-23 20:14:08
【问题描述】:

我正在尝试制作可用于愿望清单的可点击图标,为此我创建了返回 Image 的布尔属性。

这是我的代码,但不支持onClick事件,请指教解决这个问题。

  public class WishIconImg : Image, IDisposable
    {

        static FontImageSource unselected_source = new FontImageSource();
        static FontImageSource selected_source = new FontImageSource();

        public WishIconImg()
        {
            unselected_source.FontFamily = "FA-S";
            unselected_source.Glyph = "\U000f02d5";
            unselected_source.Color = Color.DarkOrange;

            selected_source.FontFamily = "FA-S";
            selected_source.Glyph = "\U000f02d1";
            selected_source.Color = Color.DarkOrange;



           OnClick += Checkbox_OnClick;

        }

        public static BindableProperty IsCheckedProperty = BindableProperty.Create(
            nameof(IsChecked), typeof(bool), typeof(WishIconImg), defaultBindingMode: BindingMode.TwoWay,
            propertyChanged: IsCheckedChanged);

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        private static void IsCheckedChanged(BindableObject bindable, object oldValue, object newValue)
        {

            var cb = (WishIconImg)bindable;
            if (cb == null)
                return;

            if ((bool)newValue)
            {
                cb.Source = selected_source;
            }
            else
            {
                cb.Source=unselected_source ;
            }



        }

       void Checkbox_OnClick(object sender, EventArgs e)
        {
            IsChecked = !IsChecked;

        }

        public void Dispose()
        {
            OnClick -= Checkbox_OnClick;
        }



    }
}

Xaml

 <controls:WishIconImg   x:Name="HeartChk" IsChecked="{Binding AddWish, Mode=TwoWay}" HeightRequest="35" WidthRequest="35"  HorizontalOptions="End"/>

即使我尝试过使用 Label 属性,但它不起作用

【问题讨论】:

  • 您也可以发布 UI 的 xaml 代码吗?我为您提供了更好的解决方案,如果您只想在其上添加 OnClick 事件,我将使用更好的解决方案编辑您的 UI
  • Xaml被添加了,我只是在xaml中调用这个控件

标签: xamarin.forms


【解决方案1】:

你可以像下面这样修改类

public WishIconImg()
        {
            unselected_source.FontFamily = "FA-S";
            unselected_source.Glyph = "\U000f02d5";
            unselected_source.Color = Color.DarkOrange;

            selected_source.FontFamily = "FA-S";
            selected_source.Glyph = "\U000f02d1";
            selected_source.Color = Color.DarkOrange;

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (s, e) => {
                // handle the tap

                IsChecked = !IsChecked;

            };
            this.GestureRecognizers.Add(tapGestureRecognizer);



        }

【讨论】:

【解决方案2】:

尝试为点击事件添加TapGestureRecognizer

做这样的事情

<StackLayout HeightRequest="35" WidthRequest="35"  HorizontalOptions="End">
    <controls:WishIconImg   x:Name="HeartChk" IsChecked="{Binding AddWish, Mode=TwoWay}" />
    <StackLayout.GestureRecognizers>
          <TapGestureRecognizer Command="{Binding Checkbox_OnClick}" /> 
    </StackLayout.GestureRecognizers>
</StackLayout>

在你的 ViewModel 中为它绑定命令

public System.Windows.Input.ICommand Checkbox_OnClick => new Xamarin.Forms.Command(Checkbox_OnClickTapped);

Checkbox_OnClickTapped 将是您点击视图时调用的方法

【讨论】:

  • 你为他们导入了什么包?
  • 如果我按照您的示例更改代码,TapGestureRecognizer Command="{Binding Checkbox_OnClick}" /> 会显示错误,
  • 我应该在控件中还是在 ViewModel 中创建 Checkbox_OnClick ?
  • 在视图模型中,但如果您的问题已经解决,您可以尝试以备不时之需...添加点击任何布局
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2018-03-01
  • 2021-12-23
  • 2018-09-28
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多