Change of color de my custom enter once is clicked my button

谁说我不能喝 提交于 2020-04-16 05:49:30

问题


im working with custon entry rendered, i need to hear from xaml in my custom render when i clicked my button

i have this code in my xaml

<local:MyEntry   eventRefresh="true">

when i clicked my button this function is actived

private async void Execute(object sender)
        {
    var entry = ((MyEntry)view);
    entry.eventRefresh = "false";

but my EntryRendered donot hear the change

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var element = Element as MyEntry;               

回答1:


You should define the property eventRefresh as Bindable Property .

in your custom Entry

using System;

using System.ComponentModel;
using System.Runtime.CompilerServices;

using Xamarin.Forms;
namespace xxx
{
    public class MyEntry:Entry,INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public static readonly BindableProperty eventRefreshProperty = BindableProperty.Create("eventRefresh", typeof(bool), typeof(MyEntry), true,propertyChanged:(obj,oldValue,newValue)=> {

            //var entry = obj as MyEntry;

          //  entry.Text = newValue.ToString();


        });

        bool refresh;
        public bool eventRefresh
        {
            get { return refresh; }
            set {
                  if(refresh !=value)
                  {
                    refresh = value;
                    NotifyPropertyChanged("eventRefresh");
                  }

              }
        }



        public MyEntry()
        {

        }



    }
}

in xaml

<StackLayout  VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">


   <local:MyEntry eventRefresh="{Binding Refresh}" BackgroundColor="{Binding BGcolor}" WidthRequest="200" HeightRequest="50" />

   <Button Command="{Binding ClickCommand}"  />


</StackLayout>

in View Model

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    Color color;
    public Color BGcolor
    {
        get { return color; }

        set
        {

            if (color != value)
            {
                color = value;
                NotifyPropertyChanged("BGcolor");
            }

        }
    }


    bool refresh;


    public bool Refresh
    {
        get { return refresh; }
        set
        {
            if (refresh != value)
            {
                refresh = value;
                NotifyPropertyChanged("Refresh");
            }

        }
    }



    public ICommand ClickCommand { get; set; }

    public MyViewModel()
    {
        BGcolor = Color.LightPink;


        ClickCommand = new Command(()=> {


            BGcolor = Color.Red;

        });


    }



}

in Custom Renderer


using System.ComponentModel;
using Android.Content;

using xxx;
using xxx.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly:ExportRenderer(typeof(MyEntry),typeof(NyEntryRenderer))]
namespace xxx.Droid
{
    public class NyEntryRenderer : EntryRenderer
    {
        public NyEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {
                Element.TextChanged += Element_TextChanged;
            }

        }

        private void Element_TextChanged(object sender, TextChangedEventArgs e)
        {
            // var content = Element.Text;
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == MyEntry.BackgroundColorProperty.PropertyName)
            {
                //  will been invoked when click button
            }


        }
    }
}



回答2:


Make your view model like this.

public class YourViewModel

  {
    public Command command
    {
        get
        {
            return new Command(() => {

               //Change here button background colors
               BackgroundColor = Color.Green; 
            });
        }
    }


    private _backgroundColor = Color.White;
    public Color BackgroundColor
    {
        get { return _backgroundColor;}
        set
        {
             if (value == _backgroundColor)
                 return;

             _backgroundColor = value;
             NotifyOnPropertyChanged(nameof(BackgroundColor));
         }

}

}

Your XAML

 <local:MyEntry Text="{Binding Password}" Placeholder="Enter"  />

<Button Text="send" Command="{Binding command}" BackgroundColor="{Binding BackgroundColor}"></Button>


来源:https://stackoverflow.com/questions/60778529/change-of-color-de-my-custom-enter-once-is-clicked-my-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!