Window Phone 7輸入法升起時,保留頁面不被推起

在很多頁面中,頁面底部會有相應的文本框,而當用戶focus輸入框時,在wp7中,默認情況下,頁面會被完全推到屏幕上方,當然很多時候交互師會覺得這樣不夠好,他們常常希望當輸入法升起時,頁面(特別是上面的主題文字)不被推起來。所以就有了這篇文章。

  這篇文章是在國外一程序員提出來的方案,我現在將它再次呈現給大家,希望大家遇到這樣的問題能更快的解決。

  同時我也不得不感謝一直支持我的滷麪網版主,是他讓我提起興趣寫了這麼一篇文章,再次感謝滷麪網,一個非常不錯的wp7開發論壇,後面我也將再次向大家發佈幾篇高質量文章,請大家到滷麪上找我吧,呵呵

在wp7中其實沒有很好的方案,來控制頁面不被輸入法推起。但這裏有一個折衷的方案,(暫時能解決這個問題)

方案基本原理是,我們能夠得到當前頁面被推起的高度,這時我們把頁面的margiin Top,設置爲這個高度,那麼讓人能錯覺的感覺到頁面沒有被推起,

1.創建頁面

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


2.cs代碼

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();
        }
    }


裏面可能一些變量是不必要的,不過原理很簡單,希望能解決大家的問題

我希望你喜歡!如果你有話要說,請到滷麪網(codewp7.com)問答區聯繫我,我會很高興知道你在想什麼。同時wp7交流QQ羣172765887中,也能找到我的身影,感謝大家

源代碼請猛擊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章