简体   繁体   中英

How to use CustomRenderer for a single ContentPage in Xamarin.iOS and Xamarin.Android

I need to change the height of the Progress bar but just for a specific ContentPage, not for all my ContentPage's. I got the code where i change the progress bar height:

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(
    ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        Control.ProgressTintColor = Color.FromRgb(0, 0, 0).ToUIColor();// This changes the color of the progress
    }


    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        var X = 1.0f;
        var Y = 10.0f; // This changes the height

        CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
        Control.Transform = transform;
    }
}

i'd like to know if it is really possible. Thanks in advance.

If your App navigation is:

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(
    ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        if (Shell.Current.CurrentPage.GetType().Name.Equals("YourPageNme"))
        {
             Control.ProgressTintColor = Color.FromRgb(0, 0, 0).ToUIColor();
        }
    }


    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        if (Shell.Current.CurrentPage.GetType().Name.Equals("YourPageNme"))
        {
             var X = 1.0f;
             var Y = 10.0f; // This changes the height

             CGAffineTransform transform = CGAffineTransform.MakeScale(X, Y);
             Control.Transform = transform;
        }
    }
}

Replace

   Shell.Current.CurrentPage?.GetType().Name.Equals("YourPageNme")

With

(App.Current.MainPage as NavigationPage)?.CurrentPage?.GetType().Name?.Equals("YourPageNme")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM