zoukankan      html  css  js  c++  java
  • Windows 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中,也能找到我的身影,感谢大家

      源代码请猛击
      

  • 相关阅读:
    [原]poj-2680-Choose the best route-dijkstra(基础最短路)
    [转]c/c++输入函数
    [原]poj-2524(裸并查集)
    [原]poj-1611-The Suspects(水并查集)
    ccnu-线段树-简单的区间更新(三题)
    团队博客(3)
    个人NABCD
    团队博客(2)
    团队博客(1)
    课堂练习:返回一个二维数组中最大子数组的和
  • 原文地址:https://www.cnblogs.com/sonyye/p/2368139.html
Copyright © 2011-2022 走看看