如何在屏幕的上边缘“粘贴”控件?

时间:2013-04-26 07:27:38

标签: silverlight windows-phone-7 silverlight-4.0

我有一个自定义控件,显示一些统计数据,需要始终放在WP7屏幕的顶部边缘。但是,当用户在文本框上输入内容时,软键盘会弹出。并将自定义控件移出屏幕。我想确保自定义控件始终可见,即使弹出软键盘也是如此。有谁知道怎么做?

2 个答案:

答案 0 :(得分:2)

你必须使用一些“魔法”。 “魔术”是指RenderTransform

解决方案很简单 - 您需要移动自定义控件(向下,键盘可见;向上,隐藏时)。
检查this valuable post - 它必须帮助你。

问候。

答案 1 :(得分:2)

<phone:PhoneApplicationPage
    x:Class="Test.Keyboard.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="PortraitOrLandscape"
    >
    <Grid x:Name="LayoutRoot" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="WINDOWS PHONE" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="developer's ?" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
        <Grid Grid.Row="1" Margin="12,0,12,0"></Grid>
        <TextBox Grid.Row="2" LostFocus="TextBoxLostFocus"/>
    </Grid>
</phone:PhoneApplicationPage>




public partial class MainPage : PhoneApplicationPage
    {
        private const double LandscapeShift = -259d;
        private const double LandscapeShiftWithBar = -328d;
        private const double Epsilon = 0.00000001d;
        private const double PortraitShift = -339d;
        private const double PortraitShiftWithBar = -408d;

        public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register("TranslateY", typeof(double), typeof(MainPage), new PropertyMetadata(0d, OnRenderXPropertyChanged));

        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPageLoaded;
        }

        public double TranslateY
        {
            get { return (double)GetValue(TranslateYProperty); }
            set { SetValue(TranslateYProperty, value); }
        }

        private static void OnRenderXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((MainPage)d).UpdateTopMargin((double)e.NewValue);
        }

        private void MainPageLoaded(object sender, RoutedEventArgs e)
        {
            BindToKeyboardFocus();
        }

        private void BindToKeyboardFocus()
        {
            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
            if (frame != null)
            {
                var group = frame.RenderTransform as TransformGroup;
                if (group != null)
                {
                    var translate = group.Children[0] as TranslateTransform;
                    var translateYBinding = new Binding("Y");
                    translateYBinding.Source = translate;
                    SetBinding(TranslateYProperty, translateYBinding);
                }
            }
        }

        private void UpdateTopMargin(double translateY)
        {
            if (IsClose(translateY, LandscapeShift) || IsClose(translateY, PortraitShift)
||IsClose(translateY, LandscapeShiftWithBar) || IsClose(translateY, PortraitShiftWithBar)
)
            {
                LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
            }
        }

        private bool IsClose(double a, double b)
        {
            return Math.Abs(a - b) < Epsilon;
        }

        private void TextBoxLostFocus(object sender, RoutedEventArgs e)
        {
            LayoutRoot.Margin = new Thickness();
        }
    }

祝你好运....