如何在Windows Phone中獲取表單的行爲,例如聯繫人>>新聯繫人>>姓名。在這個頁面中,它在scrollviewer中有很多文本框。當用戶點擊任何文本框並獲得焦點時,頁面向上滾動並且標題保持不變並顯示SIP鍵盤。 這是我的榜樣,但不是它的工作原理鍵盤處於活動狀態時ScrollViewer無法向上滾動
https://app.box.com/s/lxxcmxp8ckuottrweg52
爲什麼? 謝謝
如何在Windows Phone中獲取表單的行爲,例如聯繫人>>新聯繫人>>姓名。在這個頁面中,它在scrollviewer中有很多文本框。當用戶點擊任何文本框並獲得焦點時,頁面向上滾動並且標題保持不變並顯示SIP鍵盤。 這是我的榜樣,但不是它的工作原理鍵盤處於活動狀態時ScrollViewer無法向上滾動
https://app.box.com/s/lxxcmxp8ckuottrweg52
爲什麼? 謝謝
我已經修改,對於如下工作正常上面的代碼。
public double OldHeight;
private TranslateTransform _translateTransform;
#region TranslateY dependency property
public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register(
"TranslateYProperty", typeof(double), typeof(Chat), new PropertyMetadata(default(double), PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var chat = o as Chat;
#if DEBUG
Debug.WriteLine("New value:" + e.NewValue);
Debug.WriteLine("Old value:" + e.OldValue);
#endif
if (chat != null)
{
chat.UpdateTopMargin((double)e.NewValue);
}
}
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
#endregion
private void ChatPage_OnLoaded(object sender, RoutedEventArgs e)
{
var transform = ((Application.Current).RootVisual).RenderTransform as TransformGroup;
if (transform != null)
{
_translateTransform = transform.Children.OfType<TranslateTransform>().FirstOrDefault();
if (_translateTransform != null)
{
var binding = new Binding("Y")
{
Source = _translateTransform
};
BindingOperations.SetBinding(this, TranslateYProperty, binding);
}
}
}
private void UpdateTopMargin(double translateY)
{
LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
}
感謝
似乎仍然沒有工作。 –
檢查上面修改過的代碼對我很好 –
它會進入課堂嗎?你可以在Pastebin上發佈整個課程+ XAML嗎?這將有很大的幫助。在互聯網上幾乎沒有解決方案 –
首先,命名您的滾動瀏覽器ScrollViewer
。之後添加GotFocus
和LostFocus
的事件處理程序頁面上的每個文本框控件,並寫下面的代碼裏面:
private void txt_LostFocus(object sender, RoutedEventArgs routedEventArgs)
{
ScrollViewer.Height = _oldHeight;
}
void txt_GotFocus(object sender, RoutedEventArgs e)
{
var transform = ((Application.Current).RootVisual).RenderTransform as TransformGroup;
if (transform != null)
{
_translateTransform = transform.Children.OfType<TranslateTransform>().FirstOrDefault();
if (_translateTransform != null)
{
var binding = new Binding("Y")
{
Source = _translateTransform
};
BindingOperations.SetBinding(this, TranslateYProperty, binding);
}
}
var clipboardVisible = false;
try
{
clipboardVisible = Clipboard.ContainsText();
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
ScrollViewer.Height = _oldHeight - (clipboardVisible ? 407 : 338);
}
您需要以下依賴屬性添加到頁面:
#region TranslateY dependency property
public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register(
"TranslateYProperty", typeof(double), typeof(OrderContactPage), new PropertyMetadata(default(double), PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((OrderContactPage)o)._translateTransform.Y = 0;
}
public double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
set { SetValue(TranslateYProperty, value); }
}
#endregion
而且還幫助字段:
private double _oldHeight;
private TranslateTransform _translateTransform;
您還需要處理一些事件爲您滾動瀏覽器,將其添加到頁面的結構或者:
ScrollViewer.Loaded += ScrollViewerOnLoaded;
ScrollViewer.SizeChanged += ScrollViewer_OnSizeChanged;
執行這些事件處理程序: 私人無效ScrollViewerOnLoaded(對象發件人,RoutedEventArgs routedEventArgs) { ScrollViewer.Loaded - = ScrollViewerOnLoaded; _oldHeight = ScrollViewer.ActualHeight; }
private async void ScrollViewer_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
await ScrollToFocusedElement();
}
private async Task ScrollToFocusedElement()
{
await Task.Yield();
var focusedElement = FocusManager.GetFocusedElement() as PhoneTextBox;
if (focusedElement != null)
{
// http://stackoverflow.com/questions/1225318/how-can-i-make-the-silverlight-scrollviewer-scroll-to-show-a-child-control-with
var focusedVisualTransform = focusedElement.TransformToVisual(ScrollViewer);
var rectangle =
focusedVisualTransform.TransformBounds(
new Rect(new Point(focusedElement.Margin.Left, focusedElement.Margin.Top), focusedElement.RenderSize));
var offset = ScrollViewer.VerticalOffset + (rectangle.Bottom - ScrollViewer.ViewportHeight);
ScrollViewer.ScrollToVerticalOffset(offset);
}
}
哇,這裏有很多代碼。我正在創建可重用的東西,但我還沒有。一旦我做到了,我會在NuGet上發佈它。
請注意,這僅適用於Portrait
模式,如果你有自動建議欄中打開,將無法正常工作。我並不需要處理,在我的應用程序,所以我跳過它:)
享受。
你好,謝謝你的回覆,但在我的網頁不是它的作品正確。 ScrollViewer的高度和焦點都有問題。我已經用你的代碼更新了我的項目,你可以給我們看看。謝謝 –
你可以試試這個樣本sample project
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0"
Background="#002080">
<TextBlock Text="PAGE HEADER" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<Grid Grid.Row="1" />
<TextBox Grid.Row="2"
Text=""
x:Name="messageBox"
Background="White" LostFocus="MessageBox_OnLostFocus" />
</Grid>
public MainPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
}
私有靜態雙_newValue;
private static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register("TranslateY", typeof(double), typeof(MainPage), new PropertyMetadata(0d, OnRenderXPropertyChanged));
private double TranslateY
{
get { return (double)GetValue(TranslateYProperty); }
}
private static void OnRenderXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((double)e.NewValue <= _newValue)
((MainPage)d).UpdateTopMargin((double)e.NewValue);
_newValue = (double)e.NewValue;
}
private void BindToKeyboardFocus()
{
var frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (frame == null) return;
var group = frame.RenderTransform as TransformGroup;
if (@group == null) return;
var translate = @group.Children[0] as TranslateTransform;
var translateYBinding = new Binding("Y") { Source = translate };
SetBinding(TranslateYProperty, translateYBinding);
}
private void UpdateTopMargin(double translateY)
{
double prevTopMargin = LayoutRoot.Margin.Top;
LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
}
我知道你的意思,我可以在稍後提供源代碼。您需要檢測到您正在編輯文本,然後您需要調整大小並移動滾動查看器。實際上很乏味。 –
你有沒有例子? –
現在不行,當我回家。 –