We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to handle ValueChange event in SfRating control of Xamarin.iOS platform

Platform: Xamarin.iOS |
Control: SfRating


Handling ValueChange event in SfRating


In latest 2017 Volume 2, we have removed Delegate API and SFRatingDelegate class from SfRating control. Here we have explained how to hook the events using event handler instead of delegates.


The below codes explain how to hook the event using delegate class in our old implementations. And below that steps also provided how to handle new event handler from old delegates.


namespace DelegateToEvent{    public partial class ViewController : UIViewController    {        protected ViewController(IntPtr handle) : base(handle)        {            // Note: this .ctor should not contain any initialization logic.        }        public override void ViewDidLoad()        {            base.ViewDidLoad();            SFRating rating = new SFRating();            rating.Frame = new CoreGraphics.CGRect(10, 50, 200, 75);            rating.Delegate = new RatingDelegate();            this.View.AddSubview(rating);        }        public override void DidReceiveMemoryWarning()        {            base.DidReceiveMemoryWarning();            // Release any cached data, images, etc that aren't in use.        }    }    public class RatingDelegate : SFRatingDelegate    {         public override void DidValueChanged(SFRating sfRating, nfloat value)        {            nfloat ratedValue = value;        }    }}



Step 1:Include the ValueChanged Event handler as follows.

namespace DelegateToEvent{    public partial class ViewController : UIViewController    {        protected ViewController(IntPtr handle) : base(handle)        {            // Note: this .ctor should not contain any initialization logic.        }        public override void ViewDidLoad()        {            base.ViewDidLoad();            SFRating rating = new SFRating();            rating.Frame = new CoreGraphics.CGRect(10, 50, 200, 75);            //Rating ValueChanged event            rating.ValueChanged += (sender, e) =>             {             };
            rating.Delegate = new RatingDelegate(); 

            this.View.AddSubview(rating);        }        public override void DidReceiveMemoryWarning()        {            base.DidReceiveMemoryWarning();            // Release any cached data, images, etc that aren't in use.        }    }

    public class RatingDelegate : SFRatingDelegate    {         public override void DidValueChanged(SFRating sfRating, nfloat value)        {            nfloat ratedValue = value;        }    } 



Step 2: Include the business logics inside the new event handler which you have written in delegate class.

namespace DelegateToEvent{    public partial class ViewController : UIViewController    {        protected ViewController(IntPtr handle) : base(handle)        {            // Note: this .ctor should not contain any initialization logic.        }        public override void ViewDidLoad()        {            base.ViewDidLoad();            SFRating rating = new SFRating();            rating.Frame = new CoreGraphics.CGRect(10, 50, 200, 75);            //Rating ValueChanged event            rating.ValueChanged += (sender, e) =>            {                nfloat ratedValue = e.Value;            };            rating.Delegate = new RatingDelegate();            this.View.AddSubview(rating);        }        public override void DidReceiveMemoryWarning()        {            base.DidReceiveMemoryWarning();            // Release any cached data, images, etc that aren't in use.        }    }    public class RatingDelegate : SFRatingDelegate    {         public override void DidValueChanged(SFRating sfRating, nfloat value)        {            nfloat ratedValue = value;        }    }}



Step 3: Remove the commented Delegate API and the Delegate class from the sample.

namespace DelegateToEvent{    public partial class ViewController : UIViewController    {        protected ViewController(IntPtr handle) : base(handle)        {            // Note: this .ctor should not contain any initialization logic.        }        public override void ViewDidLoad()        {            base.ViewDidLoad();            SFRating rating = new SFRating();            rating.Frame = new CoreGraphics.CGRect(10, 50, 200, 75);            //Rating ValueChanged event            rating.ValueChanged += (sender, e) =>            {                nfloat ratedValue = e.Value;            };            this.View.AddSubview(rating);        }        public override void DidReceiveMemoryWarning()        {            base.DidReceiveMemoryWarning();            // Release any cached data, images, etc that aren't in use.        }    }





2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile