Windows Phone 7 IME升起時,保持頁面不被推起

來源:互聯網
上載者:User

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

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

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

  

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

      方案基本原理是,我們能夠得到當前頁面被推起的高度,這時我們把頁面的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中,也能找到我的身影,感謝大家

  原始碼請猛擊
  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.