Hello,
首先 MenuBarItem 只能用于Mac Catalyst 和Windows. Android 系统上没有 MenuBarItem且无法触发MenuFlyoutItem_Clicked 事件
对于windows 或者Mac Catalyst, 我们可以通过在Microsoft.Maui.Controls.Application.Current.Windows 中去查找是否有相同title的page, 如果有,就利用 Microsoft.Maui.Controls.Application.Current?.ActivateWindow(window),把打开过的windows 调到前台,注意请加一个Task.Delay(100),如果不加这个delay, 我这边测下来,当window 在后台,没有最小化的情况下无法调到前台。
如果在Microsoft.Maui.Controls.Application.Current.Windows 中没有相同title的page, 就去OpenWindow.
private async void MenuFlyoutItem_Clicked(object sender, EventArgs e)
{
LoginPage login = new LoginPage();
Microsoft.Maui.Controls.Window secondWindow = new Microsoft.Maui.Controls.Window(login);
if (Microsoft.Maui.Controls.Application.Current.Windows.Any(t => t.Page.Title.Equals(login.Title)))
{
var window = Microsoft.Maui.Controls.Application.Current.Windows.Where(t => t.Page.Title.Equals(login.Title)).FirstOrDefault();
await Task.Delay(100);
Microsoft.Maui.Controls.Application.Current?.ActivateWindow(window);
}
else
{
Microsoft.Maui.Controls.Application.Current?.OpenWindow(secondWindow);
}
}
对于Android 平台, 你可以在button 点击事件中尝试以上代码,并且不要忘了把MainActivity 的LaunchMode 设置成LaunchMode.Multiple。
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.Multiple, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.