net maui 主窗口使用菜单功能打开子窗口的效果类似MID

张中伟 80 信誉分
2025-03-08T04:00:28.03+00:00

我想用MAUI实现WINforM 的主从窗口效果。如果实现不了就多窗口也行。结果就是菜单点击后,弹出指定的PAGE。如果本来就存在那么前端显示,不重复显示。安卓上就直接跳转过去就行。如果也可以多窗口就更好。我的代码只能打开一次。还是在WIN下。麻烦给出最简洁的代码,谢谢。。

<ContentPage.MenuBarItems>

 <MenuBarItem Text="pages">

     <MenuFlyoutSubItem Text="all">

         <MenuFlyoutItem Text="page1" Clicked="MenuFlyoutItem_Clicked"/>

         <MenuFlyoutSeparator />

     </MenuFlyoutSubItem>

 </MenuBarItem>
``` </ContentPage.MenuBarItems>

namespace MauiApp1;

public partial class MainPage : ContentPage

{

```scala
public MainPage()

{

    InitializeComponent();

}

public Window? win1 = null;

private void MenuFlyoutItem_Clicked(object sender, EventArgs e)

{

    if (win1 == null)

    {

        win1 = new(new pages.Page1());

        if (DeviceInfo.Platform == DevicePlatform.WinUI)

        {

            App.Current?.OpenWindow(win1);

        }

        else if (DeviceInfo.Platform == DevicePlatform.Android)

        {

            //安卓怎么打开

        }

        else

        {

            //

        }

    }

    else

    {

        if (DeviceInfo.Platform == DevicePlatform.WinUI)

        {

            //这里无法再打开也会报错

            //var winUIWindow = win1.Handler?.PlatformView as Microsoft.UI.Xaml.Window;

            //if (winUIWindow != null)

            //{

            //    winUIWindow.Activate();

            //}

        }


        

    }

}
```}

.NET MAUI
.NET MAUI
一种 Microsoft 开源框架,用于构建跨移动设备、平板电脑、台式机的原生设备应用程序。
136 个问题
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 80,771 信誉分 Microsoft 外部员工
    2025-03-11T09:10:40.2066667+00:00

    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.

    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。