问题描述
我正在创建一个 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' 指的是 MainWindow。
XScreenSize
和 YScreenSize
获取屏幕的大小。
我试图获取通知窗口的大小以计算其位置,但对我来说似乎不可能。
win.Height
返回 NaN(同样适用于 win.Width
)
win.Bounds
也无济于事。
win.ClientSize
也是如此。
我已经尝试了所有似乎与我相关的属性,但我无法获得新窗口的大小。
有什么方法可以在两个操作系统中都适用吗?
<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">
<TextBlock DockPanel.Dock="Top" Text="{Binding NTitle}" HorizontalAlignment="Center" FontWeight="Medium" />
<TextBlock Text="{Binding NMessage}" textwrapping="Wrap" Opacity=".8" Margin="2,2"/>
</DockPanel>
</Border>
</Window>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)