Skip to content

Commit 4b70ebe

Browse files
committed
Merge branch 'dev' into 0.0.0.3
2 parents 1d0d1cf + b5be40c commit 4b70ebe

15 files changed

Lines changed: 779 additions & 262 deletions

File tree

src/WPFDevelopers.Net40/WPFDevelopers.Net40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
22
<Import Project="../Package.props" />
33
<PropertyGroup>
4+
<RestoreProjectStyle>None</RestoreProjectStyle>
45
<TargetFramework>net40</TargetFramework>
56
<SignAssembly>True</SignAssembly>
67
</PropertyGroup>

src/WPFDevelopers.Samples.Shared/ExampleViews/CropImageExample.xaml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@
1717
<RowDefinition Height="*" />
1818
<RowDefinition Height="Auto" />
1919
</Grid.RowDefinitions>
20-
<Grid.ColumnDefinitions>
21-
<ColumnDefinition />
22-
<ColumnDefinition />
23-
</Grid.ColumnDefinitions>
2420
<WrapPanel>
2521
<CheckBox
2622
x:Name="myCheckBox"
23+
wd:ElementHelper.CornerRadius="3"
2724
Content="开启等比缩放"
2825
IsChecked="False" />
29-
<StackPanel Orientation="Horizontal">
26+
<StackPanel Margin="10,0" Orientation="Horizontal">
3027
<TextBlock VerticalAlignment="Center" Text="裁剪区域比例:" />
3128
<Slider
3229
x:Name="myRectScaleSlider"
3330
Width="100"
31+
wd:ElementHelper.CornerRadius="3"
3432
Maximum="1"
3533
Minimum="0"
3634
Value="0.5" />
@@ -40,30 +38,23 @@
4038
<wd:CropImage
4139
Name="MyCropImage"
4240
Grid.Row="1"
43-
Grid.Column="0"
4441
IsRatioScale="{Binding ElementName=myCheckBox, Path=IsChecked}"
4542
RectScale="{Binding ElementName=myRectScaleSlider, Path=Value}"
4643
ScaleSize="2.35,1" />
47-
<Image
48-
Grid.Row="1"
49-
Grid.Column="1"
50-
Width="{Binding CurrentRect.Width, ElementName=MyCropImage}"
51-
Height="{Binding CurrentRect.Height, ElementName=MyCropImage}"
52-
VerticalAlignment="Center"
53-
Source="{Binding CurrentAreaBitmap, ElementName=MyCropImage}"
54-
Stretch="Uniform" />
5544
<StackPanel
5645
Grid.Row="2"
57-
Grid.ColumnSpan="2"
46+
Margin="0,10"
5847
HorizontalAlignment="Center"
48+
VerticalAlignment="Center"
49+
wd:PanelHelper.Spacing="3"
5950
Orientation="Horizontal">
6051
<Button
61-
Margin="0,20,10,20"
52+
wd:ElementHelper.CornerRadius="3"
6253
Click="OnImportClickHandler"
6354
Content="选择图片"
6455
Style="{StaticResource WD.PrimaryButton}" />
6556
<Button
66-
Margin="0,20,10,20"
57+
wd:ElementHelper.CornerRadius="3"
6758
Click="BtnSave_Click"
6859
Content="保存图片"
6960
Style="{StaticResource WD.SuccessPrimaryButton}" />

src/WPFDevelopers.Samples.Shared/ExampleViews/MultiSelectComboBoxExample.xaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,43 @@
132132
</wd:MultiSelectComboBox.View>
133133
</wd:MultiSelectComboBox>
134134
</Grid>
135+
<Grid HorizontalAlignment="Center" wd:PanelHelper.Spacing="3">
136+
<Grid.ColumnDefinitions>
137+
<ColumnDefinition Width="Auto" />
138+
<ColumnDefinition />
139+
</Grid.ColumnDefinitions>
140+
<TextBlock VerticalAlignment="Center" Text="证券代码:" />
141+
<wd:MultiSelectComboBox
142+
Grid.Column="1"
143+
Width="200"
144+
HorizontalAlignment="Left"
145+
wd:ElementHelper.CornerRadius="3"
146+
wd:ElementHelper.Watermark="请选择股票代码"
147+
DisplayMemberPath="StkFull"
148+
IsSelectAllActive="True"
149+
ItemsSource="{Binding StkInfoList, RelativeSource={RelativeSource AncestorType=local:MultiSelectComboBoxExample}}"
150+
SearchWatermark="请选择股票代码"
151+
ShowType="Tag">
152+
<wd:MultiSelectComboBox.View>
153+
<GridView>
154+
<GridViewColumn Width="30">
155+
<GridViewColumn.CellTemplate>
156+
<DataTemplate>
157+
<CheckBox
158+
HorizontalAlignment="Center"
159+
VerticalAlignment="Center"
160+
wd:ElementHelper.CornerRadius="3"
161+
IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}" />
162+
</DataTemplate>
163+
</GridViewColumn.CellTemplate>
164+
</GridViewColumn>
165+
<GridViewColumn DisplayMemberBinding="{Binding StkTypeName}" Header="类型" />
166+
<GridViewColumn DisplayMemberBinding="{Binding StkId}" Header="代码" />
167+
<GridViewColumn DisplayMemberBinding="{Binding StkName}" Header="名称" />
168+
</GridView>
169+
</wd:MultiSelectComboBox.View>
170+
</wd:MultiSelectComboBox>
171+
</Grid>
135172
</UniformGrid>
136173
<controls:CodeViewer.SourceCodes>
137174
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/MultiSelectComboBoxExample.xaml" CodeType="Xaml" />

