-
普通Page模式
在App.xaml.cs
的构造函数中替换MainPage
的对象。1 2 3 4 5 6
public App() { InitializeComponent(); MainPage = new NewPage1(); }
-
MVVM模式
在App.xaml.cs
中重写CreateWindow
方法。1 2 3 4 5 6 7 8 9 10
protected override Window CreateWindow(IActivationState? activationState) { NewPage1? desktopShellService = Handler.MauiContext!.Services.GetService<NewPage1>(); Window window = new Window(desktopShellService) { Width = DeviceDisplay.Current.MainDisplayInfo.Width / 2, Height = DeviceDisplay.Current.MainDisplayInfo.Height / 2, }; return window; }
-
启动页切换到主页面
在页面的Loaded事件或其他事件中加入MainPage对象切换。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
async Task Loaded() { await Task.Delay(5000); _ = Task.Run(() => { AppShell? desktopShellService = Application.Current.Handler.MauiContext!.Services.GetService<AppShell>(); if (desktopShellService != null && desktopShellService != Application.Current.MainPage) { MainThread.BeginInvokeOnMainThread(() => { Application.Current.MainPage = desktopShellService; }); } }); //AppShell? desktopShellService = Application.Current.Handler.MauiContext!.Services.GetService<AppShell>(); //if (desktopShellService != null && desktopShellService != Application.Current.MainPage) //{ // Application.Current.MainPage = desktopShellService; //} }