当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-10 17:00:46 C#

C#学习教程:WP7中的可滚动文本框(alaSkype和Facebook)WP7中的可滚动文本框)。我将TextBox放在ScrollViewer中,一切都很好;当TextBox获得焦点时,我禁用了ScrollViewer,因此它会在用户键入时自动滚动。我的问题是我希望用户在编辑笔记时能够滚动,就像他在阅读笔记时能够滚动一样。我认为唯一的方法是按住直到出现超大插入符,然后移动它,但我发现,事实上,第三方应用程序支持这种类型的滚动。我想要实现的是移动设备上的Word/OneNote,用户可以在编辑文档时轻松滚动(这里有一个视频显示)。在Skype和Facebook应用程序中可以看到相同的效果,在撰写消息时,您可以滚动它以查看更多信息。我想知道这是不是自定义控件,或者布局是否以特定方式设计,因为ScrollViewer中的TextBox根本不起作用。我感谢任何帮助。提前致谢。根据Ku6opr给出的答案,我调整了代码以适合我的情况。现在TextBox具有正常行为,但可以滚动,并且RootFrame不会自动上升。XAML:C#:publicpartialclassMainPage:PhoneApplicationPage{doubleInputHeight=0.0;//构造函数publicMainPage(){InitializeComponent();}privatevoidMessageText_GotFocus(objectsender,System.Windows.RoutedEventArgse){(App.CurrentasApp).RootFrame.RenderTransform=newCompositeTransform();}privatevoidinputText_TextChanged(objectsender,System.Windows.Controls.TextChangedEventArgse){Dispatcher.BeginInvoke(()=>{doubleCurrentInputHeight=MessageText.ActualHeight;if(CurrentInputHeight>InputHeight){InputScrollViewer.ScrollToVerticalOffset(InputScrollViewer.VerticalOffset+CurrentInputHeight-InputHeight);}InputHeight=CurrentInputHeight;});}publicvoidMessageText_Tap(objectsender,GestureEventArgse){InputScrollViewer.ScrollToVerticalOffset(e.GetPosition(MessageText).Y-80);Tap事件处理程序检测点击的垂直位置并滚动ScrollViewer,以便在TextBox获得焦点时插入符号在视图中。也许它不是最好的解决方案,但它的工作原理:XAML:后台代码:publicpartialclassMainPage:PhoneApplicationPage{boolIsInputFocused=false;双输入高度=0.0;双键盘高度=338;双KeyboardClipboardHeight=72;双根高度=800;publicMainPage(){InitializeComponent();DeviceStatus.KeyboardDeployedChanged+=newEventHandler(DeviceStatus_KeyboardDeployedChanged);}voidDeviceStatus_KeyboardDeployedChanged(objectsender,EventArgse){if(IsInputFocused){UpdateKeyboard();}}privatevoidinputText_TextChanged(objectsender,System.Windows.Controls.TextChangedEventArgse){Dispatcher.BeginInvoke(()=>{doubleCurrentInputHeight=MessageText.ActualHeight;if(CurrentInputHeight>InputHeight){InputScrollViewer.ScrollToVerticalOffset(InputScrollViewer.VerticalOffset+CurrentInputHeight-InputHeight);}InputHeight=CurrentInputHeight;});}privatevoidUpdateKeyboard(){(App.CurrentasApp).RootFrame.RenderTransform=newCompositeTransform();如果(!DeviceStatus.IsKeyboardDeployed){InputScrollViewer.Height=RootHeight-(KeyboardHeight+GetClipboardHeight());}else{InputScrollViewer.Height=RootHeight;}}privatedoubleGetClipboardHeight(){return(ClipboardContainsText())?键盘剪贴板高度:0;}privatevoidMessageText_GotFocus(objectsender,System.Windows.RoutedEventArgse){IsInputFocused=true;(App.Current作为App).RootFrame.RenderTransform=newCompositeTransform();更新键盘();Dispatcher.BeginInvoke(()=>{InputScrollViewer.ScrollToVerticalOffset(InputScrollViewer.VerticalOffset+338+GetClipboardHeight());});}privatevoidMessageText_LostFocus(objectsender,System.Windows.RoutedEventArgse){IsInput.HepViewFocused=false=RootHeight;首先,当TextBox获得焦点时,此代码禁用框架转换(向上移动)。此外,当键盘在屏幕上时,此代码执行新的自由屏幕高度的管理。以上是C#学习教程:WP7(alaSkypeandFacebook)中可滚动TextBox分享的所有内容。代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: