C# 如何获得Avalonia窗口的尺寸?
我正在创建一个C#.NET Core 5应用程序,它打算使用Avalonia作为GUI在windows 10和Linux上运行。我不想使用反应式api。我不知道如何使用avalonia支持的通知系统,所以我决定创建一个单独的窗口,在顶部停留X秒,并显示标题和消息。如果用户单击该窗口,该窗口将在X秒或更短时间后消失 这是代码的一部分:C# 如何获得Avalonia窗口的尺寸?,c#,avalonia,C#,Avalonia,我正在创建一个C#.NET Core 5应用程序,它打算使用Avalonia作为GUI在windows 10和Linux上运行。我不想使用反应式api。我不知道如何使用avalonia支持的通知系统,所以我决定创建一个单独的窗口,在顶部停留X秒,并显示标题和消息。如果用户单击该窗口,该窗口将在X秒或更短时间后消失 这是代码的一部分: public class NotificationMessage : Window, INotifyPropertyChanged { public eve
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”指的是主窗口
XScreenSize
和YScreenSize
获取屏幕大小。
我试图获得通知窗口的大小,以便计算其位置,但对我来说这似乎是不可能的。
win.Height
返回NaN(这同样适用于win.Width
)
win.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>
Window.ShowDialog
方法做了非常类似的事情,你对它做了什么研究?ShowDialog会阻止父窗口我是说它在引擎盖下是如何工作的。@ShadowsInRain:我不知道。您必须先关闭对话框,然后才能继续执行其他操作。我不想那样。我只想通知用户发生了一些事情,但不会阻止用户与主窗口交互。Avalonia是一款开源软件,您可以直接看到它的源代码:-搜索ShowDialog
。另一方面,使用windows来显示简单的弹出消息可能有点过头了,您可以尝试使用覆盖来代替-请参阅