mirror of
https://gitee.com/csharpui/CPF.git
synced 2025-04-05 17:37:51 +08:00
DialogService
This commit is contained in:
parent
af2e243f07
commit
2c9c0c7e0c
@ -18,7 +18,6 @@ namespace CPF.Toolkit.Demo
|
|||||||
protected override void InitializeComponent()
|
protected override void InitializeComponent()
|
||||||
{
|
{
|
||||||
//LoadStyleFile("res://CPF.Toolkit.Demo/Stylesheet1.css");
|
//LoadStyleFile("res://CPF.Toolkit.Demo/Stylesheet1.css");
|
||||||
this.Behaviors.Add(new ViewBehavior());
|
|
||||||
|
|
||||||
this.Title = "标题";
|
this.Title = "标题";
|
||||||
this.Width = 500;
|
this.Width = 500;
|
||||||
@ -39,6 +38,7 @@ namespace CPF.Toolkit.Demo
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
this.Behaviors.Add(new ViewBehavior());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ namespace CPF.Toolkit.Demo
|
|||||||
|
|
||||||
public void Test()
|
public void Test()
|
||||||
{
|
{
|
||||||
this.Close();
|
this.Dialog.Alert("确定删除所选的文件吗?", "确定删除", DialogType.Warn, "", "取消", "删除");
|
||||||
}
|
}
|
||||||
|
|
||||||
//protected override void OnClosing(ClosingEventArgs e)
|
//protected override void OnClosing(ClosingEventArgs e)
|
||||||
|
@ -5,7 +5,17 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="PropertyChanged.Fody" Version="4.1.0" />
|
<None Remove="Images\ask.png" />
|
||||||
|
<None Remove="Images\error.png" />
|
||||||
|
<None Remove="Images\sucess.png" />
|
||||||
|
<None Remove="Images\warn.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Images\ask.png" />
|
||||||
|
<EmbeddedResource Include="Images\error.png" />
|
||||||
|
<EmbeddedResource Include="Images\sucess.png" />
|
||||||
|
<EmbeddedResource Include="Images\warn.png" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
63
CPF.Toolkit/DialogService.cs
Normal file
63
CPF.Toolkit/DialogService.cs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
using CPF.Controls;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CPF.Toolkit
|
||||||
|
{
|
||||||
|
public interface IDialog
|
||||||
|
{
|
||||||
|
IDialogService Dialog { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IDialogService
|
||||||
|
{
|
||||||
|
string Alert(string text, string title, DialogType dialogType, string defaultButton, params string[] buttons);
|
||||||
|
void Alert(string text);
|
||||||
|
void Sucess(string text);
|
||||||
|
void Error(string text);
|
||||||
|
void Warn(string text);
|
||||||
|
bool Ask(string text);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DialogService : IDialogService
|
||||||
|
{
|
||||||
|
public DialogService(Window owner)
|
||||||
|
{
|
||||||
|
this.owner = owner ?? throw new ArgumentNullException(nameof(owner));
|
||||||
|
}
|
||||||
|
Window owner;
|
||||||
|
|
||||||
|
public string Alert(string text, string title, DialogType dialogType, string defaultButton, params string[] buttons)
|
||||||
|
{
|
||||||
|
var view = new DialogView(text, title, dialogType, defaultButton, buttons);
|
||||||
|
var result = view.ShowDialogSync(owner);
|
||||||
|
return result?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Alert(string text)
|
||||||
|
{
|
||||||
|
this.Alert(text, "消息", DialogType.None, "确定", "确定");
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Ask(string text)
|
||||||
|
{
|
||||||
|
return this.Alert(text, "询问", DialogType.Ask, "确定", "确定", "取消") == "确定";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(string text)
|
||||||
|
{
|
||||||
|
this.Alert(text, "错误", DialogType.Error, defaultButton: "确定", "确定");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Sucess(string text)
|
||||||
|
{
|
||||||
|
this.Alert(text, "成功", DialogType.Sucess, "确定", "确定");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Warn(string text)
|
||||||
|
{
|
||||||
|
this.Alert(text, "警告", DialogType.Warn, "确定", "确定");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
163
CPF.Toolkit/DialogView.cs
Normal file
163
CPF.Toolkit/DialogView.cs
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
using CPF.Controls;
|
||||||
|
using CPF.Drawing;
|
||||||
|
using CPF.Input;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CPF.Toolkit
|
||||||
|
{
|
||||||
|
internal class DialogView : Window
|
||||||
|
{
|
||||||
|
public DialogView(string text, string title, DialogType dialogType, string defaultButton, params string[] buttons)
|
||||||
|
{
|
||||||
|
this.Title = title;
|
||||||
|
this.Text = text;
|
||||||
|
this.Buttons = buttons;
|
||||||
|
this.DefaultButton = defaultButton;
|
||||||
|
this.DialogType = dialogType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DialogType DialogType { get => GetValue<DialogType>(); set => SetValue(value); }
|
||||||
|
public string DefaultButton { get => GetValue<string>(); set => SetValue(value); }
|
||||||
|
public string Text { get => GetValue<string>(); set => SetValue(value); }
|
||||||
|
public string[] Buttons { get => GetValue<string[]>(); set => SetValue(value); }
|
||||||
|
|
||||||
|
protected override void InitializeComponent()
|
||||||
|
{
|
||||||
|
this.ShowInTaskbar = false;
|
||||||
|
this.MaxWidth = 800;
|
||||||
|
this.MinWidth = 400;
|
||||||
|
this.MaxHeight = 600;
|
||||||
|
this.MinHeight = 250;
|
||||||
|
this.CanResize = false;
|
||||||
|
this.Width = "auto";
|
||||||
|
this.Height = "auto";
|
||||||
|
this.Background = null;
|
||||||
|
var frame = this.Children.Add(new WindowFrame(this, new Grid
|
||||||
|
{
|
||||||
|
Size = SizeField.Fill,
|
||||||
|
//LineFill = "red",
|
||||||
|
//LineStroke = new Stroke(1),
|
||||||
|
RowDefinitions =
|
||||||
|
{
|
||||||
|
new RowDefinition{ Height = "auto" },
|
||||||
|
new RowDefinition{ },
|
||||||
|
new RowDefinition{ Height = 35 },
|
||||||
|
},
|
||||||
|
Children =
|
||||||
|
{
|
||||||
|
new Picture
|
||||||
|
{
|
||||||
|
Stretch = Stretch.None,
|
||||||
|
Height = "70",
|
||||||
|
Bindings =
|
||||||
|
{
|
||||||
|
{
|
||||||
|
nameof(Visibility),
|
||||||
|
nameof(DialogType),
|
||||||
|
this,BindingMode.OneWay,
|
||||||
|
(DialogType t) => t == DialogType.None ? Visibility.Collapsed : Visibility.Visible
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nameof(Picture.Source),
|
||||||
|
nameof(DialogType),
|
||||||
|
this,BindingMode.OneWay,
|
||||||
|
(DialogType t) =>
|
||||||
|
{
|
||||||
|
switch (t)
|
||||||
|
{
|
||||||
|
case DialogType.Sucess: return "res://CPF.Toolkit/Images/sucess.png";
|
||||||
|
case DialogType.Error:return"res://CPF.Toolkit/Images/error.png";
|
||||||
|
case DialogType.Ask: return"res://CPF.Toolkit/Images/ask.png";
|
||||||
|
case DialogType.Warn:return "res://CPF.Toolkit/Images/warn.png";
|
||||||
|
default:return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new TextBox
|
||||||
|
{
|
||||||
|
Attacheds = { { Grid.RowIndex,1 } },
|
||||||
|
BorderType = BorderType.BorderThickness,
|
||||||
|
BorderStroke = new Stroke(1, DashStyles.Solid),
|
||||||
|
BorderThickness = new Thickness(0,0,0,1),
|
||||||
|
//BorderFill = "Silver",
|
||||||
|
IsReadOnly = true,
|
||||||
|
Size = SizeField.Fill,
|
||||||
|
FontSize = 16,
|
||||||
|
WordWarp = true,
|
||||||
|
TextAlignment = TextAlignment.Center,
|
||||||
|
MarginTop = 30,
|
||||||
|
Bindings =
|
||||||
|
{
|
||||||
|
{ nameof(TextBox.Text),nameof(Text),this,BindingMode.OneWay}
|
||||||
|
}
|
||||||
|
}.Assign(out var textBox),
|
||||||
|
new StackPanel
|
||||||
|
{
|
||||||
|
Height = "100%",
|
||||||
|
Attacheds = { { Grid.RowIndex,2 } },
|
||||||
|
MarginBottom = 4,
|
||||||
|
Orientation = Orientation.Horizontal,
|
||||||
|
}
|
||||||
|
.LoopCreate(this.Buttons.Length, i => new Button
|
||||||
|
{
|
||||||
|
Content = this.Buttons[i],
|
||||||
|
MinWidth = this.Buttons.Length <= 1 ? 80 : 65,
|
||||||
|
Background = "white",
|
||||||
|
BorderFill = "236,236,236",
|
||||||
|
Height = "95%",
|
||||||
|
MarginRight = 5,
|
||||||
|
Commands = { { nameof(Button.Click),(s,e) => this.DialogResult = this.Buttons[i] } }
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
var controlBox = frame.Find<StackPanel>().FirstOrDefault(x => x.Name == "controlBox");
|
||||||
|
var caption = frame.Find<Panel>().FirstOrDefault(x => x.Name == "caption");
|
||||||
|
var title = frame.Find<TextBlock>().FirstOrDefault(x => x.Name == "title");
|
||||||
|
controlBox.Visibility = Visibility.Collapsed;
|
||||||
|
caption.Background = "white";
|
||||||
|
title.Foreground = "black";
|
||||||
|
textBox.TextChanged += TextBox_TextChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TextBox_TextChanged(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
var textBox = sender as TextBox;
|
||||||
|
if (textBox.Document.Lines.Count > 5)
|
||||||
|
{
|
||||||
|
textBox.TextAlignment = TextAlignment.Left;
|
||||||
|
textBox.Height = "100%";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
textBox.TextAlignment = TextAlignment.Center;
|
||||||
|
textBox.Height = "auto";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnKeyUp(KeyEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.Key == Keys.Enter || e.Key == Keys.Space)
|
||||||
|
{
|
||||||
|
var buttons = this.Find<Button>();
|
||||||
|
var btn = buttons.FirstOrDefault(x => x.IsFocused) ?? buttons.FirstOrDefault(x => x.Content?.ToString() == this.DefaultButton);
|
||||||
|
this.DialogResult = btn.Content.ToString();
|
||||||
|
e.Handled = true;
|
||||||
|
}
|
||||||
|
base.OnKeyUp(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum DialogType
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Sucess,
|
||||||
|
Error,
|
||||||
|
Ask,
|
||||||
|
Warn
|
||||||
|
}
|
||||||
|
}
|
BIN
CPF.Toolkit/Images/ask.png
Normal file
BIN
CPF.Toolkit/Images/ask.png
Normal file
Binary file not shown.
After ![]() (image error) Size: 679 B |
BIN
CPF.Toolkit/Images/error.png
Normal file
BIN
CPF.Toolkit/Images/error.png
Normal file
Binary file not shown.
After ![]() (image error) Size: 879 B |
BIN
CPF.Toolkit/Images/sucess.png
Normal file
BIN
CPF.Toolkit/Images/sucess.png
Normal file
Binary file not shown.
After ![]() (image error) Size: 834 B |
BIN
CPF.Toolkit/Images/warn.png
Normal file
BIN
CPF.Toolkit/Images/warn.png
Normal file
Binary file not shown.
After ![]() (image error) Size: 748 B |
@ -9,6 +9,7 @@ namespace CPF.Toolkit
|
|||||||
{
|
{
|
||||||
protected override void OnBehaviorTo(Window window)
|
protected override void OnBehaviorTo(Window window)
|
||||||
{
|
{
|
||||||
|
this.DataContextChanged(window);
|
||||||
window.PropertyChanged += Window_PropertyChanged;
|
window.PropertyChanged += Window_PropertyChanged;
|
||||||
window.Loaded += Window_Loaded;
|
window.Loaded += Window_Loaded;
|
||||||
window.Closing += Window_Closing;
|
window.Closing += Window_Closing;
|
||||||
@ -52,29 +53,39 @@ namespace CPF.Toolkit
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Window_PropertyChanged(object sender, CPFPropertyChangedEventArgs e)
|
void DataContextChanged(Window window)
|
||||||
{
|
{
|
||||||
switch (e.PropertyName)
|
if (window == null) throw new ArgumentNullException(nameof(window));
|
||||||
|
if (window.DataContext is ICloseable closeable)
|
||||||
{
|
{
|
||||||
case nameof(DataContext):
|
closeable.Closable -= Closeable_Closable;
|
||||||
{
|
closeable.Closable += Closeable_Closable;
|
||||||
if (e.NewValue is ICloseable closeable)
|
}
|
||||||
{
|
|
||||||
closeable.Closable -= Closeable_Closable;
|
if (window.DataContext is IDialog dialog)
|
||||||
closeable.Closable += Closeable_Closable;
|
{
|
||||||
}
|
dialog.Dialog = new DialogService(window);
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Closeable_Closable(object _, ClosingEventArgs ee)
|
void Closeable_Closable(object _, ClosingEventArgs ee)
|
||||||
{
|
{
|
||||||
if (!ee.Cancel)
|
if (!ee.Cancel)
|
||||||
{
|
{
|
||||||
var window = (Window)sender;
|
|
||||||
window.Close();
|
window.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Window_PropertyChanged(object sender, CPFPropertyChangedEventArgs e)
|
||||||
|
{
|
||||||
|
switch (e.PropertyName)
|
||||||
|
{
|
||||||
|
case nameof(DataContext):
|
||||||
|
{
|
||||||
|
DataContextChanged(sender as Window);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,12 @@ using System.Text;
|
|||||||
|
|
||||||
namespace CPF.Toolkit
|
namespace CPF.Toolkit
|
||||||
{
|
{
|
||||||
public class ViewModelBase : INotifyPropertyChanged, ILoaded, IDisposable, ICloseable
|
public class ViewModelBase : INotifyPropertyChanged, ILoaded, IDisposable, ICloseable, IDialog
|
||||||
{
|
{
|
||||||
WeakEventHandlerList events = new WeakEventHandlerList();
|
WeakEventHandlerList events = new WeakEventHandlerList();
|
||||||
|
|
||||||
|
IDialogService IDialog.Dialog { get; set; }
|
||||||
|
|
||||||
event EventHandler<ClosingEventArgs> ICloseable.Closable { add => AddHandler(value); remove => RemoveHandler(value); }
|
event EventHandler<ClosingEventArgs> ICloseable.Closable { add => AddHandler(value); remove => RemoveHandler(value); }
|
||||||
|
|
||||||
public virtual void Dispose() { }
|
public virtual void Dispose() { }
|
||||||
@ -19,6 +22,8 @@ namespace CPF.Toolkit
|
|||||||
|
|
||||||
void ILoaded.OnLoaded() => this.OnLoaded();
|
void ILoaded.OnLoaded() => this.OnLoaded();
|
||||||
|
|
||||||
|
protected IDialogService Dialog => (this as IDialog).Dialog;
|
||||||
|
|
||||||
protected virtual void OnLoaded() { }
|
protected virtual void OnLoaded() { }
|
||||||
protected void Close() => this.RaiseEvent(new ClosingEventArgs(), nameof(ICloseable.Closable));
|
protected void Close() => this.RaiseEvent(new ClosingEventArgs(), nameof(ICloseable.Closable));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user