Csharp/C#教程:WP7中的可滚动TextBox(ala Skype和Facebook)分享


WP7中的可滚动TextBox(ala Skype和Facebook)

基本上,我正在开发一个笔记记录应用程序,用户可以根据需要打字(有点像WP7中的Scrollable TextBox )。 我把TextBox放在ScrollViewer中,一切都很好; 我在TextBox获得焦点时禁用ScrollViewer,因此在用户输入时它会自动滚动。

我的问题是,我希望用户能够在编辑音符时滚动,就像他能够在阅读音符时滚动一样。 我认为唯一的方法就是按住直到超大插入符号出现,然后移动它,但我发现,实际上,第三方应用程序支持这种类型的滚动。

我想要实现的是手机上的Word / OneNote,用户可以在编辑文档时轻松滚动( 这是一个展示它的video )。 在Skype和Facebook应用程序中也可以看到相同的效果,在编写消息时,您可以滚动它以查看更多信息。

我想知道这是一个自定义控件,还是布局是以特定方式设计的,因为ScrollViewer中的TextBox根本不起作用。

我很感激任何帮助。 提前致谢。

根据Ku6opr给出的答案,我调整了代码,以便在我的情况下工作。 现在,TextBox具有常规行为,但是可滚动,并且RootFrame不会自动上升。

XAML:

     

C#:

 public partial class MainPage : PhoneApplicationPage { double InputHeight = 0.0; // Constructor public MainPage() { InitializeComponent(); } private void MessageText_GotFocus(object sender, System.Windows.RoutedEventArgs e) { (App.Current as App).RootFrame.RenderTransform = new CompositeTransform(); } private void inputText_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { Dispatcher.BeginInvoke(() => { double CurrentInputHeight = MessageText.ActualHeight; if (CurrentInputHeight > InputHeight) { InputScrollViewer.ScrollToVerticalOffset(InputScrollViewer.VerticalOffset + CurrentInputHeight - InputHeight); } InputHeight = CurrentInputHeight; }); } public void MessageText_Tap(object sender, GestureEventArgs e) { InputScrollViewer.ScrollToVerticalOffset(e.GetPosition(MessageText).Y - 80); } } 

Tap事件处理程序检测点击的垂直位置,并滚动ScrollViewer,以便当TextBox获得焦点时插入符号处于视图中。

也许它不是最好的解决方案,但它的工作原理:

XAML:

           

后台代码:

 public partial class MainPage : PhoneApplicationPage { bool IsInputFocused = false; double InputHeight = 0.0; double KeyboardHeight = 338; double KeyboardClipboardHeight = 72; double RootHeight = 800; public MainPage() { InitializeComponent(); DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged); } void DeviceStatus_KeyboardDeployedChanged(object sender, EventArgs e) { if (IsInputFocused) { UpdateKeyboard(); } } private void inputText_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { Dispatcher.BeginInvoke(() => { double CurrentInputHeight = MessageText.ActualHeight; if (CurrentInputHeight > InputHeight) { InputScrollViewer.ScrollToVerticalOffset(InputScrollViewer.VerticalOffset + CurrentInputHeight - InputHeight); } InputHeight = CurrentInputHeight; }); } private void UpdateKeyboard() { (App.Current as App).RootFrame.RenderTransform = new CompositeTransform(); if (!DeviceStatus.IsKeyboardDeployed) { InputScrollViewer.Height = RootHeight - (KeyboardHeight + GetClipboardHeight()); } else { InputScrollViewer.Height = RootHeight; } } private double GetClipboardHeight() { return (Clipboard.ContainsText()) ? KeyboardClipboardHeight : 0; } private void MessageText_GotFocus(object sender, System.Windows.RoutedEventArgs e) { IsInputFocused = true; (App.Current as App).RootFrame.RenderTransform = new CompositeTransform(); UpdateKeyboard(); Dispatcher.BeginInvoke(() => { InputScrollViewer.ScrollToVerticalOffset(InputScrollViewer.VerticalOffset + 338 + GetClipboardHeight()); }); } private void MessageText_LostFocus(object sender, System.Windows.RoutedEventArgs e) { IsInputFocused = false; InputScrollViewer.Height = RootHeight; } } 

首先,当TextBox聚焦时,此代码禁用Frame转换(向上移动)。 此外,当键盘在屏幕上时,此代码执行新的自由屏幕高度的管理。

上述就是C#学习教程:WP7中的可滚动TextBox(ala Skype和Facebook)分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1297895.html

(0)
上一篇 2022年12月11日
下一篇 2022年12月11日

精彩推荐