.NET MAUIでプロジェクトを新規作成し、対象をWindowsで実行すると特大サイズのウインドウが表示されます。
解像度1920×1080だと1426×752となりました。そんなに大きくなくていい。と思います。
ということで初期表示のウインドウサイズを変更していきます。

初期表示時のウインドウサイズ変更方法
ウインドウサイズの指定はxamlではなくTestMaui/Platforms/Windows/App.xaml/App.xaml.csで行います。
コンストラクタのAPP()内のthis.InitializeComponent();の後ろが追加箇所です。

using Microsoft.UI;
using Microsoft.UI.Windowing;
using Windows.Graphics;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace TestMaui.WinUI;
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
int WindowWidth = 600;
int WindowHeight = 500;
Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
{
var mauiWindow = handler.VirtualView;
var nativeWindow = handler.PlatformView;
nativeWindow.Activate();
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
appWindow.Resize(new SizeInt32(WindowWidth, WindowHeight));
});
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
TestMaui/App.xaml/App.xaml.csでも出来ますがプラットフォーム側に書いた方がいいのかなと思ってます。
namespace sinoalice_event_loop_4_android;
#if WINDOWS
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Windows.Graphics;
#endif
public partial class App : Application
{
private const int WindowWidth = 600;
private const int WindowHeight = 500;
public App()
{
InitializeComponent();
Microsoft.Maui.Handlers.WindowHandler.Mapper.AppendToMapping(nameof(IWindow), (handler, view) =>
{
#if WINDOWS
var mauiWindow = handler.VirtualView;
var nativeWindow = handler.PlatformView;
nativeWindow.Activate();
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(nativeWindow);
WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(windowHandle);
AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
appWindow.Resize(new SizeInt32(WindowWidth, WindowHeight));
#endif
});
MainPage = new AppShell();
}
}
実行すると小さくなりました。



コメント