src/WPFDevelopers.Samples.Shared/ExampleViews/MultiSelectComboBoxExample.xaml.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ public ObservableCollection<UserInfo> ItemsSource
2828

2929
public static readonly DependencyProperty ItemsSourceProperty =
3030
DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<UserInfo>), typeof(MultiSelectComboBoxExample), new PropertyMetadata(null));
31+
32+
33+
public ObservableCollection<StkInfo> StkInfoList
34+
{
35+
get { return (ObservableCollection<StkInfo>)GetValue(StkInfoListProperty); }
36+
set { SetValue(StkInfoListProperty, value); }
37+
}
38+
39+
public static readonly DependencyProperty StkInfoListProperty =
40+
DependencyProperty.Register("StkInfoList", typeof(ObservableCollection<StkInfo>), typeof(MultiSelectComboBoxExample), new PropertyMetadata(null));
41+
42+
3143
public MultiSelectComboBoxExample()
3244
{
3345
InitializeComponent();
@@ -44,6 +56,13 @@ private void MultiSelectComboBoxExample_Loaded(object sender, RoutedEventArgs e)
4456

4557
ItemsSource = new ObservableCollection<UserInfo>(list2);
4658
SelectedItems = new ObservableCollection<UserInfo>(list2.Where(x => x.ID == "1" || x.ID == "3"));
59+
60+
var stkInfos = new List<StkInfo>();
61+
stkInfos.Add(new StkInfo() { StkName = "平安银行", StkId = "000001", StkType = "sz", StkTypeName = "深A" });
62+
stkInfos.Add(new StkInfo() { StkName = "上证指数", StkId = "000001", StkType = "sh", StkTypeName = "指数" });
63+
stkInfos.Add(new StkInfo() { StkName = "浦发银行", StkId = "600000", StkType = "sh", StkTypeName = "沪A" });
64+
StkInfoList = new ObservableCollection<StkInfo>(stkInfos);
65+
4766
}
4867

4968
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
@@ -54,4 +73,19 @@ private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
5473
WPFDevelopers.Controls.MessageBox.Show(message, "选中内容", MessageBoxButton.OK, MessageBoxImage.Information);
5574
}
5675
}
76+
public class StkInfo
77+
{
78+
public string StkId { get; set; }
79+
public string StkName { get; set; }
80+
public string StkType { get; set; }
81+
public string StkTypeName { get; set; }
82+
public string StkFull
83+
{
84+
get
85+
{
86+
return StkType + StkId;
87+
}
88+
}
89+
90+
}
5791
}
37.2 KB
Binary file not shown.

