0

I am playing a Little with Windows 8 and building Windows Store Apps. Currently I am trying to create a popup. I want to tap on a button on a (primitive) user control which is hosted as the Child of a Windows::UI::Xaml::Controls::Primitives::Popup.
When I tapped on the button I want to do something and eventually close the popup. As I read here


Routed events outside the visual tree

... If you want to handle routed events from a Popup or ToolTip, place the handlers on specific UI elements that are within the Popup or ToolTip and not the Popup or ToolTip elements themselves. Don't rely on routing inside any compositing that is performed for Popup or ToolTip content. This is because event routing for routed events works only along the main visual tree. ...


I then created an ugly solution as to pass the popup (as parent) to my custom control.

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;
}

Is there any other way around? Thank you.

4

2 回答 2

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弹出窗口中托管的控件来获得正确的布局更新和软键盘行为。

于 2013-04-23T23:41:55.780 回答
0

在命名空间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上找到(虽然很差)。可以从 这里获得一个示例项目。

于 2013-04-25T20:23:43.523 回答