Articles in this section
Category / Section

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

1 min read

 

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

 

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied