Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 39b54c5

Browse files
committed
add dialogwindow and provide function of pass value
1 parent f9742be commit 39b54c5

File tree

9 files changed

+346
-14
lines changed

9 files changed

+346
-14
lines changed

CookPopularControl/Controls/DialogBox/DialogBox.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ namespace CookPopularControl.Controls
2020
/// <summary>
2121
/// 对话窗口
2222
/// </summary>
23-
public class DialogBox : ContentControl
23+
public class DialogBox : ContentControl, IDialog
2424
{
2525
private AdornerContainer Container;
2626
private static readonly HashSet<FrameworkElement> DialogInstances = new HashSet<FrameworkElement>();
2727
public static readonly ICommand OpenDialogCommand = new RoutedCommand("OpenDialog", typeof(DialogBox));
2828
public static readonly ICommand CloseDialogCommand = new RoutedCommand("CloseDialog", typeof(DialogBox));
2929

30-
31-
public bool IsOpen { get; private set; }
32-
30+
public bool IsClosed { get; private set; }
3331

3432
public static string GetMark(DependencyObject obj) => (string)obj.GetValue(MarkProperty);
3533
public static void SetMark(DependencyObject obj, string value) => obj.SetValue(MarkProperty, value);
@@ -75,7 +73,7 @@ public static DialogBox Show(object content, string mark = "")
7573
}
7674

7775
DialogBox dialogBox;
78-
dialogBox = new DialogBox { Content = content, IsOpen = true };
76+
dialogBox = new DialogBox { Content = content, IsClosed = false };
7977
SetMark(dialogBox, mark);
8078

