2

有什么方法可以在 Windows 8 Metro 中心显示弹出窗口?

VerticalAlignmentand设置HorizontalAlignment为“Center”,并将VerticsalOffsetandHorizontalOffset设置为 0 会导致屏幕中心左上角的弹出窗口。

有什么办法可以做得很好吗?

为了使这个问题更难,这个弹出窗口在快照视图中具有不同的大小,也应该居中。

有什么想法吗?

4

3 回答 3

2

希望这会有所帮助,如何将弹出窗口放在画布中然后操作画布......

XAML

<Canvas x:Name="myCanvas"
            HorizontalAlignment="Center"
            Height="127"
            VerticalAlignment="Center"
            Width="191"/>
    <Button Content="myButton"
            Height="100"
            Width="100"
            Click="myButton_Click"/>

C#

 private void myButton_Click(object sender, RoutedEventArgs e)
    {
        Popup myPopup = new Popup();
        myPopup.IsOpen = true;

        TextBox myTextbox = new TextBox();
        myTextbox.Text = "Your Message Here";

        myPopup.Child = myTextbox;

        myCanvas.Children.Add(myPopup);
    }

只是玩得开心尝试...

于 2012-09-01T10:56:39.397 回答
2

我把它放在我的 Xaml 中主网格的末尾,在其他所有内容之后,所以它肯定会出现在前面。

<Canvas x:Name="pCanvas" HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel x:Name="pStackPanel">

        <TextBlock x:Name="pText" Text="Enter Name of Playlist:" Margin="25,12" Width="300" />

        <TextBox x:Name="pInputBox" Margin="25,12" Width="300" />

        <Button x:Name="pButton" Content="OK" Height="30" Width="100" Click="pButton_Clicked" Margin="6,12" />

    </StackPanel>
</Canvas>
于 2013-03-13T20:27:00.573 回答
1

I also added the following code to my xaml.cs (code behind) file:

(see the xaml I posted previously)

I then just wire up any test button's _Tapped event with the myPopup() function below.

I still haven't quite got the hang of Stackpanel to get the message centered, etc, but I'm getting there!

    async void messageBox(String msg)
    {
        MessageDialog dialog = new MessageDialog(msg,"Alert");
        await dialog.ShowAsync();
    }

   private void pButton_Clicked(object sender, RoutedEventArgs e)
    {
        PLPopup.IsOpen = false;

        String str = pInputBox.Text;

        hidePopup();

        messageBox(str);
    }

    void hidePopup()
    {
        pCanvas.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        pStackPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        pText.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        pInputBox.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        pButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    }

    void showPopup()
    {
        pCanvas.Visibility = Windows.UI.Xaml.Visibility.Visible;
        pStackPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
        pText.Visibility = Windows.UI.Xaml.Visibility.Visible;
        pInputBox.Visibility = Windows.UI.Xaml.Visibility.Visible;
        pButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
    }

    private void myPopup(object sender, RoutedEventArgs e)
    {
        Brush myBrush = new SolidColorBrush(Windows.UI.Colors.Black);

        topAppBar.IsOpen = false;
        bottomAppBar.IsOpen = false;

        myBrush.Opacity = .5;

        PLPopup = new Popup();
        PLPopup.IsOpen = true;

        //PLPopup.Child = myTextbox;

        pCanvas.Background = myBrush;
        pCanvas.Children.Add(PLPopup);

        pCanvas.Width = this.ActualWidth;
        pCanvas.Height = this.ActualHeight;

        showPopup();
    }
于 2013-03-13T20:33:22.187 回答