diff --git a/src/Dialogs/DialogBase.cs b/src/Dialogs/DialogBase.cs index 9efbc1e..e75a420 100644 --- a/src/Dialogs/DialogBase.cs +++ b/src/Dialogs/DialogBase.cs @@ -16,13 +16,26 @@ namespace wpf_material_dialogs.Dialogs { + /// /// /// Class DialogBase. - /// Implements the + /// Implements the /// - /// + /// public abstract class DialogBase : IDialog { + #region Events + + /// + /// + /// Occurs when [property changed]. + /// + public event PropertyChangedEventHandler PropertyChanged; + + #endregion + + #region Fields + private readonly object buttonLock = new(); private readonly List buttonTypes = new(); private HorizontalAlignment buttonAlignment = HorizontalAlignment.Right; @@ -40,6 +53,10 @@ public abstract class DialogBase : IDialog private FontWeight titleFontWeight = FontWeights.Bold; private double width = double.NaN; + #endregion + + #region Properties + /// /// Gets the selected buttons based on the selection. /// @@ -131,36 +148,6 @@ public DialogButton DialogButtons } } - /// - /// Gets the buttons. - /// - /// The button enum to convert to an IButton. - /// IButtons. - /// button - /// button - /// button - public IButtons GetButtons(DialogButton button) - { - lock (buttonLock) - { - var assembly = Assembly.GetExecutingAssembly(); - - if (!buttonTypes?.Any() ?? false) - { - buttonTypes.AddRange(assembly - .GetTypes() - .Where(mytype => mytype.GetInterfaces().Contains(typeof(IButtons)))); - } - - var buttons = buttonTypes - ?.First(mytype => mytype.Name.Equals(button.ToString(), StringComparison.CurrentCultureIgnoreCase)); - - return buttons?.FullName != null - ? assembly.CreateInstance(buttons.FullName) as IButtons - : throw new ArgumentOutOfRangeException(nameof(button)); - } - } - /// /// Gets or sets the icon brush. /// @@ -189,44 +176,6 @@ public PackIconKind IconKind } } - /// - /// Notifies the of property changed. - /// - /// Name of the property. - public void NotifyOfPropertyChanged([CallerMemberName] string propertyName = "") - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - /// - /// Shows the dialog with given configuration. - /// - /// The dialog hos identifier. - /// The opened action. - /// The closing action. - /// containing and . - public async Task ShowDialog(object dialogHosId = null, - DialogOpenedEventHandler openedAction = null, - DialogClosingEventHandler closingAction = null) - { - return await DialogHost.Show(this, dialogHosId, openedAction, closingAction) as DialogResult? ?? - DialogResult.None; - } - - /// - /// Shows the dialog with given configuration. - /// - /// The dialog hos identifier. - /// The opened action. - /// The closing action. - /// containing and . - public async Task ShowDialogObject(object dialogHosId = null, - DialogOpenedEventHandler openedAction = null, - DialogClosingEventHandler closingAction = null) - { - return await DialogHost.Show(this, dialogHosId, openedAction, closingAction); - } - /// /// Gets or sets a value indicating whether to show the icon. /// @@ -297,10 +246,7 @@ public double TitleFontSize } } - /// - /// Occurs when [property changed]. - /// - public event PropertyChangedEventHandler PropertyChanged; + #endregion /// /// Shows the destroy alert dialog. @@ -308,31 +254,28 @@ public double TitleFontSize /// The text. /// The buttons. /// true if , false otherwise. - public static async Task ShowAlertDialog(string text, DialogButton buttons) + public static async Task ShowAlertDialog(string text, DialogButton buttons) => await new AlertDialog { - return await new AlertDialog - { - Title = "Alert", - IconBrush = Brushes.Red, - Text = text, - DialogButtons = buttons - }.ShowDialog(); - } + Title = "Alert", + IconBrush = Brushes.Red, + Text = text, + DialogButtons = buttons, + }.ShowDialog(); /// /// Shows the dialog with a few customizable options. /// /// The dialog text. - /// The footer dialog buttons. - /// The dialog title. + /// The footer dialog buttons. + /// The dialog title. /// The dialog identifier. /// . - public async Task ShowDialog(string dialogText, DialogButton buttons, string title, + public async Task ShowDialog(string dialogText, DialogButton buttonType, string titleText, object dialogHosId = null) { - Title = title; + Title = titleText; Text = dialogText; - DialogButtons = buttons; + DialogButtons = buttonType; return await ShowDialog(dialogHosId); } @@ -341,20 +284,102 @@ public async Task ShowDialog(string dialogText, DialogButton butto /// Shows the dialog with a few customizable options. /// /// The dialog text. - /// The footer dialog buttons. - /// Kind of the icon. - /// The icon brush. - /// The dialog title. + /// The footer dialog buttons. + /// Kind of the icon. + /// The icon brush. + /// The dialog title. /// The dialog identifier. /// . - public async Task ShowDialog(string dialogText, DialogButton buttons, PackIconKind iconKind, - Brush iconBrush, string title, object dialogHosId = null) + public async Task ShowDialog(string dialogText, DialogButton buttonType, PackIconKind packIconKind, + Brush packIconBrush, string titleText, object dialogHosId = null) { - IconKind = iconKind; + IconKind = packIconKind; ShowIcon = true; - IconBrush = iconBrush; + IconBrush = packIconBrush; - return await ShowDialog(dialogText, buttons, title, dialogHosId); + return await ShowDialog(dialogText, buttonType, titleText, dialogHosId); } + + public static async Task ShowDialogContent(object content, object dialogHosId = null, + DialogOpenedEventHandler openedAction = null, + DialogClosingEventHandler closingAction = null) + => content == null + ? throw new ArgumentNullException(nameof(content)) + : await DialogHost.Show(content, dialogHosId, openedAction, closingAction) as DialogResult? ?? DialogResult.None; + + #region IDialog + + /// + /// + /// Gets the buttons. + /// + /// The button enum to convert to an IButton. + /// IButtons. + /// button + /// button + /// button + public IButtons GetButtons(DialogButton button) + { + // ReSharper disable once PossibleNullReferenceException + lock (buttonLock) + { + var assembly = Assembly.GetExecutingAssembly(); + + if (!buttonTypes?.Any() ?? false) + { + buttonTypes.AddRange(assembly + .GetTypes() + .Where(mytype => mytype.GetInterfaces().Contains(typeof(IButtons)))); + } + + var selectedButtonType = + buttonTypes?.First(mytype => button.ToString().Equals(mytype?.Name, StringComparison.CurrentCultureIgnoreCase)); + + return selectedButtonType?.FullName != null + ? assembly.CreateInstance(selectedButtonType.FullName) as IButtons + : throw new ArgumentOutOfRangeException(nameof(button)); + } + } + + /// + /// Notifies the of property changed. + /// + /// Name of the property. + public void NotifyOfPropertyChanged([CallerMemberName] string propertyName = "") => + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + + /// + /// + /// Shows the dialog with given configuration. + /// + /// The dialog hos identifier. + /// The opened action. + /// The closing action. + /// + /// containing and + /// . + /// + public async Task ShowDialog(object dialogHosId = null, + DialogOpenedEventHandler openedAction = null, + DialogClosingEventHandler closingAction = null) => + await DialogHost.Show(this, dialogHosId, openedAction, closingAction) as DialogResult? ?? + DialogResult.None; + + /// + /// + /// Shows the dialog with given configuration. + /// + /// The dialog hos identifier. + /// The opened action. + /// The closing action. + /// + /// containing and + /// . + /// + public async Task ShowDialogObject(object dialogHosId = null, + DialogOpenedEventHandler openedAction = null, + DialogClosingEventHandler closingAction = null) => await DialogHost.Show(this, dialogHosId, openedAction, closingAction); + + #endregion } } \ No newline at end of file diff --git a/src/Dialogs/WarningDialog.cs b/src/Dialogs/WarningDialog.cs index 50039af..fa2637b 100644 --- a/src/Dialogs/WarningDialog.cs +++ b/src/Dialogs/WarningDialog.cs @@ -1,17 +1,17 @@ using MaterialDesignThemes.Wpf; -using System.ComponentModel; using System.Windows.Media; using wpf_material_dialogs.Enums; namespace wpf_material_dialogs.Dialogs { + /// /// /// Warning dialog. - /// Implements the - /// Implements the + /// Implements the + /// Implements the /// - /// - /// + /// + /// public class WarningDialog : DialogBase { /// diff --git a/src/wpf-material-dialogs.csproj b/src/wpf-material-dialogs.csproj index fd71acc..ee1fa32 100644 --- a/src/wpf-material-dialogs.csproj +++ b/src/wpf-material-dialogs.csproj @@ -1,6 +1,6 @@  - net472;net5.0-windows + net472;net6.0-windows;net7.0-windows Material Design WPF Dialogs true latest diff --git a/wpf-material-dialogs.test/DialogTests.cs b/wpf-material-dialogs.test/DialogTests.cs index 391e503..8723f54 100644 --- a/wpf-material-dialogs.test/DialogTests.cs +++ b/wpf-material-dialogs.test/DialogTests.cs @@ -14,8 +14,8 @@ namespace wpf_material_dialogs.test { - [Apartment(ApartmentState.STA)] - public class DialogTestBase : IDisposable where TDialog : IDialog, new() + [NUnit.Framework.Ignore("base class")] + internal class DialogTestBase : IDisposable where TDialog : IDialog, new() { private DialogHost dialogHost; private IDialog testDialog; @@ -184,22 +184,22 @@ private async Task OpenTestDialog(IDialog dialog, DialogOpenedEven } [Apartment(ApartmentState.STA)] - public class AlertDialogTests : DialogTestBase + internal class AlertDialogTests : DialogTestBase { } [Apartment(ApartmentState.STA)] - public class ErrorDialogTests : DialogTestBase + internal class ErrorDialogTests : DialogTestBase { } [Apartment(ApartmentState.STA)] - public class InfoDialogTests : DialogTestBase + internal class InfoDialogTests : DialogTestBase { } [Apartment(ApartmentState.STA)] - public class WarningDialogTests : DialogTestBase + internal class WarningDialogTests : DialogTestBase { } } diff --git a/wpf-material-dialogs.test/wpf-material-dialogs.test.csproj b/wpf-material-dialogs.test/wpf-material-dialogs.test.csproj index 1b25ff4..31bcd74 100644 --- a/wpf-material-dialogs.test/wpf-material-dialogs.test.csproj +++ b/wpf-material-dialogs.test/wpf-material-dialogs.test.csproj @@ -1,7 +1,6 @@ - - + - net5.0-windows + net7.0-windows wpf_material_dialogs.test false @@ -13,11 +12,11 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/wpf-material-wpfTest/App.xaml.cs b/wpf-material-wpfTest/App.xaml.cs index 77dcb5e..73f8448 100644 --- a/wpf-material-wpfTest/App.xaml.cs +++ b/wpf-material-wpfTest/App.xaml.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; namespace wpf_material_wpfTest { + /// /// /// Interaction logic for App.xaml /// diff --git a/wpf-material-wpfTest/MainWindow.xaml b/wpf-material-wpfTest/MainWindow.xaml index 10b2547..e4ed20e 100644 --- a/wpf-material-wpfTest/MainWindow.xaml +++ b/wpf-material-wpfTest/MainWindow.xaml @@ -9,7 +9,12 @@ xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" Foreground="{DynamicResource AttentionToActionBrush}"> - + + + + + + diff --git a/wpf-material-wpfTest/MainWindow.xaml.cs b/wpf-material-wpfTest/MainWindow.xaml.cs index d69cdfc..3856c7a 100644 --- a/wpf-material-wpfTest/MainWindow.xaml.cs +++ b/wpf-material-wpfTest/MainWindow.xaml.cs @@ -1,31 +1,47 @@ -using System; +using CommunityToolkit.Mvvm.Input; +using System.Threading.Tasks; using System.Windows; +using System.Windows.Forms; +using System.Windows.Input; using wpf_material_dialogs.Dialogs; +using HorizontalAlignment = System.Windows.HorizontalAlignment; namespace wpf_material_wpfTest { + /// /// - /// Interaction logic for MainWindow.xaml + /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { - public MainWindow() => InitializeComponent(); + #region Properties - /// - protected override async void OnContentRendered(EventArgs e) + public ICommand OpenDialog => new AsyncRelayCommand(async () => { - base.OnContentRendered(e); - var dialog = await new CustomDialog + var result = await DialogBase.ShowDialogContent(new CustomDialog { SubTitle = "This is a custom dialog provided by the test application. It can use regular dialog parameters or custom parameters, colors, buttons or reuse parts of the built-in dialog.", - }.ShowDialog(); + }); - await new InfoDialog - { - Text = $"You pressed {dialog}", - ButtonAlignment = HorizontalAlignment.Center, - }.ShowDialog(); - } + await ShowResult(result); + }); + + public ICommand OpenDialog2 => new AsyncRelayCommand(async () => + { + var result = await DialogBase.ShowDialogContent(new TestCustomDialog()); + + await ShowResult(result); + }); + + #endregion + + public MainWindow() => InitializeComponent(); + + private static async Task ShowResult(DialogResult result) => await new InfoDialog + { + Text = $"You pressed {result}", + ButtonAlignment = HorizontalAlignment.Center, + }.ShowDialog(); } -} +} \ No newline at end of file diff --git a/wpf-material-wpfTest/TestCustomDialog.xaml b/wpf-material-wpfTest/TestCustomDialog.xaml new file mode 100644 index 0000000..ef9aad3 --- /dev/null +++ b/wpf-material-wpfTest/TestCustomDialog.xaml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + Dialog Title + Dialog text or data goes here + + + + + + + diff --git a/wpf-material-wpfTest/TestCustomDialog.xaml.cs b/wpf-material-wpfTest/TestCustomDialog.xaml.cs new file mode 100644 index 0000000..262ccf3 --- /dev/null +++ b/wpf-material-wpfTest/TestCustomDialog.xaml.cs @@ -0,0 +1,29 @@ +using CommunityToolkit.Mvvm.Input; +using MaterialDesignThemes.Wpf; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Windows.Input; +using wpf_material_dialogs.Interfaces; +using UserControl = System.Windows.Controls.UserControl; + +namespace wpf_material_wpfTest +{ + /// + /// + /// + /// Interaction logic for TestCustomDialog.xaml + /// + public partial class TestCustomDialog : UserControl + { + public ICommand CloseCommand => new AsyncRelayCommand((result) => + { + DialogHost.CloseDialogCommand.Execute(result, this); + return Task.CompletedTask; + }); + + public TestCustomDialog() + { + InitializeComponent(); + } + } +} diff --git a/wpf-material-wpfTest/wpf-material-wpfTest.csproj b/wpf-material-wpfTest/wpf-material-wpfTest.csproj index 040c599..a951c12 100644 --- a/wpf-material-wpfTest/wpf-material-wpfTest.csproj +++ b/wpf-material-wpfTest/wpf-material-wpfTest.csproj @@ -2,7 +2,7 @@ WinExe - net5.0-windows + net7.0-windows wpf_material_wpfTest true true @@ -15,8 +15,13 @@ - - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/wpf-material-wpftestFramework/App.xaml.cs b/wpf-material-wpftestFramework/App.xaml.cs index 66fc89b..80fa564 100644 --- a/wpf-material-wpftestFramework/App.xaml.cs +++ b/wpf-material-wpftestFramework/App.xaml.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; namespace wpf_material_wpftestFramework { + /// /// /// Interaction logic for App.xaml /// diff --git a/wpf-material-wpftestFramework/wpf-material-wpftestFramework.csproj b/wpf-material-wpftestFramework/wpf-material-wpftestFramework.csproj index dbfe21b..21c809b 100644 --- a/wpf-material-wpftestFramework/wpf-material-wpftestFramework.csproj +++ b/wpf-material-wpftestFramework/wpf-material-wpftestFramework.csproj @@ -76,7 +76,6 @@ ResXFileCodeGenerator Resources.Designer.cs - SettingsSingleFileGenerator Settings.Designer.cs