繁体   English   中英

UWP Grid.ContextFlyout无法打开

[英]UWP Grid.ContextFlyout not opening

在UWP应用程序中,我在主窗口中有一个列表视图,在辅助窗口中。 我的列表视图中的每个列表视图项都是一个Grid并且我已经Grid.ContextFlyout网格设置了Grid.ContextFlyout 该ContextFlyout包含MenuFlyout与4 MenuFlyoutItem

我打开我的应用程序,在主窗口列表视图中,我右键单击一个项目。 我看到ContextFlyout打开并且EventHandler<object> Opened被触发。

现在,我打开应用程序的辅助窗口,然后关闭主窗口。 我再次通过从开始菜单打开应用程序来打开应用程序的主窗口。

现在,如果在列表视图中右键单击之前单击的项目,则可以触发EventHandler<object> Opened ,但是上下文弹出窗口未在UI中打开。

仅在上述情况下才会出现此问题(1.打开应用程序,2.右键单击该项目,3.打开辅助窗口,4.关闭主窗口,5.从开始菜单中再次打开应用程序的主窗口,6 。右键单击该项目)

下面是我的网格

<Grid
    Name="RootGrid">
    <Grid.ContextFlyout>
        <MenuFlyout
            x:Name="OptionsFlyout"
            Opening="Flyout_Opening" 
            Opened="Flyout_Opened"
            Closed="Flyout_Closed">
            <MenuFlyoutItem Name="Item1"/>
            <MenuFlyoutItem Name="Item2"/>
            <MenuFlyoutItem Name="Item3"/>
            <MenuFlyoutItem Name="Item4"/>
        </MenuFlyout>
    </Grid.ContextFlyout>
    <TextBlock Text="MyGridItem"/>
</Grid>

在我的App.xaml.cs我在OnLaunched方法中使用以下代码来恢复主窗口

protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
    Window.Current.Dispatcher.RunOnUIThread(async () =>
    {
      tryShow = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(ApplicationView.GetApplicationViewIdForWindow(CoreApplication.GetCurrentView().CoreWindow), ViewSizePreference.Default, e.CurrentlyShownApplicationViewId, ViewSizePreference.Default);
    });
}

打开/恢复之前关闭的主窗口时,我是否丢失任何东西? 还是有什么办法可以解决这个问题?

以下是我为重现此问题而创建的示例应用程序的github链接。 UWP网格上下文菜单

仅在上述情况下才会出现此问题(1.打开应用程序,2.右键单击该项目,3.打开辅助窗口,4.关闭主窗口,5.从开始菜单中再次打开应用程序的主窗口,6 。右键单击该项目)

我可以重现此问题。 我已经报告了相关的团队。 您也可以在我们的反馈中心中提交。

这是您的解决方法。 您可以使用FlyoutBase.AttachedFlyout代替“ ContextFlyout”。 您只需要注册Grid的RightTapped事件并添加一些如下代码:

<Grid Name="RootGrid" Height="50" Width="200" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Green" RightTapped="RootGrid_RightTapped">
       <FlyoutBase.AttachedFlyout>
            <MenuFlyout
        x:Name="OptionsFlyout"
            Opening="MenuFlyout_Opening" 
            Opened="FolderOptionsFlyout_Opened"
            Closed="FolderOptionsFlyout_Closed">
                        <MenuFlyoutItem Name="Item1" Text="Item1"/>
                        <MenuFlyoutItem Name="Item2" Text="Item2"/>
                        <MenuFlyoutItem Name="Item3" Text="Item3"/>
                        <MenuFlyoutItem Name="Item4" Text="Item4"/>
             </MenuFlyout>
       </FlyoutBase.AttachedFlyout>
       <TextBlock Text="{x:Bind}" Height="150" Width="100"/>
</Grid>
private void RootGrid_RightTapped(System.Object sender, RightTappedRoutedEventArgs e)
{
    var fe = sender as FrameworkElement;
    var menu = Flyout.GetAttachedFlyout(fe);
    menu.ShowAt(fe);
}

根据文档,关闭主窗口应该将其隐藏。
一个应用程序显示多个视图

“如果打开了辅助视图,则可以隐藏主视图的窗口-例如,通过单击窗口标题栏中的关闭(x)按钮-但其线程保持活动状态。”

从1703开始,您可以让主窗口处理关闭请求事件。 一旦关闭,代码可以同时隐藏主窗口切换到副窗口。 然后通过将Handled属性设置为true来告诉系统您已经自己处理了关闭操作。

在appxmanifest中添加ConfirmAppClose功能。

<Capabilities>
     <Capability Name="internetClient" />
     <rescap:Capability Name="confirmAppClose"/> 
</Capabilities>

现在处理CloseRequested事件。 代码如下所示:

    private int MainViewId;
    private int SecondViewId;

    public MainPage()
    {
        this.InitializeComponent();
        SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += MainPage_CloseRequested;
    }

    private async void MainPage_CloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
    {   
        // Switch to Secondary window, Hide main window                          
        await ApplicationViewSwitcher.SwitchAsync(
                SecondViewId,
                MainViewId,
                ApplicationViewSwitchingOptions.ConsolidateViews);

        // The close was handled, don't do anything else
        e.Handled = true;
    }

    private async void Button_Tapped(object sender, TappedRoutedEventArgs e)
    {
        MainViewId = ApplicationView.GetForCurrentView().Id;
        var newCoreApplicationView = CoreApplication.CreateNewView();             

        await newCoreApplicationView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            SecondViewId = ApplicationView.GetForCurrentView().Id;

            Window SecondWindow = Window.Current;
            var frame = new Frame();
            frame.Navigate(typeof(Assets.SecondWindow));
            SecondWindow.Content = frame;
            SecondWindow.Activate();
        });

        await ApplicationViewSwitcher.TryShowAsStandaloneAsync(SecondViewId, ViewSizePreference.Default);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM