c# - Flyout 导致应用程序崩溃 [Windows 10] C# XAML

标签 c# xaml windows-10 windows-10-mobile

我希望程序在用户按住控件(在移动设备上)或当用户右键单击控件(在 PC 上)时显示附加的浮出控件。

这是我的 XAML:

<DataTemplate x:DataType="data:Cards" x:Key="card">
        <StackPanel x:Name="cardstack" Holding="cardstack_Holding" KeyDown="cardstack_KeyDown" >
            <StackPanel Background="Blue" Height="100" />
            <FlyoutBase.AttachedFlyout>
                <MenuFlyout x:Name="optionpass">
                    <MenuFlyoutItem x:Name="delete" Text="Delete" Click="delete_Click"/>
                </MenuFlyout>
            </FlyoutBase.AttachedFlyout>
        </StackPanel>
    </DataTemplate>

这是我的 C#:

    private void cardstack_Holding(object sender, HoldingRoutedEventArgs e)
    {
        FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
    }

    private void cardstack_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == Windows.System.VirtualKey.RightButton)
        {
            FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
        }
    }

当我在移动模拟器上点击并按住 Stackpanel 时,Holding 事件有效,但是当我右键单击我的 PC 时,它崩溃了!它说“没有附加的弹出窗口!”。我不知道哪里出了问题。

“您是否尝试过 RightTapped 事件?它有效吗?”

是和否:(

最佳答案

我刚刚找到了解决问题的方法。

原来你必须命名 MenuFlyout就像我的一样 x:Name = "option_menu" , 和 Flyoutbase.AttachedFlyout不能在 DataTemplate 中, 意味着你必须把它放在除 DataTemplate 以外的任何地方,以便 .cs 文件可以找到 MenuFlyout 的名称.

这是我的 C#:

public void cardstack_Holding(object sender, HoldingRoutedEventArgs e)
    {
        option_menu.ShowAt(sender as FrameworkElement);
        e.Handled = true;
    }

private void cardstack_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        Pointer pointr = e.Pointer;

        if (pointr.PointerDeviceType ==  Windows.Devices.Input.PointerDeviceType.Mouse)
        {
            Windows.UI.Input.PointerPoint pointrd = e.GetCurrentPoint(sender as UIElement);
            if (pointrd.Properties.IsRightButtonPressed)
            {
                option_menu.ShowAt(sender as FrameworkElement);
            }
        }
        e.Handled = true;
    }

请注意,在此之前我使用 ShowAttachedFlyout , 现在我用 option_menu.ShowAt .

KeyDown事件以某种方式不适用于我的应用程序,所以我使用了 PointerPressed相反。

希望这对您有所帮助。 (0w0)/

关于c# - Flyout 导致应用程序崩溃 [Windows 10] C# XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928821/

相关文章:

c# - 如何在 ASP.NET 5 中使用基于 IAppBuilder 的 Owin 中间件

windows-phone - Caliburn 微导航服务导致空引用异常

c# - Mvvm Light、UWP 和代码隐藏

wpf - 单独的 ResourceDictionary 中的 DataTemplate

python - 使用 Brother QL-800 标签打印机打印标签

c# - 使用 StaticResource 作为默认绑定(bind)与 x :Bind, 的绑定(bind)及其在 DataContext 中的差异

c# - 如何在 C# 中创建固定大小的字节数组用户类型?

c# - 混淆或保护 .Net 程序集的最佳方法

c# - 生成后事件执行 PowerShell

wpf - Brush 类型的绑定(bind)属性在异步调用时引发异常