0

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>
pzogr
  • 424
  • 1
  • 12
  • 30
  • The `Window.ShowDialog` method does very similar thing, what's your research on it? – Shadows In Rain Apr 18 '21 at 09:41
  • The ShowDialog will block the parent window – pzogr Apr 18 '21 at 10:08
  • I mean how it works under the hood. – Shadows In Rain Apr 18 '21 at 11:21
  • @ShadowsInRain: I don't know. You'll have to close the dialog before proceeding with anything else. I don't want that. I just want to notify the user that something happened without preventing the user from interacting with the main window. – pzogr Apr 18 '21 at 11:48
  • Avalonia is an open-source software, you can literally see its source code: https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/Window.cs — search for `ShowDialog`. On other hand, using windows for simple pop-up message may be an overkill, you could try an overlay instead — see https://stackoverflow.com/q/62194012/1125702 – Shadows In Rain Apr 18 '21 at 12:37
  • @ShadowsInRain: I know I can see the code but I just need to get the size of the window! I don't have the time to check the code to see what's going on behind the scenes and then perhaps I might get what I want. My code is almost ready. The pop-up link is not what I was looking for. Thanks anyway... – pzogr Apr 18 '21 at 12:58

0 Answers0