Изменение цвета де мой пользовательский ввод один раз нажал мою кнопку - PullRequest
1 голос
/ 20 марта 2020

я работаю с представленной записью custon, мне нужно услышать от xaml в моем пользовательском рендере, когда я нажал кнопку

у меня есть этот код в моем xaml

<local:MyEntry   eventRefresh="true">

, когда я нажал моя кнопка, эта функция активна

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

, но мой EntryRendered не слышит изменения

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

Ответы [ 2 ]

1 голос
/ 23 марта 2020

Вы должны определить свойство eventRefre sh как привязываемое свойство.

в своей пользовательской записи

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()
        {

        }



    }
}

в xaml

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


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

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


</StackLayout>

в 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;

        });


    }



}

в 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
            }


        }
    }
}
0 голосов
/ 21 марта 2020

Создайте свою модель просмотра следующим образом.

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));
         }

}

}

Ваш XAML

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

<Button Text="send" Command="{Binding command}" BackgroundColor="{Binding BackgroundColor}"></Button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...