From f1cc462abcb41190c1322edbc6899024ca219c65 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 16 Apr 2019 20:51:53 +0100 Subject: [PATCH] implement MVVM style notifications. (Managed only) --- nukebuild/Numerge | 2 +- samples/ControlCatalog/MainWindow.xaml | 7 ++++ samples/ControlCatalog/MainWindow.xaml.cs | 23 ++---------- .../ViewModels/MainWindowViewModel.cs | 35 +++++++++++++++++++ .../ViewModels/NotificationViewModel.cs | 12 +++++++ .../Views/NotificationView.xaml | 23 ++++++++++++ .../Views/NotificationView.xaml.cs | 19 ++++++++++ .../Notifications/NotificationManager.cs | 6 ++-- .../NotificationArea.xaml | 12 +++---- 9 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 samples/ControlCatalog/ViewModels/MainWindowViewModel.cs create mode 100644 samples/ControlCatalog/ViewModels/NotificationViewModel.cs create mode 100644 samples/ControlCatalog/Views/NotificationView.xaml create mode 100644 samples/ControlCatalog/Views/NotificationView.xaml.cs diff --git a/nukebuild/Numerge b/nukebuild/Numerge index aef10ae67d..4464343aef 160000 --- a/nukebuild/Numerge +++ b/nukebuild/Numerge @@ -1 +1 @@ -Subproject commit aef10ae67dc55c95f49b52a505a0be33bfa297a5 +Subproject commit 4464343aef5c8ab7a42fcb20a483a6058199f8b8 diff --git a/samples/ControlCatalog/MainWindow.xaml b/samples/ControlCatalog/MainWindow.xaml index ae9574189a..86ca8c16ac 100644 --- a/samples/ControlCatalog/MainWindow.xaml +++ b/samples/ControlCatalog/MainWindow.xaml @@ -3,7 +3,14 @@ Icon="/Assets/test_icon.ico" xmlns:local="clr-namespace:ControlCatalog" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:vm="clr-namespace:ControlCatalog.ViewModels" + xmlns:v="clr-namespace:ControlCatalog.Views" x:Class="ControlCatalog.MainWindow"> + + + + + diff --git a/samples/ControlCatalog/MainWindow.xaml.cs b/samples/ControlCatalog/MainWindow.xaml.cs index 4057af59ff..02cc692949 100644 --- a/samples/ControlCatalog/MainWindow.xaml.cs +++ b/samples/ControlCatalog/MainWindow.xaml.cs @@ -3,6 +3,7 @@ using Avalonia.Controls; using Avalonia.Controls.Notifications; using Avalonia.Markup.Xaml; using Avalonia.Threading; +using ControlCatalog.ViewModels; using System; using System.Threading.Tasks; @@ -10,8 +11,6 @@ namespace ControlCatalog { public class MainWindow : Window { - private readonly NotificationManager _notificationManager = new NotificationManager(); - public MainWindow() { this.InitializeComponent(); @@ -19,25 +18,7 @@ namespace ControlCatalog //Renderer.DrawFps = true; //Renderer.DrawDirtyRects = Renderer.DrawFps = true; - Dispatcher.UIThread.InvokeAsync(async () => - { - await Task.Delay(5000); - - _notificationManager.Show(new NotificationContent { Message = "Test1", Type = NotificationType.Information }, "Main"); - - await Task.Delay(500); - _notificationManager.Show(new NotificationContent { Message = "Test2", Type = NotificationType.Error }, "Main"); - - await Task.Delay(500); - _notificationManager.Show(new NotificationContent { Message = "Test3", Type = NotificationType.Warning }, "Main"); - - await Task.Delay(500); - _notificationManager.Show(new NotificationContent { Message = "Test4", Type = NotificationType.Success }, "Main"); - - await Task.Delay(500); - _notificationManager.Show("Test5", "Main"); - - }); + DataContext = new MainWindowViewModel(); } private void InitializeComponent() diff --git a/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs new file mode 100644 index 0000000000..5335d1cacc --- /dev/null +++ b/samples/ControlCatalog/ViewModels/MainWindowViewModel.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Avalonia.Controls.Notifications; +using Avalonia.Threading; + +namespace ControlCatalog.ViewModels +{ + class MainWindowViewModel + { + public MainWindowViewModel() + { + Dispatcher.UIThread.InvokeAsync(async () => + { + await Task.Delay(5000); + + NotificationManager.Instance.Show(new NotificationViewModel { Title = "Warning", Message = "Please save your work before closing." }, "Main"); + + await Task.Delay(1500); + NotificationManager.Instance.Show(new NotificationContent { Message = "Test2", Type = NotificationType.Error }, "Main"); + + await Task.Delay(2000); + NotificationManager.Instance.Show(new NotificationContent { Message = "Test3", Type = NotificationType.Warning }, "Main"); + + await Task.Delay(2500); + NotificationManager.Instance.Show(new NotificationContent { Message = "Test4", Type = NotificationType.Success }, "Main"); + + await Task.Delay(500); + NotificationManager.Instance.Show("Test5", "Main"); + + }); + } + } +} diff --git a/samples/ControlCatalog/ViewModels/NotificationViewModel.cs b/samples/ControlCatalog/ViewModels/NotificationViewModel.cs new file mode 100644 index 0000000000..4ce41b5566 --- /dev/null +++ b/samples/ControlCatalog/ViewModels/NotificationViewModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ControlCatalog.ViewModels +{ + public class NotificationViewModel + { + public string Title { get; set; } + public string Message { get; set; } + } +} diff --git a/samples/ControlCatalog/Views/NotificationView.xaml b/samples/ControlCatalog/Views/NotificationView.xaml new file mode 100644 index 0000000000..45210a338c --- /dev/null +++ b/samples/ControlCatalog/Views/NotificationView.xaml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + +