fix: remove Manager OnDetachedFromVisualTree.

This commit is contained in:
Zhang Dian
2024-09-12 18:28:13 +08:00
parent 4258e4bbea
commit a054d17fd7
4 changed files with 36 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.VisualTree;
using Ursa.Controls;
using Ursa.Demo.ViewModels;
@@ -8,6 +10,7 @@ namespace Ursa.Demo.Pages;
public partial class NotificationDemo : UserControl
{
private NotificationDemoViewModel _viewModel;
private TopLevel? _topLevel;
public NotificationDemo()
{
@@ -19,7 +22,17 @@ public partial class NotificationDemo : UserControl
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
var topLevel = TopLevel.GetTopLevel(this);
_viewModel.NotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 };
_topLevel = TopLevel.GetTopLevel(this);
_viewModel.NotificationManager = new WindowNotificationManager(_topLevel) { MaxItems = 3 };
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
var adorner = _topLevel.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;
if (adorner is not null && _viewModel.NotificationManager is not null)
{
adorner.Children.Remove(_viewModel.NotificationManager);
}
}
}

View File

@@ -1,5 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.VisualTree;
using Ursa.Controls;
using Ursa.Demo.ViewModels;
@@ -8,6 +10,7 @@ namespace Ursa.Demo.Pages;
public partial class ToastDemo : UserControl
{
private ToastDemoViewModel _viewModel;
private TopLevel? _topLevel;
public ToastDemo()
{
@@ -19,7 +22,17 @@ public partial class ToastDemo : UserControl
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
var topLevel = TopLevel.GetTopLevel(this);
_viewModel.ToastManager = new WindowToastManager(topLevel) { MaxItems = 3 };
_topLevel = TopLevel.GetTopLevel(this);
_viewModel.ToastManager = new WindowToastManager(_topLevel) { MaxItems = 3 };
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
var adorner = _topLevel.FindDescendantOfType<VisualLayerManager>()?.AdornerLayer;
if (adorner is not null && _viewModel.ToastManager is not null)
{
adorner.Children.Remove(_viewModel.ToastManager);
}
}
}

View File

@@ -37,7 +37,7 @@ public class MenuViewModel: ViewModelBase
new() { MenuHeader = "Message Box", Key = MenuKeys.MenuKeyMessageBox },
new() { MenuHeader = "MultiComboBox", Key = MenuKeys.MenuKeyMultiComboBox, Status = "Updated" },
new() { MenuHeader = "Nav Menu", Key = MenuKeys.MenuKeyNavMenu },
new() { MenuHeader = "Notification", Key = MenuKeys.MenuKeyNotification },
new() { MenuHeader = "Notification", Key = MenuKeys.MenuKeyNotification, Status = "New"},
new() { MenuHeader = "Number Displayer", Key = MenuKeys.MenuKeyNumberDisplayer, Status = "New" },
new() { MenuHeader = "Numeric UpDown", Key = MenuKeys.MenuKeyNumericUpDown },
new() { MenuHeader = "NumPad", Key = MenuKeys.MenuKeyNumPad },
@@ -54,7 +54,7 @@ public class MenuViewModel: ViewModelBase
new() { MenuHeader = "Timeline", Key = MenuKeys.MenuKeyTimeline },
new() { MenuHeader = "TreeComboBox", Key = MenuKeys.MenuKeyTreeComboBox },
new() { MenuHeader = "TwoTonePathIcon", Key = MenuKeys.MenuKeyTwoTonePathIcon},
new() { MenuHeader = "Toast", Key = MenuKeys.MenuKeyToast },
new() { MenuHeader = "Toast", Key = MenuKeys.MenuKeyToast, Status = "New"},
new() { MenuHeader = "ToolBar", Key = MenuKeys.MenuKeyToolBar },
new() { MenuHeader = "Time Box", Key = MenuKeys.MenuKeyTimeBox },
};

View File

@@ -196,19 +196,19 @@ public abstract class MessageCard : ContentControl
{
switch (NotificationType)
{
case Avalonia.Controls.Notifications.NotificationType.Error:
case NotificationType.Error:
PseudoClasses.Add(PC_Error);
break;
case Avalonia.Controls.Notifications.NotificationType.Information:
case NotificationType.Information:
PseudoClasses.Add(PC_Information);
break;
case Avalonia.Controls.Notifications.NotificationType.Success:
case NotificationType.Success:
PseudoClasses.Add(PC_Success);
break;
case Avalonia.Controls.Notifications.NotificationType.Warning:
case NotificationType.Warning:
PseudoClasses.Add(PC_Warning);
break;
}