我正在玩一点Windows 8和建立Windows商店应用程序。目前,我正在尝试创建一个弹出窗口。我想点击(原语)用户控件上的一个按钮,该控件作为Child的Windows::UI::Xaml::Controls::Primitives::Popup托管。
当我点击按钮,我想做些什么,并最终关闭弹出窗口。我读这里的时候
在可视树之外路由事件
..。如果要处理弹出或ToolTip中的路由事件,请将处理程序放在弹出或ToolTip中的特定UI元素上,而不是弹出或ToolTip元素本身。不要依赖于为弹出或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;
}还有别的路吗?谢谢。
发布于 2013-04-25 20:23:43
在名称空间Windows::UI::Popups中,有一个PopupMenu-class,它提供了我所需要的东西。它创建了一个类似于经典桌面应用程序中的上下文菜单的弹出窗口。
首先,使用以下简单方法创建一个新对象
Windows::UI::Popups::PopupMenu^ popupmenu = ref new Windows::UI::Popups::PopupMenu();之后,使用以下方法填充最多6个项的弹出窗口
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-23 23:41:55
您可以将弹出放在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控件的软键盘行为。
https://stackoverflow.com/questions/16178058
复制相似问题