Avalonia 通知导致应用程序崩溃

Posted

技术标签:

【中文标题】Avalonia 通知导致应用程序崩溃【英文标题】:Avalonia notification causes the application to crash 【发布时间】:2021-12-31 21:23:15 【问题描述】:

我正在测试 Avalonia 通知(C#、Windows 10、Visual Studio Community 2019)。每当我按下按钮并更改属性“问候”时,以下代码就会崩溃。

我想问题是图片中的备注所指示的。问题是如何避免这种情况?更改 Greeting 属性的内容后如何显示通知? 代码如下:

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Controls.Notifications;
using System;

namespace Avalonia_test_Notifications.Views

    public partial class MainWindow : Window
    
        public ViewModels.MainWindowViewModel ViewModel;
        public WindowNotificationManager Manager;
        public MainWindow()
                    
            InitializeComponent();
            this.DataContext = ViewModel;
#if DEBUG
            this.AttachDevTools();
#endif
    


    private void InitializeComponent()
    
        AvaloniaXamlLoader.Load(this);
        Manager = new WindowNotificationManager(this);
        ViewModel = new ViewModels.MainWindowViewModel(Manager);
    

    private void ShowNotification(object sender, RoutedEventArgs e)
    
        ViewModel.ClickEvent(sender, e);
    
    

    


using System;
using System.Collections.Generic;
using System.Text;
using ReactiveUI;
using System.Reactive;

namespace Avalonia_test_Notifications.ViewModels

    public class MainWindowViewModel : ViewModelBase
    
        private string _greeting = "Greetings";
        public string Greeting
        
            get => _greeting;
            set 
             
                this.RaiseAndSetIfChanged(ref _greeting, value); 
            
        
        public Avalonia.Controls.Notifications.Notification Notification;
        public Avalonia.Controls.Notifications.IManagedNotificationManager Manager;
        public string [] stringArray;
        public MainWindowViewModel(Avalonia.Controls.Notifications.IManagedNotificationManager manager)
        
            stringArray = new string[3]  "message 1", "message 2", "message 3" ;
            Manager = manager;            
            this.WhenAnyValue(x => x.Greeting).Subscribe(x=>ShowNotification(x));
        

        public void ClickEvent(object sender, Avalonia.Interactivity.RoutedEventArgs e)
        
            System.Random rand = new System.Random();
            int x = rand.Next(0, 3);
            Greeting = stringArray[x];
        
        
        public void ShowNotification(string s)
        
            Notification = new Avalonia.Controls.Notifications.Notification("TestNotification", s, Avalonia.Controls.Notifications.NotificationType.Information, TimeSpan.FromSeconds(0));
            Manager.Show(Notification);
        
    

最后:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:Avalonia_test_Notifications.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="Avalonia_test_Notifications.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="Avalonia_test_Notifications">

    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>
  <StackPanel Orientation="Vertical">
    <TextBlock Text="Binding Greeting" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    <Button Content="Notification" Click="ShowNotification"></Button>
</StackPanel>
</Window>

【问题讨论】:

【参考方案1】:

似乎 MainWindow 在与它交互之前需要一些时间来准备。我找到的解决方案是仅在 IsActive 为真时才响应更改。我在创建 ViewModel 时传递了对 MainWindow 的引用,而不是传递对 NotificationManager 的引用。

 public MainWindowViewModel(Views.MainWindow Window)
        
            stringArray = new string[3]  "message 1", "message 2", "message 3" ;
            Manager = new Avalonia.Controls.Notifications.WindowNotificationManager(Window);
            
            this.WhenAnyValue(x => x.Greeting).WhereNotNull()
                .Subscribe(x=>
            
                if (Window.IsActive)
                ShowNotification(x);
            
            );
        

【讨论】:

以上是关于Avalonia 通知导致应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

由于 Activity 意图,Android 通知点击导致应用程序崩溃

由于我的应用程序的后台通知导致三星设备上经常崩溃的消息

CALayercornerRadius 导致应用程序间歇性崩溃

iOS - 从本地通知启动时托管对象上下文崩溃应用程序

Titanium - 确定应用程序崩溃的原因

应用程序被杀死时点击推送通知后应用程序崩溃