Windows Phone 7 输入法升起时,保持页面不被推起

junwong 发布于 2012/03/05 16:37
阅读 340
收藏 0

在很多页面中,页面底部会有相应的文本框,而当用户focus输入框时,在wp7中,默认情况下,页面会被完全推到屏幕上方,当然很多时候交互师会觉得这样不够好,他们常常希望当输入法升起时,页面(特别是上面的主题文字)不被推起来。所以就有了这篇文章。

这篇文章是在国外一程序员提出来的方案,我现在将它再次呈现给大家,希望大家遇到这样的问题能更快的解决。

同时我也不得不感谢一直支持我的卤面网版主,是他让我提起兴趣写了这么一篇文章,再次感谢卤面网,一个非常不错的wp7开发论坛,后面我也将再次向大家发布几篇高质量文章,请大家到卤面上找我吧,呵呵

  

      在wp7中其实没有很好的方案,来控制页面不被输入法推起。但这里有一个折衷的方案,(暂时能解决这个问题)

      方案基本原理是,我们能够得到当前页面被推起的高度,这时我们把页面的margiin Top,设置为这个高度,那么让人能错觉的感觉到页面没有被推起, 

 

1.创建页面

<phone:PhoneApplicationPage
x:Class= " Test.Keyboard.MainPage "
xmlns= " http://schemas.microsoft.com/winfx/2006/xaml/sentation "
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= " " 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[ ] 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( , -translateY, , );
}
}

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中,也能找到我的身影,感谢大家

  源代码请猛击


原文链接:http://www.cnblogs.com/sonyye/archive/2012/02/25/2368139.html
加载中
返回顶部
顶部