Windows Phone中的弹出面板

本文关键字:Phone Windows | 更新日期: 2023-09-27 18:27:25

我想创建一个控件,它的行为与Windows Phone的键盘按钮类似。当用户点击并按住控件时,它应该展开以显示其他选项。

虽然我通常知道如何实现这个控件,但我不知道如何显示面板,这将超出控件的边界。如果有一种方法可以在Windows Phone中实现这种效果(而且应该是-键盘以这种方式显示),我该怎么做?

Windows Phone中的弹出面板

您可以将Popup控件添加到控件中,并通过将Popup的IsOpen属性设置为true来显示它。Popup可以显示在控件的上下文之外。这是一个非常俗气的UserControl,它显示了它的Popup on Hold:

<UserControl x:Class="PhoneApp48.WindowsPhoneControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480"
    Hold="UserControl_Hold">
    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
        <Rectangle HorizontalAlignment="Stretch" Height="100" Fill="Yellow" />
        <Popup x:Name="myPopup" IsOpen="False">
            <StackPanel Width="{Binding ElementName=LayoutRoot, Path=ActualWidth}" Background="{StaticResource PhoneBackgroundBrush}">
                 <TextBlock TextWrapping="Wrap" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." />
                 <Button Click="Button_Click">Close</Button>
            </StackPanel>
        </Popup>
    </Grid>
</UserControl>

代码:

private void UserControl_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
    myPopup.IsOpen = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    myPopup.IsOpen = false;
}