C# WINDOWS MAUI專案 全螢幕(maximize application) & 移除/隱藏標題列(Remove/Hide the Title Bar )
C# WINDOWS MAUI專案 全螢幕(maximize application) & 移除/隱藏標題列(Remove/Hide the Title Bar )
資料來源: https://stackoverflow.com/questions/72128525/net-maui-how-to-maximize-application-on-startup
https://github.com/dotnet/maui/issues/15142
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/windows
GITHUB: https://github.com/jash-git/MAUI_WinAPI_Object_test/tree/main/Code/01
Code ~ [MauiProgram.cs]
//---
//.net MAUI how to maximize application on startup
//https://stackoverflow.com/questions/72128525/net-maui-how-to-maximize-application-on-startup
using Microsoft.Maui.LifecycleEvents;
#if WINDOWS
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Windows.Graphics;
#endif
//---.net MAUI how to maximize application on startup
namespace MAUI_WinAPI_Object_test;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
//---
//.net MAUI how to maximize application on startup
https://stackoverflow.com/questions/72128525/net-maui-how-to-maximize-application-on-startup
#if WINDOWS
builder.ConfigureLifecycleEvents(events =>
{
events.AddWindows(wndLifeCycleBuilder =>
{
wndLifeCycleBuilder.OnWindowCreated(window =>
{
IntPtr nativeWindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);
WindowId win32WindowsId = Win32Interop.GetWindowIdFromWindow(nativeWindowHandle);
AppWindow winuiAppWindow = AppWindow.GetFromWindowId(win32WindowsId);
if(winuiAppWindow.Presenter is OverlappedPresenter p)
{
p.Maximize();
}
//---
//[Windows] How to completely hide the TitleBar?
//https://github.com/dotnet/maui/issues/15142
window.ExtendsContentIntoTitleBar = false;
switch (winuiAppWindow.Presenter)
{
case Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter:
overlappedPresenter.SetBorderAndTitleBar(false, false);
overlappedPresenter.Maximize();
break;
}
//---[Windows] How to completely hide the TitleBar?
});
});
});
#endif
//---.net MAUI how to maximize application on startup
return builder.Build();
}
}
Code ~ [MainPage.xaml.cs]
//using Android.App;
using Microsoft.Maui.Controls;
namespace MAUI_WinAPI_Object_test;
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
}
private void OnClosed(object sender, EventArgs e)
{
// Close the active window
//https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/windows
Application.Current.CloseWindow(GetParentWindow());
}
}
2 thoughts on “C# WINDOWS MAUI專案 全螢幕(maximize application) & 移除/隱藏標題列(Remove/Hide the Title Bar )”
C# WINDOWS MAUI專案 關閉程式
https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/windows
private void OnClosed(object sender, EventArgs e)
{
// Close the active window
Application.Current.CloseWindow(GetParentWindow());
}
winuiAppWindow.Move(new Windows.Graphics.PointInt32(0, 0)); //C# MAUI WINDOWS 程式指定視窗起始位置