Skip to content

Instantly share code, notes, and snippets.

@huanlin
Last active October 28, 2022 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huanlin/30f704e641987a39603cde8f8cf8e3fb to your computer and use it in GitHub Desktop.
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.
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