【问题标题】:How to handle RoutedEvents in WinRT Popup如何在 WinRT 弹出窗口中处理 RoutedEvents
【发布时间】:2013-04-23 19:57:28
【问题描述】:

我正在玩一点 Windows 8 并构建 Windows 应用商店应用程序。目前我正在尝试创建一个弹出窗口。我想点击作为Windows::UI::Xaml::Controls::Primitives::PopupChild 托管的(原始)用户控件上的按钮。
当我点击按钮时,我想做一些事情并最终关闭弹出窗口。当我读到here


可视化树之外的路由事件

... 如果要处理来自 Popup 或 ToolTip 的路由事件,请将处理程序放在 Popup 或 ToolTip 内的特定 UI 元素上,而不是 Popup 或 ToolTip 元素本身。不要依赖于为 Popup 或 ToolTip 内容执行的任何合成中的路由。这是因为路由事件的事件路由仅沿主视觉树工作。 ...


然后我创建了一个丑陋的解决方案,将弹出窗口(作为父级)传递给我的自定义控件。

void Rotate::MainPage::Image1_RightTapped_1(Platform::Object^ sender, Windows::UI::Xaml::Input::RightTappedRoutedEventArgs^ e)
{
    Popup^ popup = ref new Popup();
    ImagePopup^ popupchild = ref new ImagePopup(popup); //pass parent's reference to child 
    popupchild->Tapped += ref new TappedEventHandler(PopupButtonClick);
    popup->SetValue(Canvas::LeftProperty, 100);
    popup->SetValue(Canvas::TopProperty, 100);
    popup->Child = popupchild;
    popup->IsOpen = true;
}

void PopupButtonClick(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e){
    ImagePopup^ imgPop = static_cast<ImagePopup^>(sender);
    if(imgPop != nullptr){
        imgPop->CloseParent();
        e->Handled = true;
    }
    e->Handled = false;
}

还有其他方法吗?谢谢。

【问题讨论】:

    标签: c++ xaml windows-runtime routed-events


    【解决方案1】:

    您可以像这样将弹出窗口放在 UserControl 的根目录中:

    <UserControl
        x:Class="App15.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App15"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
    
        <Grid>
            <Popup
                x:Name="popup"
                IsOpen="True">
                <Button
                    Content="Close"
                    Click="Button_Click" />
            </Popup>
        </Grid>
    </UserControl>
    

    然后在 UserControl 中隐藏它:

    void App15::MyUserControl::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        popup->IsOpen = false;
    }
    

    这并不是一个更好的方法,只是不同而已。我认为您可能应该使用HorizontalOffset 而不是Canvas.Left 定位,并且通常最好将弹出窗口设置为某个面板的子项,以使用弹出窗口中托管的TextBox 控件获得正确的布局更新和软键盘行为。

    【讨论】:

    • 感谢您的想法。 Canvas::Left is that I want to use the popup on an Image-control, which can be freely positioned on a Canvas`的原因,其实没有别的布局容器可以做这个的。
    【解决方案2】:

    在命名空间Windows::UI::Popups 中有PopupMenu-class,它提供了我所需要的。它会创建一个类似于经典桌面应用程序中的上下文菜单的弹出窗口。

    首先,使用简单的方法创建一个新对象

    Windows::UI::Popups::PopupMenu^ popupmenu = ref new Windows::UI::Popups::PopupMenu();
    

    然后,使用最多六个项目填充弹出窗口

    popupmenu->Commands->Append(ref new Windows::UI::Popups::UICommand("Do Something",
    [this](IUICommand^ command){
    //your action here
    }));
    

    最终,使用您指定的UIElement 喜欢弹出窗口

    popupmenu->ShowAsync(Windows::Foundation::Point(xArg, yArg));
    

    该文档可在MSDN 上找到(虽然很差)。一个示例项目可以从 here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-11
      • 2022-11-22
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多