1
我正在開發Windows Phone應用程序,我遇到了這個問題: 我有一個列表控件,它顯示我的搜索結果,但是當鍵盤打開我的一些結果是不可見的,因爲我的鍵盤...無法滾動到結果結束時keyborad打開(Windows Phone)
有沒有辦法縮小控制,直到鍵盤邊界?爲了看到所有的結果。
即使打開鍵盤,我也想滾動到結果的結尾。
我正在開發Windows Phone應用程序,我遇到了這個問題: 我有一個列表控件,它顯示我的搜索結果,但是當鍵盤打開我的一些結果是不可見的,因爲我的鍵盤...無法滾動到結果結束時keyborad打開(Windows Phone)
有沒有辦法縮小控制,直到鍵盤邊界?爲了看到所有的結果。
即使打開鍵盤,我也想滾動到結果的結尾。
有我的解決方案
public class ResizeContentOnKeyboardShowingBehavior : Behavior<Page>
{
private readonly double _screenHeight;
public ResizeContentOnKeyboardShowingBehavior()
{
_screenHeight = Window.Current.Bounds.Height;
}
protected override void OnAttached()
{
InputPane.GetForCurrentView().Showing += OnKeyboardShowing;
InputPane.GetForCurrentView().Hiding += OnKeyboardHiding;
}
protected override void OnDetaching()
{
InputPane.GetForCurrentView().Showing -= OnKeyboardShowing;
InputPane.GetForCurrentView().Hiding -= OnKeyboardHiding;
}
private void OnKeyboardHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)AssociatedObject.Content;
content.Height = _screenHeight;
}
private void OnKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)AssociatedObject.Content;
double keyboardHeight = sender.OccludedRect.Height;
content.Height = _screenHeight - keyboardHeight;
}
}
基本行爲實現:
public abstract class Behavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; set; }
public virtual void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
}
public virtual void Detach()
{
}
}
public abstract class Behavior<T> : Behavior
where T : DependencyObject
{
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public new T AssociatedObject { get; set; }
public override void Attach(DependencyObject associatedObject)
{
base.Attach(associatedObject);
this.AssociatedObject = (T)associatedObject;
OnAttached();
}
public override void Detach()
{
base.Detach();
OnDetaching();
}
protected virtual void OnAttached()
{
}
protected virtual void OnDetaching()
{
}
}
IBehavior接口是從Microsoft.Xaml.Interactivity命名空間從行爲SDK http://scr.hu/4m4q/pzl07
用法:
<Page x:Class="MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:behaviors="using:Behaviors">
<interactivity:Interaction.Behaviors>
<behaviors:ResizeContentOnKeyboardShowingBehavior />
</interactivity:Interaction.Behaviors>
<Grid>
</Grid>
或者相同的功能,但沒有任何行爲。剛添加到頁面代碼後面。
public sealed partial class MainPage : Page
{
private readonly InputPane _inputPane;
private readonly double _screenHeight;
public MainPage()
{
this.InitializeComponent();
_screenHeight = Window.Current.Bounds.Height;
_inputPane = InputPane.GetForCurrentView();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_inputPane.Hiding += OnKeyboardHiding;
_inputPane.Showing += OnKeyboardShowing;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
_inputPane.Hiding -= OnKeyboardHiding;
_inputPane.Showing -= OnKeyboardShowing;
}
private void OnKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)Window.Current.Content;
double keyboardHeight = sender.OccludedRect.Height;
content.Height = _screenHeight - keyboardHeight;
}
private void OnKeyboardHiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
var content = (FrameworkElement)Window.Current.Content;
content.Height = _screenHeight;
}
}
您的代碼可以作爲一個非常有用的紅外線使用! :) –
這是一個非常好的解決方案。我已經實現了它,並且內容被調整大小,所以鍵盤不會覆蓋應用程序的窗口 - 但內容不會滾動到視圖中。我可以點擊並拖動內容到視圖中。任何想法爲什麼? – nitech
找到原因:我的內容上有一個邊緣,對應於鍵盤的高度(或者高度的一半)。當我添加'content.VerticalAlignment = VerticalAlignment.Top'時,它解決了這個問題。所以它是關於內容的地方,一旦它被調整大小。也許最好調整父容器的大小而不是內容。 – nitech