8179
FrameworkElement element;
@@ -123,7 +121,7 @@ private void Close(DependencyObject element)
123121
var decorator = VisualTreeHelperExtension.GetVisualDescendants(element).OfType<AdornerDecorator>().FirstOrDefault();
124122
if (decorator != null)
125123
{
126-
IsOpen = false;
124+
IsClosed = true;
127125
var layer = decorator.AdornerLayer;
128126
layer?.Remove(Container);
129127
DialogInstances.Remove(this);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Description:DialogExtensions
3+
* Author: Chance.Zheng
4+
* Create Time: 2023-03-08 11:48:52
5+
* .Net Version: 4.6
6+
* CLR Version: 4.0.30319.42000
7+
* Copyright (c) CookCSharp 2023 All Rights Reserved.
8+
*/
9+
10+
11+
using CookPopularCSharpToolkit.Windows;
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Text;
16+
using System.Threading.Tasks;
17+
using System.Windows;
18+
using System.Windows.Controls;
19+
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
20+
21+
22+
namespace CookPopularControl.Controls
23+
{
24+
public interface IDialogResultable<T>
25+
{
26+
T Result { get; set; }
27+
28+
Action CloseAction { get; set; }
29+
}
30+
31+
public static class DialogExtensions
32+
{
33+
public static IDialog Initialize<TViewModel>(this IDialog dialog, Action<TViewModel> action)
34+
{
35+
action?.Invoke(dialog.GetViewModel<TViewModel>());
36+
37+
return dialog;
38+
}
39+
40+
public static TViewModel GetViewModel<TViewModel>(this IDialog dialog)
41+
{
42+
if (dialog is not ContentControl control)
43+
throw new InvalidOperationException("The dialog is not subclass of the ContentControl. ");
44+
45+
if (control.Content is not FrameworkElement frameworkElement)
46+
throw new InvalidOperationException("The dialog's content is not subclass of the FrameworkElement. ");
47+
48+
if (frameworkElement.DataContext is not TViewModel viewModel)
49+
throw new InvalidOperationException($"The viewmodel of the dialog's content is not the {typeof(TViewModel)} type or its subclass. ");
50+
51+
return viewModel;
52+
}
53+
54+
public static Task<TResult> GetResultAsync<TResult>(this IDialog dialog)
55+
{
56+
var tcs = new TaskCompletionSource<TResult>();
57+
58+
try
59+
{
60+
if (dialog.IsClosed)
61+
{
62+
SetResult();
63+
}
64+
else
65+
{
66+
(dialog as FrameworkElement).Unloaded += OnUnloaded;
67+
68+
var dialogBox = dialog as DialogBox;
69+
if (dialogBox != null)
70+
dialog.GetViewModel<IDialogResultable<TResult>>().CloseAction = dialogBox.Close;
71+
72+
var win = dialog as Windows.DialogWindow;
73+
if (win != null)
74+
{
75+
dialog.GetViewModel<IDialogResultable<TResult>>().CloseAction = win.CloseWindow;
76+
}
77+
}
78+
}
79+
catch (Exception ex)
80+
{
81+
tcs.TrySetException(ex);
82+
}
83+
84+
return tcs.Task;
85+
86+
void OnUnloaded(object sender, RoutedEventArgs args)
87+
{
88+
(dialog as FrameworkElement).Unloaded -= OnUnloaded;
89+
SetResult();
90+
}
91+
92+
void SetResult()
93+
{
94+
try
95+
{
96+
bool isExcute = false;
97+
if (dialog is Windows.DialogWindow win && win.IsConfirm)
98+
isExcute = true;
99+
else if (dialog is DialogBox dialogBox)
100+
isExcute = true;
101+
else
102+
isExcute = false;
103+
104+
if (isExcute)
105+
tcs.TrySetResult(dialog.GetViewModel<IDialogResultable<TResult>>().Result);
106+
}
107+
catch (Exception e)
108+
{
109+
tcs.TrySetException(e);
110+
}
111+
}
112+
}
113+
}
114+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Description:IDialog
3+
* Author: Chance.Zheng
4+
* Create Time: 2023-03-08 14:58:25
5+
* .Net Version: 4.6
6+
* CLR Version: 4.0.30319.42000
7+
* Copyright (c) CookCSharp 2023 All Rights Reserved.
8+
*/
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Text;
15+
using System.Threading.Tasks;
16+
using System.Windows;
17+
18+
namespace CookPopularControl.Controls
19+
{
20+
public interface IDialog
21+
{
22+
/// <summary>
23+
/// 对话框是否关闭
24+
/// </summary>
25+
public bool IsClosed { get; }
26+
}
27+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Description:DialogWindow
3+
* Author: Chance.Zheng
4+
* Create Time: 2023-03-08 12:43:57
5+
* .Net Version: 4.6
6+
* CLR Version: 4.0.30319.42000
7+
* Copyright (c) CookCSharp 2023 All Rights Reserved.
8+
*/
9+
10+
11+
using CookPopularControl.Communal;
12+
using CookPopularControl.Controls;
13+
using CookPopularControl.Windows;
14+
using CookPopularCSharpToolkit.Communal;
15+
using CookPopularCSharpToolkit.Windows;
16+
using System;
17+
using System.CodeDom.Compiler;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
using System.Text;
21+
using System.Threading.Tasks;
22+
using System.Windows;
23+
using System.Windows.Input;
24+
25+
26+
namespace CookPopularControl.Windows
27+
{
28+
public class DialogWindow : NormalWindow, IDialog
29+
{
30+
public bool IsClosed { get; private set; }
31+
/// <summary>
32+
/// 是否点击了确定,如果true,则将对话框的内容传至发起界面,false则不响应
33+
/// </summary>
34+
public bool IsConfirm { get; private set; }
35+
36+
/// <summary>
37+
/// 是否显示窗体Icon图标
38+
/// </summary>
39+
public bool IsShowIcon
40+
{
41+
get => (bool)GetValue(IsShowIconProperty);
42+
set => SetValue(IsShowIconProperty, ValueBoxes.BooleanBox(value));
43+
}
44+
/// <summary>
45+
/// 提供<see cref="IsShowIcon"/>的依赖属性
46+
/// </summary>
47+
public static readonly DependencyProperty IsShowIconProperty =
48+
DependencyProperty.Register("IsShowIcon", typeof(bool), typeof(DialogWindow), new PropertyMetadata(ValueBoxes.FalseBox));
49+
50+
51+
static DialogWindow()
52+
{
53+
StyleProperty.OverrideMetadata(typeof(DialogWindow), new FrameworkPropertyMetadata(default, (s, e) => ResourceHelper.GetResource<Style>("DialogWindowStyle")));
54+
}
55+
56+
public DialogWindow()
57+
{
58+
CommandBindings.Add(new CommandBinding(ControlCommands.ConfirmCommand, Executed, (s, e) => e.CanExecute = true));
59+
CommandBindings.Add(new CommandBinding(ControlCommands.CancelCommand, Executed, (s, e) => e.CanExecute = true));
60+
61+
this.Owner = WindowExtension.GetActiveWindow();
62+
this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
63+
}
64+
65+
private static void Executed(object sender, ExecutedRoutedEventArgs e)
66+
{
67+
var win = sender as DialogWindow;
68+
if (e.Command == ControlCommands.ConfirmCommand)
69+
win.IsConfirm = true;
70+
else if (e.Command == ControlCommands.CancelCommand)
71+
win.IsConfirm = false;
72+
73+
win.Close();
74+
}
75+
76+
public void CloseWindow()
77+
{
78+
this.Close();
79+
IsClosed = true;
80+
}
81+
82+
public static DialogWindow Show<T>(string caption = default, bool isShowIcon = false) where T : new()
83+
{
84+
return Show(new T(), caption, isShowIcon);
85+
}
86+
87+
public static DialogWindow Show(object content, string caption = default, bool isShowIcon = false)
88+
{
89+
var win = new DialogWindow();
90+
win.Title = caption ?? string.Empty;
91+
win.Content = content;
92+
win.IsShowIcon = isShowIcon;
93+
win.Show();
94+
95+
return win;
96+
}
97+
98+
public static DialogWindow ShowDialog<T>(string caption = default, bool isShowIcon = false) where T : new()
99+
{
100+
return ShowDialog(new T(), caption, isShowIcon);
101+
}
102+
103+
public static DialogWindow ShowDialog(object content, string caption = default, bool isShowIcon = false)
104+
{
105+
var win = new DialogWindow();
106+
win.Title = caption ?? string.Empty;
107+
win.Content = content;
108+
win.IsShowIcon = isShowIcon;
109+
win.ShowDialog();
110+
111+
return win;
112+
}
113+
}
114+
}

CookPopularControl/Controls/Windows/MessageDialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Brush ImageBrush
7272
DependencyProperty.Register("ImageBrush", typeof(Brush), typeof(MessageDialog), new PropertyMetadata(default(Brush)));
7373

7474
/// <summary>
75-
/// 是否显示<see cref="MessageDialog"/>的图标
75+
/// 是否显示<see cref="MessageDialog"/>信息的类型图标
7676
/// </summary>
7777
public bool IsShowImage
7878
{
@@ -83,7 +83,7 @@ public bool IsShowImage
8383
DependencyProperty.Register("IsShowImage", typeof(bool), typeof(MessageDialog), new PropertyMetadata(ValueBoxes.TrueBox));
8484

8585

86-
private MessageDialog()
86+
protected MessageDialog()
8787
{
8888
CommandBindings.Add(new CommandBinding(ControlCommands.ConfirmCommand, Executed, (s, e) => e.CanExecute = true));
8989
CommandBindings.Add(new CommandBinding(ControlCommands.YesCommand, Executed, (s, e) => e.CanExecute = true));

CookPopularControl/CookPopularControl.xml

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CookPopularControl/Resources/Dictionaries/EffectShadow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
Color="{StaticResource ShadowEffectColor}"
4444
Opacity="0.4"
4545
BlurRadius="10" Direction="270" RenderingBias="Performance"
46-
ShadowDepth="-10" po:Freeze="True" />
46+
ShadowDepth="-5" po:Freeze="True" />
4747
<DropShadowEffect x:Key="NotifyMessageShadowEffect"
4848
Color="{StaticResource ShadowEffectColor}"
4949
Opacity="0.2"

CookPopularControl/Themes/DefaultPopularControl.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Style x:Key="{x:Type win:NormalWindow}" TargetType="{x:Type win:NormalWindow}" BasedOn="{StaticResource NormalWindowStyle}" />
7575
<Style x:Key="{x:Type win:SideBarWindow}" TargetType="{x:Type win:SideBarWindow}" BasedOn="{StaticResource SideBarWindowStyle}" />
7676
<Style x:Key="{x:Type win:NoneTitleBarWindow}" TargetType="{x:Type win:NoneTitleBarWindow}" BasedOn="{StaticResource NoneTitleBarWindowStyle}" />
77+
<Style x:Key="{x:Type win:DialogWindow}" TargetType="{x:Type win:DialogWindow}" BasedOn="{StaticResource DialogWindowStyle}" />
7778
<Style x:Key="{x:Type win:MessageDialog}" TargetType="{x:Type win:MessageDialog}" BasedOn="{StaticResource MessageDialogStyle}" />
7879

7980

0 commit comments

Comments
 (0)