Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
完成WaterfallContentPanel,未测试
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Apr 12, 2019
1 parent 558d295 commit e4960f7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
82 changes: 82 additions & 0 deletions PixivFSUWP/Controls/WaterfallContentPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace PixivFSUWP.Controls
{
public class WaterfallContentPanel : Panel
{
//此属性决定瀑布流列数
public static readonly DependencyProperty ColumsProperty =
DependencyProperty.Register("Colums", typeof(int),
typeof(WaterfallContentPanel), new PropertyMetadata(2,
(DepObj, e) =>
{
(DepObj as WaterfallContentPanel).InvalidateMeasure();
(DepObj as WaterfallContentPanel).InvalidateArrange();
}));

public int Colums
{
get => (int)GetValue(ColumsProperty);
set => SetValue(ColumsProperty, value);
}

//此属性决定项目间隔
public static readonly DependencyProperty ItemMarginProperty =
DependencyProperty.Register("ItemMargin", typeof(double),
typeof(WaterfallContentPanel), new PropertyMetadata(0,
(DepObj, e) =>
{
(DepObj as WaterfallContentPanel).InvalidateMeasure();
(DepObj as WaterfallContentPanel).InvalidateArrange();
}));

public double ItemMargin
{
get => (double)GetValue(ItemMarginProperty);
set => SetValue(ItemMarginProperty, value);
}

//测量panel需要的空间
//宽度填满,高度进行计算
protected override Size MeasureOverride(Size availableSize)
{
Size toret = new Size();
List<double> heights = new List<double>(Colums);
toret.Width = availableSize.Width;
double itemwidth = (availableSize.Width - ItemMargin * (Colums - 1)) / Colums;
foreach (var i in Children)
{
i.Measure(new Size(itemwidth, double.PositiveInfinity));
heights[heights.IndexOf(heights.Min())] += ItemMargin + i.DesiredSize.Height;
}
toret.Height = heights.Max();
return toret;
}

//排版,不改变大小
protected override Size ArrangeOverride(Size finalSize)
{
List<double> Xs = new List<double>();
List<double> Ys = new List<double>();
for (int i = 0; i < Colums; i++)
{
Xs.Add(i * (DesiredSize.Width + ItemMargin) / Colums);
Ys.Add(0);
}
foreach (var i in Children)
{
var minC = Xs.IndexOf(Xs.Min());
i.Arrange(new Rect(Xs[minC], Ys[minC], i.DesiredSize.Width, i.DesiredSize.Height));
Ys[minC] += i.DesiredSize.Height + ItemMargin;
}
return finalSize;
}
}
}
9 changes: 8 additions & 1 deletion PixivFSUWP/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PixivFSUWP"
xmlns:viewmodels="using:PixivFSUWP.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">


<Page.Transitions>
<TransitionCollection>
<NavigationThemeTransition />
</TransitionCollection>
</Page.Transitions>

<Grid>

</Grid>
Expand Down
1 change: 1 addition & 0 deletions PixivFSUWP/PixivFSUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\WaterfallContentPanel.cs" />
<Compile Include="Data\OverAll.cs" />
<Compile Include="Data\WaterfallItem.cs" />
<Compile Include="LoginPage.xaml.cs">
Expand Down

0 comments on commit e4960f7

Please sign in to comment.