src/WPFDevelopers.Shared/Controls/CropAvatar/CropAvatar.cs

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Diagnostics;
2+
using System.Threading.Tasks;
33
using System.Windows;
44
using System.Windows.Controls;
55
using System.Windows.Input;
@@ -16,7 +16,7 @@ namespace WPFDevelopers.Controls
1616
[TemplatePart(Name = GridTemplateName, Type = typeof(SmallPanel))]
1717
[TemplatePart(Name = ReplaceButtonTemplateName, Type = typeof(Button))]
1818
[TemplatePart(Name = AddButtonTemplateName, Type = typeof(Button))]
19-
public class CropAvatar : Control
19+
public class CropAvatar : Control, IDisposable
2020
{
2121
private const string CanvasTemplateName = "PART_Canvas";
2222
private const string ImageTemplateName = "PART_Image";
@@ -33,6 +33,8 @@ public class CropAvatar : Control
3333
private BitmapFrame _bitmapFrame;
3434
private Canvas _canvas;
3535
private CroppedBitmap _crop;
36+
private WriteableBitmap _cachedResult;
37+
private bool _disposed = false;
3638
private SmallPanel _grid;
3739
private Image _image;
3840
private int _initialX, _initialY, _voffsetX, _voffsetY;
@@ -91,8 +93,15 @@ public override void OnApplyTemplate()
9193
_addButton.Click -= OnAddButton_Click;
9294
_addButton.Click += OnAddButton_Click;
9395
}
96+
Unloaded -= OnCropAvatar_Unloaded;
97+
Unloaded += OnCropAvatar_Unloaded;
9498
}
9599

100+
private void OnCropAvatar_Unloaded(object sender, RoutedEventArgs e)
101+
{
102+
Dispose();
103+
}
104+
96105
private void OnCanvas_Loaded(object sender, RoutedEventArgs e)
97106
{
98107
if (sender is Canvas canvas)
@@ -190,15 +199,31 @@ private void OnAddButton_Click(object sender, RoutedEventArgs e)
190199

191200
private void InitialImage()
192201
{
202+
CleanupOldResources();
193203
_vNewStartX = 0;
194204
_vNewStartY = 0;
195205
var uri = Helper.ImageUri();
196206
if (uri == null) return;
197-
var bitmap = new BitmapImage();
198-
bitmap.BeginInit();
199-
bitmap.CacheOption = BitmapCacheOption.OnLoad;
200-
bitmap.UriSource = uri;
201-
bitmap.EndInit();
207+
BitmapImage bitmap = null;
208+
try
209+
{
210+
bitmap = new BitmapImage();
211+
bitmap.BeginInit();
212+
bitmap.CacheOption = BitmapCacheOption.OnLoad;
213+
bitmap.UriSource = uri;
214+
bitmap.EndInit();
215+
if (bitmap.CanFreeze)
216+
bitmap.Freeze();
217+
}
218+
catch
219+
{
220+
throw;
221+
}
222+
223+
if (bitmap == null || bitmap.Width == 0 || bitmap.Height == 0)
224+
{
225+
return;
226+
}
202227
if (bitmap.Height > bitmap.Width)
203228
{
204229
var scale = bitmap.Width / _path.Width;
@@ -253,5 +278,79 @@ private void InitialImage()
253278
}
254279
OutImageSource = _crop;
255280
}
281+
282+
private void CleanupOldResources()
283+
{
284+
if (_bitmapFrame != null)
285+
{
286+
_bitmapFrame = null;
287+
}
288+
289+
if (_crop != null)
290+
{
291+
_crop = null;
292+
}
293+
294+
#if NET40
295+
Task.Factory.StartNew(() =>
296+
{
297+
GC.Collect(2);
298+
});
299+
#else
300+
Task.Run(() =>
301+
{
302+
GC.Collect(2, GCCollectionMode.Forced);
303+
});
304+
#endif
305+
}
306+
307+
public void Dispose()
308+
{
309+
Dispose(true);
310+
GC.SuppressFinalize(this);
311+
}
312+
313+
protected virtual void Dispose(bool disposing)
314+
{
315+
if (!_disposed)
316+
{
317+
if (disposing)
318+
{
319+
CleanupOldResources();
320+
321+
if (_image != null)
322+
{
323+
_image.Source = null;
324+
}
325+
if (_canvas != null)
326+
{
327+
_canvas.Loaded -= OnCanvas_Loaded;
328+
}
329+
if (_image != null)
330+
{
331+
_image.MouseDown -= OnImage_MouseDown;
332+
_image.MouseMove -= OnImage_MouseMove;
333+
_image.MouseUp -= OnImage_MouseUp;
334+
_image.MouseLeave -= OnImage_MouseLeave;
335+
}
336+
if (_replaceButton != null)
337+
{
338+
_replaceButton.Click -= OnReplaceButton_Click;
339+
}
340+
if (_addButton != null)
341+
{
342+
_addButton.Click -= OnAddButton_Click;
343+
}
344+
Unloaded -= OnCropAvatar_Unloaded;
345+
}
346+
_disposed = true;
347+
}
348+
}
349+
350+
351+
~CropAvatar()
352+
{
353+
Dispose(false);
354+
}
256355
}
257356
}

0 commit comments

Comments
 (0)