弹出导致应用崩溃[Windows 10] c# XAML

本文关键字:Windows XAML 崩溃 应用 | 更新日期: 2023-09-27 18:16:05

我希望程序在用户按住控件(在移动设备上)或当用户右键单击控件(在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时,按住事件有效,但当我右键单击PC时,它崩溃了!上面写着"没有附加的Flyout!"我不知道哪里不对。

"你试过右击事件了吗? "它有效吗?"

是和否:(

弹出导致应用崩溃[Windows 10] c# XAML

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

原来你必须命名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代替。

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