惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
The GitHub Blog
The GitHub Blog
腾讯CDC
A
About on SuperTechFans
H
Help Net Security
The Register - Security
The Register - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
V
Visual Studio Blog
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
DataBreaches.Net
I
InfoQ
L
Lohrmann on Cybersecurity
M
MIT News - Artificial intelligence
I
Intezer
博客园 - 聂微东
Webroot Blog
Webroot Blog
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
N
Netflix TechBlog - Medium
小众软件
小众软件
The Cloudflare Blog
WordPress大学
WordPress大学
N
News | PayPal Newsroom
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
C
Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
W
WeLiveSecurity
Schneier on Security
Schneier on Security

博客园 - dalgleish

C# Avalonia 22- SoundAndVideo- SpeechVoiceTest C# Avalonia 22- SoundAndVideo- SpeechRecognition C# Avalonia 22- SoundAndVideo- SpeechSynthesis C# Avalonia 22- SoundAndVideo- CodePlayback C# Avalonia 22- SoundAndVideo- SoundPlayerTest C# Avalonia 21- MenusAndToolbars- RibbonTest C# Avalonia 21- MenusAndToolbars- ProportionalStatusBar C# Avalonia 21- MenusAndToolbars- BasicToolbar C# Avalonia 21- MenusAndToolbars- ToolBar/Ribbon开源项目介绍和修复 C# Avalonia 21- MenusAndToolbars- SidebarMenu C# Avalonia 21- MenusAndToolbars- MixedMenus C# Avalonia 20 - WindowsMenu- 魔改Hyperlink - 使用例子 C# Avalonia 20 - WindowsMenu- 魔改Hyperlink C# Avalonia 20 - WindowsMenu- WebViewTest C# Avalonia 20 - WindowsMenu- WebView C# Avalonia 20 - WindowsMenu- ModernWindowTest C# Avalonia 20 - WindowsMenu- ModernWindow C# Avalonia 20 - WindowsMenu- TransparentWithShapes C# Avalonia 20 - WindowsMenu- TransparentBackground C# Avalonia 20 - WindowsMenu- OpenFileTest C# Avalonia 20 - WindowsMenu- SavePostion C# Avalonia 19- DataBinding- DataGridGrouping C# Avalonia 19- DataBinding- DirectoryTreeView C# Avalonia 19- DataBinding- BoundTreeView C# Avalonia 19- DataBinding- CustomListViewTest
C# Avalonia 20 - WindowsMenu- CenterScreen
dalgleish · 2026-03-18 · via 博客园 - dalgleish

CenterScreen.axaml代码

<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"
        Width="400" Height="250"
        x:Class="AvaloniaUI.CenterScreen"
        Title="CenterScreen">
    <StackPanel Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Name="cmdCenter"
                Content="Center"
                Click="cmdCenter_Click"/>
    </StackPanel>
</Window>

CenterScreen.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace AvaloniaUI;

public partial class CenterScreen : Window
{
    public CenterScreen()
    {
        InitializeComponent();
    }
    private void cmdCenter_Click(object? sender, RoutedEventArgs e)
    {
        var screen = Screens.ScreenFromWindow(this) ?? Screens.Primary;
        if (screen is null)
            return;

        // 工作区(排除任务栏等)
        var workArea = screen.WorkingArea;

        // 当前窗口大小(像素)
        var windowWidth = (int)Bounds.Width;
        var windowHeight = (int)Bounds.Height;

        var x = workArea.X + (workArea.Width - windowWidth) / 2;
        var y = workArea.Y + (workArea.Height - windowHeight) / 2;

        Position = new PixelPoint(x, y);
    }
}

运行效果

image