Avalonia是个跨平台的框架,具体的更多基本知识我就不在赘述。Avalonia框架的功能和WPF的功能基本相似,很多写法也可以套用,但是还是有很多细节不一致的,这就需要再运用中总结。本文主要是描述Avalonia页面怎么固定位置和判断当前屏幕是横屏还是竖屏。
判断当前屏幕是横屏还是竖屏主要是通过获取屏幕的宽和高来实现:
Avalonia横屏;
Screens.Primary.Bounds.Width> Screens.Primary.Bounds.Height
Avalonia竖屏;
Screens.Primary.Bounds.Width < Screens.Primary.Bounds.Height
WPF横屏;
Screen.PrimaryScreen.Bounds.Width > Screen.PrimaryScreen.Bounds.Height
WPF竖屏;
Screen.PrimaryScreen.Bounds.Width < Screen.PrimaryScreen.Bounds.Height
Avalonia固定窗口位置位置:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Window_Loaded("", null);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Screen[] screens = Screens.All.ToArray();
foreach (var s in screens)
{
if (s.Primary == false)
{
this.WindowState = WindowState.Normal;
this.WindowStartupLocation = WindowStartupLocation.Manual;
this.Position = new Avalonia.PixelPoint(s.Bounds.X, s.Bounds.Y);
}
}
}
}
WPF固定窗口位置位置:
this.StartPosition = FormStartPosition.Manual;
this.Left = s.Bounds.X;
this.Top = s.Bounds.Y;
Avalonia和WPF的具体位置可以在上文中的 s.Bounds.X 和 s.Bounds.Y 后加具体的值,根据自己的需求进行调整。
如果需要居中显示的话:
Avalonia:
WindowStartupLocation = WindowStartupLocation.CenterScreen;
WPF:
StartPosition = FormStartPosition.CenterScreen;
WindowStartupLocation = WindowStartupLocation.CenterScreen;
希望可以帮助正在寻找这个问题解决方法的人。