maui-linux/System.Maui.Platform.UAP/ProgressBarRenderer.cs

98 строки
2.3 KiB
C#

using System.ComponentModel;
using global::Windows.UI.Xaml;
using global::Windows.UI.Xaml.Controls.Primitives;
using System.Maui.Internals;
namespace System.Maui.Platform.UWP
{
public class ProgressBarRenderer : ViewRenderer<ProgressBar, global::Windows.UI.Xaml.Controls.ProgressBar>
{
object _foregroundDefault;
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (Control != null)
{
Control.ValueChanged -= ProgressBarOnValueChanged;
Control.Loaded -= OnControlLoaded;
}
}
base.Dispose(disposing);
}
protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
if (Control != null)
{
Control.Loaded -= OnControlLoaded;
}
}
if (e.NewElement != null)
{
if (Control == null)
{
var progressBar = new global::Windows.UI.Xaml.Controls.ProgressBar { Minimum = 0, Maximum = 1 };
progressBar.ValueChanged += ProgressBarOnValueChanged;
SetNativeControl(progressBar);
Control.Loaded += OnControlLoaded;
}
Control.Value = e.NewElement.Progress;
UpdateFlowDirection();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName)
Control.Value = Element.Progress;
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection();
else if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName)
UpdateProgressColor();
}
void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_foregroundDefault = Control.GetForegroundCache();
UpdateProgressColor();
}
void UpdateProgressColor()
{
Color color = Element.ProgressColor;
if (color.IsDefault)
{
Control.RestoreForegroundCache(_foregroundDefault);
}
else
{
Control.Foreground = color.ToBrush();
}
}
void ProgressBarOnValueChanged(object sender, RangeBaseValueChangedEventArgs rangeBaseValueChangedEventArgs)
{
((IVisualElementController)Element)?.InvalidateMeasure(InvalidationTrigger.MeasureChanged);
}
void UpdateFlowDirection()
{
Control.UpdateFlowDirection(Element);
}
}
}