I'm creating a C# .NET Core 5 application that's intended to run on both windows 10 and Linux using Avalonia as a GUI. I don't want to use the reactive api. I couldn't find out how to use the notification system that avalonia supports so I decided to create a separate window which stays on top for X seconds and displays a title and a message. The window disappears after X seconds or sooner if the user clicks on it.
This is part of the code:
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();
}
}
The window is called like this:
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' refers to the MainWindow.
XScreenSize
and YScreenSize
get the size of the screen.
I've tried to get the size of the notification window in order to calculate its placement but it seems impossible to me.
win.Height
returns NaN (the same applies for win.Width
)
win.Bounds
does not help either.
The same for win.ClientSize
.
I've tried every property that seemed relevant to me but I can't get the size of the new window.
Is there any way to do so that will work in both OS?
I suppose it should be obvious but I'm adding the xaml file for the notification window as well:
<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>