1

我目前正在開發通用應用程序,但這裏有一個問題。我有用於用戶電話號碼的文本框的框架。 Initial PageInputPane無法正常工作

所以,我想改變我的LayoutRoot(網格)的高度,以便它可以適應可用空間。
爲此,我使用了InputPane.GetForCurrentView().ShowingInputPane.GetForCurrentView().Hiding。 這是我的代碼。

public UserRegistrationAuthorization_PhoneNumber() 
    { 
     this.InitializeComponent(); 
     LayoutRootInitialHeight = LayoutRoot.ActualHeight; 
     InputPane.GetForCurrentView().Showing += UserRegistrationAuthorization_PhoneNumber_Showing; 
     InputPane.GetForCurrentView().Hiding += UserRegistrationAuthorization_PhoneNumber_Hiding; 
    } 

private void UserRegistrationAuthorization_PhoneNumber_Showing(InputPane sender, InputPaneVisibilityEventArgs args) 
    { 
     LayoutRoot.Height = LayoutRoot.ActualHeight - args.OccludedRect.Height; 
     LayoutRoot.VerticalAlignment = VerticalAlignment.Top; 
     args.EnsuredFocusedElementInView = true; 
    } 

private void UserRegistrationAuthorization_PhoneNumber_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) 
    { 
     // TODO: Get rid of that shit 
     LayoutRoot.Height = LayoutRootInitialHeight; 
     args.EnsuredFocusedElementInView = false; 
    } 

當我點擊文本框鍵盤之外隱藏和離開後,在屏幕上的黑洞。 2

enter image description here

不過,最有意思的是,當我按我的Lumia物理後退按鈕,鍵盤通常隱藏和我的LayoutRoot獲取框架的初始高度。

這是一個錯誤還是我做錯了什麼?

回答

2

發生這種情況的原因是,當您在構造函數中保存LayoutRootInitialHeight時,LayoutRoot實際上並未加載,並且它的ActualHeight == 0.然後將LayoutRoot.Height設置爲0,以使其不可見。所以你應該將LayoutRootInitialHeight保存在LayoutRoot的Loaded事件處理程序中。

我也建議你不要改變LayoutRoot的高度。它會導致整個視覺樹從頭開始渲染,這是一般的糟糕做法。相反,修改所有必要元素的RenderTransform,使它們移動到合適的位置。 RenderTransform是處理屏幕上運動和動畫的正確方法,您可以使用Next按鈕向上移動與鍵盤相同的視覺效果。

粗略你的代碼可以是這樣的:

<Button Content="Next" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center"> 
    <Button.RenderTransform> 
     <CompositeTransform x:Name="NextButtonTransform" TranslateY="0"/> 
    </Button.RenderTransform> 
</Button> 

...

private void UserRegistrationAuthorization_PhoneNumber_Showing(InputPane sender, InputPaneVisibilityEventArgs args) 
{ 
    NextButtonTransform.TranslateY = -300; 
    EnsuredFocusedElementInView = true; 
} 

private void UserRegistrationAuthorization_PhoneNumber_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) 
{ 
    NextButtonTransform.TranslateY = 0; 
    args.EnsuredFocusedElementInView = false; 
} 

,更復雜的方法是運行一些故事板,讓您的下一個按鈕,在同樣的速度上下移動隨着鍵盤,總是出現在它的頂部。雖然自從InputPane.GetForCurrentView()。顯示在鍵盤已經完全顯示後被觸發,您應該將所有動畫連接到TextBox.GotFocus和TextBox.LostFocus事件。在手機上,當文本框具有焦點時總是顯示鍵盤,所以它會很好地工作。

+0

感謝您的詳細答案! –