MainWindow.xaml.cs
using System;
using demo1;
using Microsoft.UI.Xaml;
using Microsoft.UI;
using Windows.Graphics;//注意要以上操作正确,否则会报错
namespace Demo1
{
public sealed partial class MainWindow : Window
{
Microsoft.UI.Windowing.AppWindow m_appWindow;
public MainWindow()
{
this.InitializeComponent();
ContentPage.Navigate(typeof(Page_TalkToSelf));
m_appWindow = GetAppWindowForCurrentWindow();
//m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen); // This line
// 调整窗口位置和大小,以屏幕像素为单位
m_appWindow.MoveAndResize(new RectInt32(_X: 560, _Y: 280, _Width: 480, _Height: 850));
}
private Microsoft.UI.Windowing.AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
return Microsoft.UI.Windowing.AppWindow.GetFromWindowId(myWndId);
}
}
}
注意 using的包不要导入错误!
总体思路就是获取窗口的handle句柄
根据句柄获得ID
根据id获得app Window
然后对这个app Window设置大小