编写测试代码
This commit is contained in:
@@ -14,8 +14,8 @@
|
||||
</local:WindowDecorator>
|
||||
</local:ExMarkup.WindowChrome>
|
||||
</Border>
|
||||
<Grid>
|
||||
|
||||
</Grid>
|
||||
<AdornerDecorator x:Name="decorator">
|
||||
<Button Content="打开抽屉" Click="Button_Click"/>
|
||||
</AdornerDecorator>
|
||||
</DockPanel>
|
||||
</Window>
|
||||
|
||||
@@ -25,5 +25,10 @@ namespace Deedy
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DrawerManager.ShowDrawer(this.decorator, new Border() { Background = Brushes.Red, MinWidth = 400, MinHeight = 400, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch });
|
||||
}
|
||||
}
|
||||
}
|
||||
128
DeedyDesigner/Deedy.Testing/SlideDrawerAdorner.cs
Normal file
128
DeedyDesigner/Deedy.Testing/SlideDrawerAdorner.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace Deedy
|
||||
{
|
||||
/// <summary>
|
||||
/// 简化的蒙层装饰器
|
||||
/// </summary>
|
||||
public class SlideDrawerAdorner : Adorner
|
||||
{
|
||||
private readonly VisualCollection _visuals;
|
||||
private readonly FrameworkElement _content;
|
||||
private readonly Border _mask;
|
||||
|
||||
public SlideDrawerAdorner(UIElement adornedElement, FrameworkElement content, double maskOpacity = 0.5)
|
||||
: base(adornedElement)
|
||||
{
|
||||
_visuals = new VisualCollection(this);
|
||||
|
||||
// 创建蒙层
|
||||
_mask = new Border
|
||||
{
|
||||
Background = Brushes.Black,
|
||||
Opacity = maskOpacity
|
||||
};
|
||||
_mask.MouseLeftButtonDown += (s, e) => CloseRequested?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
// 添加内容
|
||||
_content = content;
|
||||
|
||||
_visuals.Add(_mask);
|
||||
//_visuals.Add(_content);
|
||||
_mask.Child = _content;
|
||||
}
|
||||
|
||||
public event EventHandler? CloseRequested;
|
||||
|
||||
protected override int VisualChildrenCount => _visuals.Count;
|
||||
protected override Visual GetVisualChild(int index) => _visuals[index];
|
||||
|
||||
protected override Size ArrangeOverride(Size finalSize)
|
||||
{
|
||||
// 蒙层覆盖整个区域
|
||||
_mask.Arrange(new Rect(finalSize));
|
||||
|
||||
// 内容初始位置在左侧外部
|
||||
_content.Arrange(new Rect(-_content.DesiredSize.Width, 0,
|
||||
_content.DesiredSize.Width, finalSize.Height));
|
||||
return finalSize;
|
||||
}
|
||||
|
||||
public void SlideIn(TimeSpan duration)
|
||||
{
|
||||
var transform = new TranslateTransform();
|
||||
_content.RenderTransform = transform;
|
||||
|
||||
var animation = new DoubleAnimation
|
||||
{
|
||||
To = 0,
|
||||
Duration = duration,
|
||||
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
|
||||
};
|
||||
|
||||
transform.BeginAnimation(TranslateTransform.XProperty, animation);
|
||||
}
|
||||
|
||||
public void SlideOut(TimeSpan duration, Action? onCompleted = null)
|
||||
{
|
||||
var transform = _content.RenderTransform as TranslateTransform;
|
||||
if (transform == null) return;
|
||||
|
||||
var animation = new DoubleAnimation
|
||||
{
|
||||
To = -_content.DesiredSize.Width,
|
||||
Duration = duration,
|
||||
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn }
|
||||
};
|
||||
|
||||
if (onCompleted != null)
|
||||
animation.Completed += (s, e) => onCompleted();
|
||||
|
||||
transform.BeginAnimation(TranslateTransform.XProperty, animation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 简化的抽屉管理器
|
||||
/// </summary>
|
||||
public static class DrawerManager
|
||||
{
|
||||
public static void ShowDrawer(UIElement parentElement, FrameworkElement drawerContent, double maskOpacity = 0.5)
|
||||
{
|
||||
var layer = AdornerLayer.GetAdornerLayer(parentElement);
|
||||
if (layer == null) return;
|
||||
|
||||
var adorner = new SlideDrawerAdorner(parentElement, drawerContent, maskOpacity);
|
||||
adorner.CloseRequested += (s, e) => HideDrawer(parentElement);
|
||||
|
||||
layer.Add(adorner);
|
||||
adorner.SlideIn(TimeSpan.FromMilliseconds(300));
|
||||
}
|
||||
|
||||
public static void HideDrawer(UIElement parentElement)
|
||||
{
|
||||
var layer = AdornerLayer.GetAdornerLayer(parentElement);
|
||||
if (layer == null) return;
|
||||
|
||||
var adorners = layer.GetAdorners(parentElement);
|
||||
if (adorners == null) return;
|
||||
|
||||
foreach (var adorner in adorners)
|
||||
{
|
||||
if (adorner is SlideDrawerAdorner slideAdorner)
|
||||
{
|
||||
slideAdorner.SlideOut(TimeSpan.FromMilliseconds(250), () =>
|
||||
{
|
||||
layer.Remove(slideAdorner);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
DeedyDesigner/Deedy.Testing/SlideViewer.cs
Normal file
19
DeedyDesigner/Deedy.Testing/SlideViewer.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace Deedy.Testing
|
||||
{
|
||||
public class SlideViewer : Adorner
|
||||
{
|
||||
public SlideViewer(UIElement adornedElement) : base(adornedElement)
|
||||
{
|
||||
//「AdornedElement」=「adornedElement」
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user