在 UWP 中动态调整阴影大小
Dynamically resizing shadow in UWP
我正在为 GridView 项目添加阴影。下面的代码就是这样做的,但是我的 GridView 项目可以根据 window 大小调整大小。并且阴影不会随之调整大小。我明白为什么它没有,因为我只是在阴影上设置了一个静态大小,但我不知道如何根据它后面的网格调整阴影的大小。
这是我的代码:
private void Item_Loaded(object sender, RoutedEventArgs e)
{
var root = (UIElement)sender;
InitializeShadowAnimation(root, FindVisualChild<Canvas>(root));
}
private void InitializeShadowAnimation(UIElement root, UIElement shadowHost)
{
var rootVisual = ElementCompositionPreview.GetElementVisual(root);
var shadowHostVisual = ElementCompositionPreview.GetElementVisual(shadowHost);
var compositor = shadowHostVisual.Compositor;
var shadow = compositor.CreateDropShadow();
shadow.BlurRadius = 35f;
shadow.Offset = new Vector3(15, 15, 5);
var shadowVisual = compositor.CreateSpriteVisual();
shadowVisual.Shadow = shadow;
var size = new Vector2(0, 0);
if (root is FrameworkElement element)
{
size = new Vector2((float)element.ActualWidth - 50, (float)element.ActualHeight - 50);
}
shadowVisual.Size = size;
ElementCompositionPreview.SetElementChildVisual(shadowHost, shadowVisual);
}
这里是数据模板对应的xaml代码:
<DataTemplate x:Key="CardViewItemTemplate" x:DataType="data:MyData">
<Grid x:Name="MainGrid" Loaded="Item_Loaded" Tag="{Binding}" >
<Canvas x:Name="ShadowContainer"/>
</Grid>
</DataTemplate>
提前致谢。
我的解决方案(虽然可能不是最优雅的)是为大小更改事件添加一个方法并重置阴影。
private void Item_SizeChanged(object sender, SizeChangedEventArgs e)
{
var root = (UIElement)sender;
UIElement shadowHost = FindVisualChild<Canvas>(root);
var shadowVisual = ElementCompositionPreview.GetElementChildVisual(shadowHost);
if (shadowVisual == null)
{
return;
}
var size = new Vector2(0, 0);
if (root is FrameworkElement element)
{
size = new Vector2((float)element.ActualWidth - 50, (float)element.ActualHeight - 50);
}
shadowVisual.Size = size;
}
我正在为 GridView 项目添加阴影。下面的代码就是这样做的,但是我的 GridView 项目可以根据 window 大小调整大小。并且阴影不会随之调整大小。我明白为什么它没有,因为我只是在阴影上设置了一个静态大小,但我不知道如何根据它后面的网格调整阴影的大小。
这是我的代码:
private void Item_Loaded(object sender, RoutedEventArgs e)
{
var root = (UIElement)sender;
InitializeShadowAnimation(root, FindVisualChild<Canvas>(root));
}
private void InitializeShadowAnimation(UIElement root, UIElement shadowHost)
{
var rootVisual = ElementCompositionPreview.GetElementVisual(root);
var shadowHostVisual = ElementCompositionPreview.GetElementVisual(shadowHost);
var compositor = shadowHostVisual.Compositor;
var shadow = compositor.CreateDropShadow();
shadow.BlurRadius = 35f;
shadow.Offset = new Vector3(15, 15, 5);
var shadowVisual = compositor.CreateSpriteVisual();
shadowVisual.Shadow = shadow;
var size = new Vector2(0, 0);
if (root is FrameworkElement element)
{
size = new Vector2((float)element.ActualWidth - 50, (float)element.ActualHeight - 50);
}
shadowVisual.Size = size;
ElementCompositionPreview.SetElementChildVisual(shadowHost, shadowVisual);
}
这里是数据模板对应的xaml代码:
<DataTemplate x:Key="CardViewItemTemplate" x:DataType="data:MyData">
<Grid x:Name="MainGrid" Loaded="Item_Loaded" Tag="{Binding}" >
<Canvas x:Name="ShadowContainer"/>
</Grid>
</DataTemplate>
提前致谢。
我的解决方案(虽然可能不是最优雅的)是为大小更改事件添加一个方法并重置阴影。
private void Item_SizeChanged(object sender, SizeChangedEventArgs e)
{
var root = (UIElement)sender;
UIElement shadowHost = FindVisualChild<Canvas>(root);
var shadowVisual = ElementCompositionPreview.GetElementChildVisual(shadowHost);
if (shadowVisual == null)
{
return;
}
var size = new Vector2(0, 0);
if (root is FrameworkElement element)
{
size = new Vector2((float)element.ActualWidth - 50, (float)element.ActualHeight - 50);
}
shadowVisual.Size = size;
}