2013-04-23 56 views
0

我在玩Windows 8並創建Windows應用商店應用。目前我正在嘗試創建一個彈出窗口。我想點擊作爲Windows::UI::Xaml::Controls::Primitives::PopupChild託管的(原始)用戶控件上的按鈕。
當我點擊按鈕時,我想要做一些事情並最終關閉彈出窗口。因爲我讀的可視化樹外here如何處理WinRT彈出窗口中的RoutedEvents


路由事件

... 如果你想處理從彈出或工具提示路由事件,將處理程序上都彈出內的特定UI元素或ToolTip,而不是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; 
} 

是否有其他解決方法?謝謝。

回答

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(ra儘管如此,窮人)。示例項目可從 here獲得。

1

你可以把彈出你的用戶控件的根像這樣:

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

然後從用戶控件中隱藏:

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

這是不是一個真正的更好的方法,只是不同。我認爲您應該使用HorizontalOffset而不是Canvas.Left定位,並且通常彈出式窗口是某個面板的子項,以便在彈出窗口中託管TextBox控件以獲得正確的佈局更新和軟鍵盤行爲。

+0

謝謝你的主意。 Canvas :: Left的原因是我想在'Image'控件上使用彈出窗口,它可以自由定位在Canvas上,實際上沒有其他佈局容器用於此目的。 – 2013-04-24 18:16:58

相關問題