参考:https://www.likecs.com/ask-10337653.html#sc=200
【解决方案1】
根目录下的App.xaml.cs
【解决方案1】
根目录下的App.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | //设置界面宽高 #if WINDOWS using Microsoft.UI; using Microsoft.UI.Windowing; using Windows.Graphics; #endif namespace MauiAppForWin { public partial class App : Application { const int WindowWidth = 480; const int WindowHeight = 800; public App() { InitializeComponent(); MainPage = new MainPage(); //使用MainPage的页面配置 //var width = (int)MainPage.WidthRequest; //var height = (int)MainPage.HeightRequest; 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 }); } } } |
【解决方案2】
目录:Platforms/Windows/App.xaml.cs
重写OnLaunched
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using Microsoft.UI.Xaml; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. //设置界面宽高 using Microsoft.UI; using Microsoft.UI.Windowing; using Windows.Graphics; using WinRT.Interop; namespace MauiAppForWin.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(); } /// <summary> /// 设置界面宽高 /// </summary> /// <param name="args"></param> protected override void OnLaunched(LaunchActivatedEventArgs args) { base .OnLaunched(args); var currentWindow = Application.Windows[0].Handler.PlatformView; IntPtr _windowHandle = WindowNative.GetWindowHandle(currentWindow); var windowId = Win32Interop.GetWindowIdFromWindow(_windowHandle); AppWindow appWindow = AppWindow.GetFromWindowId(windowId); appWindow.Resize( new SizeInt32(350, 600)); } protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2018-12-29 下载网络资源-带下载进度