private static CompositionGradientBrush CreateCompositionGradientBrush(GradientBrush gradientBrush, Compositor compositor) { CompositionGradientBrush compositionBrush; if (gradientBrush is LinearGradientBrush linearGradient) { CompositionLinearGradientBrush compositionLinearGradientBrush = compositor.CreateLinearGradientBrush(); compositionLinearGradientBrush.StartPoint = linearGradient.StartPoint.ToVector2(); compositionLinearGradientBrush.EndPoint = linearGradient.EndPoint.ToVector2(); compositionBrush = compositionLinearGradientBrush; } else { return(null); } compositionBrush.RelativeTransformMatrix = gradientBrush.RelativeTransform?.MatrixCore ?? Matrix3x2.Identity; compositionBrush.ExtendMode = ConvertGradientExtendMode(gradientBrush.SpreadMethod); compositionBrush.MappingMode = ConvertBrushMappingMode(gradientBrush.MappingMode); ConvertGradientColorStops(compositor, compositionBrush, gradientBrush.GradientStops, gradientBrush.Opacity); return(compositionBrush); }
private static IDisposable AssignAndObserveGradientBrush(GradientBrush gradientBrush, Compositor compositor, BrushSetterHandler brushSetter) { var disposables = new CompositeDisposable(); var compositionBrush = CreateCompositionGradientBrush(gradientBrush, compositor); if (gradientBrush is LinearGradientBrush linearGradient) { var clgb = (CompositionLinearGradientBrush)compositionBrush; gradientBrush.RegisterDisposablePropertyChangedCallback( LinearGradientBrush.StartPointProperty, (s, e) => clgb.StartPoint = (Vector2)e.NewValue ) .DisposeWith(disposables); gradientBrush.RegisterDisposablePropertyChangedCallback( LinearGradientBrush.EndPointProperty, (s, e) => clgb.EndPoint = (Vector2)e.NewValue ) .DisposeWith(disposables); } gradientBrush.RegisterDisposablePropertyChangedCallback( GradientBrush.GradientStopsProperty, (s, e) => ConvertGradientColorStops( compositionBrush.Compositor, compositionBrush, (GradientStopCollection)e.NewValue, ((GradientBrush)s).Opacity) ) .DisposeWith(disposables); gradientBrush.RegisterDisposablePropertyChangedCallback( GradientBrush.MappingModeProperty, (s, e) => compositionBrush.MappingMode = (CompositionMappingMode)e.NewValue ) .DisposeWith(disposables); gradientBrush.RegisterDisposablePropertyChangedCallback( GradientBrush.OpacityProperty, (s, e) => ConvertGradientColorStops( compositionBrush.Compositor, compositionBrush, ((GradientBrush)s).GradientStops, (double)e.NewValue) ) .DisposeWith(disposables); gradientBrush.RegisterDisposablePropertyChangedCallback( GradientBrush.SpreadMethodProperty, (s, e) => compositionBrush.ExtendMode = ConvertGradientExtendMode((GradientSpreadMethod)e.NewValue) ) .DisposeWith(disposables); gradientBrush.RegisterDisposablePropertyChangedCallback( GradientBrush.RelativeTransformProperty, (s, e) => compositionBrush.RelativeTransformMatrix = ((Transform)e.NewValue)?.MatrixCore ?? Matrix3x2.Identity ) .DisposeWith(disposables); brushSetter(compositionBrush); return(disposables); }