Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Access ViewModel inside CustomRenderer

$
0
0

Hello everyone :)

I'm trying to implement a long press behavior on a switch control. I have a custom switch renderer in which I use the "Long click" event:

[assembly: ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]

namespace Version1.Droid
{
    public class CustomSwitchRenderer : SwitchRenderer
    {
        public CustomSwitchRenderer(Context context): base(context) {}

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Switch> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                Control.LongClick += Control_LongClick;
            }
        }

        private async void Control_LongClick(object sender, LongClickEventArgs e)
        {
            var answer = await App.Current.MainPage.DisplayAlert("Title", "Blablablabla?", "Yes", "No");
            if (answer)
            {
                //Access binding context of the switch ???
            }
        }
    }
}

How could I access the binding context of the switch inside the LongClick handler ?
I was thinking using the messaging center but I couldn't find any similar example.

Thank you in advance for any help!
Charles


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>