I have the simple custom button in my code:
public class CustomButton : Button
{
public bool State { get; set; } = false;
}
and its renderer:
public class CustomButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
{
public CustomButtonRenderer(Context context) : base(context) { }
ObjectAnimator objectAnimator;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var button = (Control as Android.Widget.Button);
(Control as Android.Widget.Button).Touch += Control_Touch;
// this don't works:
if (Control.State) Control.SetBackgroundColor(global::Android.Graphics.Color.LightGray);
}
}
}
And I want to get access to State
property of CustomButton
instance inside my CustomButtonRenderer
class. But I can't, because the Control
has type Android.Support.V7.Widget.AppCompatButton
, absolutly unrelated to my CustomButton
class.
Are there any ways to gain access to fields of rendered CustomButton
object inside its renderer?
Control
is the native control rendering your CustomButton
. What you are looking for is the property Element
, which represents your Xamarin.Forms CustomButton
.
Within OnElementChanged
it is available as e.NewElement
.
if (e.NewElement is CustomButton customButton
&& customButton.State)
{
Control.SetBackgroundColor(global::Android.Graphics.Color.LightGray);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.