maui-linux/Xamarin.Forms.Platform.UAP/ActivityIndicatorRenderer.cs

63 строки
1.6 KiB
C#

using System.ComponentModel;
using Windows.UI.Xaml;
namespace Xamarin.Forms.Platform.UWP
{
public class ActivityIndicatorRenderer : ViewRenderer<ActivityIndicator, FormsProgressBar>
{
object _foregroundDefault;
protected override void OnElementChanged(ElementChangedEventArgs<ActivityIndicator> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
SetNativeControl(new FormsProgressBar { IsIndeterminate = true, Style = Windows.UI.Xaml.Application.Current.Resources["FormsProgressBarStyle"] as Windows.UI.Xaml.Style });
Control.Loaded += OnControlLoaded;
}
// UpdateColor() called when loaded to ensure we can cache dynamic default colors
UpdateIsRunning();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ActivityIndicator.IsRunningProperty.PropertyName || e.PropertyName == VisualElement.OpacityProperty.PropertyName)
UpdateIsRunning();
else if (e.PropertyName == ActivityIndicator.ColorProperty.PropertyName)
UpdateColor();
}
void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_foregroundDefault = Control.GetForegroundCache();
UpdateColor();
}
void UpdateColor()
{
Color color = Element.Color;
if (color.IsDefault)
{
Control.RestoreForegroundCache(_foregroundDefault);
}
else
{
Control.Foreground = color.ToBrush();
}
}
void UpdateIsRunning()
{
Control.ElementOpacity = Element.IsRunning ? Element.Opacity : 0;
}
}
}