0

我正在创建一个 C# .NET Core 5 应用程序,该应用程序旨在使用 Avalonia 作为 GUI 在 Windows 10 和 Linux 上运行。我不想使用反应式 api。我不知道如何使用 avalonia 支持的通知系统,所以我决定创建一个单独的窗口,它在顶部停留 X 秒并显示标题和消息。如果用户单击它,窗口会在 X 秒后或更早消失。

这是代码的一部分:

public class NotificationMessage : Window, INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    
    public string NMessage { get; set; }
    public string NTitle { get; set; }

    public NotificationMessage()
    {
        InitializeComponent();
    }

    
    public NotificationMessage(string title, string message, int Xparent, int Yparent, int time)
    {
        DataContext = this;
        NMessage = message;
        NTitle = title;
        Timer timer = new Timer(time);
        timer.AutoReset = false;
        
        InitializeComponent();
        timer.Elapsed += OnTime;
        timer.Start();
        double height = this.Height;
        double width = this.ClientSize.Width;
        double sum = height + width; // this line was added just to insert a breakpoint
        //System.Diagnostics.Debug.WriteLine($"{this.Bounds.Width}-{this.Bounds.Height}");
    }

    private void OnTime(object sender, System.EventArgs e)
    {
        
        Dispatcher.UIThread.InvokeAsync(() => { this.Close(); });
    }
    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }

    private void Tapped(object sender, Avalonia.Interactivity.RoutedEventArgs e)
    {
        
        this.Close();
    }
}

窗口是这样调用的:

XScreenSize = Screens.Primary.WorkingArea.Width;
YScreenSize = Screens.Primary.WorkingArea.Height;
var win = new NotificationMessage("my title", "my message", XScreenSize, YScreenSize, 3000);
win.Show(this);

“this”指的是主窗口。

XScreenSizeYScreenSize获取屏幕的大小。我试图获取通知窗口的大小以计算其位置,但对我来说似乎是不可能的。 win.Height返回 NaN (同样适用于win.Widthwin.Bounds也无济于事。对于win.ClientSize. 我已经尝试了所有与我相关的属性,但我无法获得新窗口的大小。有什么方法可以在这两个操作系统中工作吗?

我想这应该很明显,但我也在为通知窗口添加 xaml 文件:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             x:Class="NotificationMessage" HasSystemDecorations="False" CanResize="False" 
        Tapped="Tapped" SizeToContent="WidthAndHeight" FontSize="16" Topmost="True">
  <Border BorderThickness="2">
    <DockPanel Background="DodgerBlue" Margin="2,2,2,2">

      <TextBlock DockPanel.Dock="Top" Text="{Binding NTitle}" HorizontalAlignment="Center" FontWeight="Medium" />

      <TextBlock Text="{Binding NMessage}" TextWrapping="Wrap" Opacity=".8" Margin="2,2,2,2"/>

    </DockPanel>
  </Border>

</Window>
4

0 回答 0