2017-06-20 6 views
1

J'ai fait une personnalisation de la barre de progression (renderer personnalisé) pour changer la barre de progression couleur dans iOS et Droid comme on le voit dans lePersonnalisation de la couleur de la barre de progression dans UWP en utilisant Xamarin Forms

classe barre de progression personnalisée dans la PCL suivante:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace App2 
{ 
    public class ColorProgressBar : ProgressBar 
    { 
     public static BindableProperty BarColorProperty 
      = BindableProperty.Create<ColorProgressBar, Color>(p => 
        p.BarColor, default(Color)); 

     public Color BarColor 
      { 
      get { return (Color)GetValue(BarColorProperty); } 
      set { SetValue(BarColorProperty, value); } 
      } 
    } 
    } 

sur mesure pour iOS renderer:

using App2; 
using App2.iOS; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using Xamarin.Forms; 

//using MonoTouch.Foundation; 
//using MonoTouch.UIKit; 
using Xamarin.Forms.Platform.iOS; 

[assembly: ExportRenderer(typeof(ColorProgressBar), 
typeof(ColorProgressBarRenderer))] 

namespace App2.iOS 
{ 
    public class ColorProgressBarRenderer : ProgressBarRenderer 
    { 
     public ColorProgressBarRenderer() 
     { } 

     protected override void 
     OnElementChanged(ElementChangedEventArgs<ProgressBar> e) 
     { 
     base.OnElementChanged(e); 

     if (e.NewElement == null) 
      return; 

     if (Control != null) 
     { 
      UpdateBarColor(); 
     } 
     } 

     protected override void OnElementPropertyChanged(object sender, 
     PropertyChangedEventArgs e) 
     { 
      base.OnElementPropertyChanged(sender, e); 

      if (e.PropertyName == 
       ColorProgressBar.BarColorProperty.PropertyName) 
      { 
      UpdateBarColor(); 
      } 
     } 
     private void UpdateBarColor() 
     { 
      var element = Element as ColorProgressBar; 
      Control.TintColor = element.BarColor.ToUIColor(); 
     } 
     } 
    } 

sur mesure pour Android renderer:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Renderscripts; 
using static Java.Util.ResourceBundle; 
using Xamarin.Forms.Platform.Android; 
using Android.Graphics; 
using System.ComponentModel; 
using Xamarin.Forms; 
using App2; 
using App2.Droid; 

[assembly: ExportRenderer(typeof(ColorProgressBar), 
         typeof(ColorProgressBarRenderer))] 
namespace App2.Droid 
{ 
    public class ColorProgressBarRenderer : ProgressBarRenderer 
    { 
     public ColorProgressBarRenderer() 
     { } 

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

        if (e.NewElement == null) 
         return; 

        if (Control != null) 
        { 
         UpdateBarColor(); 
        } 
       } 

     protected override void OnElementPropertyChanged(object sender, 
     PropertyChangedEventArgs e) 
       { 
       base.OnElementPropertyChanged(sender, e); 

       if (e.PropertyName == 
         ColorProgressBar.BarColorProperty.PropertyName) 
        { 
         UpdateBarColor(); 
        } 
       } 

     private void UpdateBarColor() 
       { 
       var element = Element as ColorProgressBar; 
       // http://stackoverflow.com/a/29199280 
       Control.IndeterminateDrawable.SetColorFilter( 
       element.BarColor.ToAndroid(), PorterDuff.Mode.SrcIn); 
       Control.ProgressDrawable.SetColorFilter( 
       element.BarColor.ToAndroid(), PorterDuff.Mode.SrcIn); 
       } 
    } 
} 

J'installe la couleur de la barre de progression personnalisée de cette façon:

var progressBar = new ColorProgressBar(); 
progressBar.BarColor = Color.Red; 

Je ne comprends pas comment faire une classe renderer personnalisée pour UWP qui change la couleur de la barre de progression. Quelqu'un pourrait m'aider s'il vous plaît à comprendre comment faire ce cours?

Répondre

1

Vous allez vouloir mettre à jour la propriété Foreground du contrôle natif Windows.UI.Xaml.Controls.ProgressBar pour changer la couleur.

Il devrait ressembler à ceci:

private void UpdateBarColor() 
{ 
    var element = Element as ColorProgressBar; 
    Control.Foreground = new Windows.UI.Xaml.Media.SolidColorBrush(
     GetWindowsColor(element.BarColor)); 
} 

Windows.UI.Color GetWindowsColor(Color color) 
{ 
    return Windows.UI.Color.FromArgb((byte)(255 * color.A), (byte)(255 * color.R), (byte)(255 * color.G), (byte)(255 * color.B)); 
} 

Cela prendra votre BarColor, l'utiliser pour faire un SolidColorBrush de la bonne couleur, puis attribuez-lui que votre commande natif ProgressBar.

+0

cela fonctionne pour Windows Phone merci beaucoup (^_^) – paula

+0

@paula Pas de problème! Veillez à marquer cette solution comme la réponse si elle a répondu à votre question. –