Last active
October 28, 2022 17:54
-
-
Save huanlin/30f704e641987a39603cde8f8cf8e3fb to your computer and use it in GitHub Desktop.
Change size and position when a window is activated. For MAUI on Windows platform.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class App : Application | |
{ | |
public App() | |
{ | |
InitializeComponent(); | |
MainPage = new AppShell(); | |
} | |
protected override Window CreateWindow(IActivationState activationState) | |
{ | |
Window window = base.CreateWindow(activationState); | |
window.Activated += Window_Activated; | |
return window; | |
} | |
private async void Window_Activated(object sender, EventArgs e) | |
{ | |
#if WINDOWS | |
const int DefaultWidth = 1024; | |
const int DefaultHeight = 800; | |
var window = sender as Window; | |
// 變更視窗大小 | |
window.Width = DefaultWidth; | |
window.Height = DefaultHeight; | |
// 給它一點時間來把「變更視窗大小」的操作確實完成。 | |
await window.Dispatcher.DispatchAsync(() => { }); | |
// 取得主要顯示器的資訊 | |
var disp = DeviceDisplay.Current.MainDisplayInfo; | |
// 將視窗置於螢幕中央 | |
window.X = (disp.Width / disp.Density - window.Width) / 2; | |
window.Y = (disp.Height / disp.Density - window.Height) / 2; | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment