This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 514
/
HorizontalRepeaterView.xaml.cs
201 lines (175 loc) · 7.22 KB
/
HorizontalRepeaterView.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) Richasy. All rights reserved.
using Bili.ViewModels.Uwp.Core;
using Splat;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
namespace Bili.App.Controls
{
/// <summary>
/// 水平列表视图.
/// </summary>
public sealed partial class HorizontalRepeaterView : UserControl
{
/// <summary>
/// <see cref="ItemsSource"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(HorizontalRepeaterView), new PropertyMetadata(null));
/// <summary>
/// <see cref="WideItemTemplate"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty WideItemTemplateProperty =
DependencyProperty.Register(nameof(WideItemTemplate), typeof(DataTemplate), typeof(HorizontalRepeaterView), new PropertyMetadata(null));
/// <summary>
/// <see cref="NarrowItemTemplate"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty NarrowItemTemplateProperty =
DependencyProperty.Register(nameof(NarrowItemTemplate), typeof(DataTemplate), typeof(HorizontalRepeaterView), new PropertyMetadata(null));
/// <summary>
/// <see cref="Header"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register(nameof(Header), typeof(string), typeof(HorizontalRepeaterView), new PropertyMetadata(string.Empty));
/// <summary>
/// <see cref="HeaderVisibility"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty HeaderVisibilityProperty =
DependencyProperty.Register(nameof(HeaderVisibility), typeof(Visibility), typeof(HorizontalRepeaterView), new PropertyMetadata(Visibility.Collapsed));
/// <summary>
/// <see cref="NarrowHeight"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty NarrowHeightProperty =
DependencyProperty.Register(nameof(NarrowHeight), typeof(double), typeof(HorizontalRepeaterView), new PropertyMetadata(128d));
/// <summary>
/// <see cref="AdditionalContent"/>的依赖属性.
/// </summary>
public static readonly DependencyProperty AdditionalContentProperty =
DependencyProperty.Register(nameof(AdditionalContent), typeof(object), typeof(HorizontalRepeaterView), new PropertyMetadata(null));
/// <summary>
/// Initializes a new instance of the <see cref="BannerView"/> class.
/// </summary>
public HorizontalRepeaterView()
{
InitializeComponent();
SizeChanged += OnSizeChanged;
Loaded += OnLoaded;
}
/// <summary>
/// 数据源.
/// </summary>
public object ItemsSource
{
get { return (object)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
/// <summary>
/// 宽视图下的条目模板.
/// </summary>
public DataTemplate WideItemTemplate
{
get { return (DataTemplate)GetValue(WideItemTemplateProperty); }
set { SetValue(WideItemTemplateProperty, value); }
}
/// <summary>
/// 窄视图下的条目模板.
/// </summary>
public DataTemplate NarrowItemTemplate
{
get { return (DataTemplate)GetValue(NarrowItemTemplateProperty); }
set { SetValue(NarrowItemTemplateProperty, value); }
}
/// <summary>
/// 标题.
/// </summary>
public string Header
{
get { return (string)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
/// <summary>
/// 标题可见性.
/// </summary>
public Visibility HeaderVisibility
{
get { return (Visibility)GetValue(HeaderVisibilityProperty); }
set { SetValue(HeaderVisibilityProperty, value); }
}
/// <summary>
/// 窄视图下的高度.
/// </summary>
public double NarrowHeight
{
get { return (double)GetValue(NarrowHeightProperty); }
set { SetValue(NarrowHeightProperty, value); }
}
/// <summary>
/// 顶部附加内容.
/// </summary>
public object AdditionalContent
{
get { return (object)GetValue(AdditionalContentProperty); }
set { SetValue(AdditionalContentProperty, value); }
}
/// <inheritdoc/>
protected override void OnPointerEntered(PointerRoutedEventArgs e) => CheckOffsetButtonStatus();
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
CheckOffsetButtonStatus();
CheckLayout();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
CheckOffsetButtonStatus();
CheckLayout();
}
private void CheckOffsetButtonStatus()
{
if (WideScrollViewer.ExtentWidth <= WideScrollViewer.ViewportWidth)
{
LeftOffsetButton.Visibility = RightOffsetButton.Visibility = Visibility.Collapsed;
}
else
{
LeftOffsetButton.Visibility = WideScrollViewer.HorizontalOffset == 0 ? Visibility.Collapsed : Visibility.Visible;
RightOffsetButton.Visibility = WideScrollViewer.ScrollableWidth - WideScrollViewer.HorizontalOffset > 0 ? Visibility.Visible : Visibility.Collapsed;
}
}
private void CheckLayout()
{
var windowWidth = Window.Current.Bounds.Width;
if (windowWidth < Splat.Locator.Current.GetService<AppViewModel>().NarrowWindowThresholdWidth && NarrowItemTemplate != null)
{
WideContainer.Visibility = Visibility.Collapsed;
NarrowContainer.Visibility = Visibility.Visible;
}
else
{
WideContainer.Visibility = Visibility.Visible;
NarrowContainer.Visibility = Visibility.Collapsed;
}
}
private void OnLeftOffsetButtonClick(object sender, RoutedEventArgs e)
{
var leftOffset = WideScrollViewer.HorizontalOffset - WideScrollViewer.ViewportWidth;
if (leftOffset < 0)
{
leftOffset = 0;
}
WideScrollViewer.ChangeView(leftOffset, 0, 1);
}
private void OnRightOffsetButtonClick(object sender, RoutedEventArgs e)
{
var rightOffset = WideScrollViewer.HorizontalOffset + WideScrollViewer.ViewportWidth;
if (rightOffset > WideScrollViewer.ExtentWidth)
{
rightOffset = WideScrollViewer.ScrollableWidth - WideScrollViewer.HorizontalOffset;
}
WideScrollViewer.ChangeView(rightOffset, 0, 1);
}
private void OnWideScrollViewerChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
CheckOffsetButtonStatus();
}
}
}