Replace some BindSize calls with RelativeSizeAdjustment

This commit is contained in:
Sergio Pedri 2021-11-25 14:16:40 +01:00
Родитель a6296043b6
Коммит c99142919b
3 изменённых файлов: 18 добавлений и 8 удалений

Просмотреть файл

@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Numerics;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;
@ -48,10 +50,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Media
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance for the current event.</param>
private static async void OnVisualFactoryPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = (UIElement)d;
var attachedVisual = await ((AttachedVisualFactoryBase)e.NewValue).GetAttachedVisualAsync(element);
UIElement element = (UIElement)d;
Visual attachedVisual = await ((AttachedVisualFactoryBase)e.NewValue).GetAttachedVisualAsync(element);
attachedVisual.BindSize(element);
attachedVisual.RelativeSizeAdjustment = Vector2.One;
ElementCompositionPreview.SetElementChildVisual(element, attachedVisual);
}

Просмотреть файл

@ -18,11 +18,11 @@ namespace Microsoft.Toolkit.Uwp.UI.Media
internal static class CompositionObjectExtensions
{
/// <summary>
/// Starts an <see cref="ExpressionAnimation"/> to keep the size of the source <see cref="CompositionObject"/> in sync with the target <see cref="UIElement"/>
/// Starts an <see cref="ExpressionAnimation"/> to keep the size of the source <see cref="Visual"/> in sync with the target <see cref="UIElement"/>
/// </summary>
/// <param name="source">The <see cref="CompositionObject"/> to start the animation on</param>
/// <param name="source">The <see cref="Visual"/> to start the animation on</param>
/// <param name="target">The target <see cref="UIElement"/> to read the size updates from</param>
public static void BindSize(this CompositionObject source, UIElement target)
public static void BindSize(this Visual source, UIElement target)
{
var visual = ElementCompositionPreview.GetElementVisual(target);
var bindSizeAnimation = source.Compositor.CreateExpressionAnimation($"{nameof(visual)}.Size");

Просмотреть файл

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.UI.Animations;
using Windows.Graphics.Effects;
@ -188,7 +189,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Media.Pipelines
/// <returns>A <see cref="Task{T}"/> that returns the final <see cref="SpriteVisual"/> instance to use</returns>
public async Task<SpriteVisual> AttachAsync(UIElement target, UIElement reference = null)
{
var visual = Window.Current.Compositor.CreateSpriteVisual();
SpriteVisual visual = Window.Current.Compositor.CreateSpriteVisual();
visual.Brush = await BuildAsync();
@ -196,7 +197,14 @@ namespace Microsoft.Toolkit.Uwp.UI.Media.Pipelines
if (reference != null)
{
visual.BindSize(reference);
if (reference == target)
{
visual.RelativeSizeAdjustment = Vector2.One;
}
else
{
visual.BindSize(reference);
}
}
return visual;