ScrollViewer 不滚动
我大约一周前开始 WP7 开发(以及一般编程),并且一直在开发一个小应用程序,但我遇到了让 ScrollViewer 正常运行的问题。
当满足某些条件时,应用程序会创建一个新的枢轴项目,我试图在其中添加一个可滚动的文本块,每次用户点击屏幕时,该文本块都会显示从列表中随机选择的文本字符串,其中一些文本长度足够长以要求垂直滚动。
// A bit cleaned version of my code,
// had to translate stuff a bit for them to make sense
// Sets the PivotItem header depending on user choice
// and creates ScrollViewer and TextBlock
PivotItem newPivotItem = new PivotItem { Header = choice, Name = "newPivot"};
ScrollViewer newScrollviewer = new ScrollViewer();
TextBlock newTextBlock = new TextBlock { Text = "tap the screen", Name = choice};
newScrollviewer.Content = newTextBlock;
newPivotItem.Content = newScrollviewer;
mainPivot.Items.Add(newPivotItem);
在 Tap 事件中添加文本,只是用新字符串替换 Text 属性。文本更新正常且符合预期,但 ScrollViever 在更新后停止工作。
newString = list[rand];
PivotItem selectedPivot = mainPivot.SelectedItem as PivotItem;
TextBlock selectedText = selectedPivot.FindName(choice) as TextBlock;
selectedText.Text = newString;
selectedText.Height = selectedText.ActualHeight;
在 xaml 中声明的另一个 PivotItem 中的类似 ScrollViewer - TextBlock 组合效果很好。
i've started WP7 development about a week ago (and programming in general) and I've been working on a little app, but i faced a problem with getting ScrollViewer to function properly.
Application creates a new pivot item when certain conditions are met, and i'm trying to add a scrollable textblock in it, which shows randomly chosen strings of text from a list every time user taps the screen, from which some are long enough to require vertical scrolling.
// A bit cleaned version of my code,
// had to translate stuff a bit for them to make sense
// Sets the PivotItem header depending on user choice
// and creates ScrollViewer and TextBlock
PivotItem newPivotItem = new PivotItem { Header = choice, Name = "newPivot"};
ScrollViewer newScrollviewer = new ScrollViewer();
TextBlock newTextBlock = new TextBlock { Text = "tap the screen", Name = choice};
newScrollviewer.Content = newTextBlock;
newPivotItem.Content = newScrollviewer;
mainPivot.Items.Add(newPivotItem);
Text is added in Tap event, which just replaces the Text property with new string. Text updates just fine and as intended, but ScrollViever stops working after update.
newString = list[rand];
PivotItem selectedPivot = mainPivot.SelectedItem as PivotItem;
TextBlock selectedText = selectedPivot.FindName(choice) as TextBlock;
selectedText.Text = newString;
selectedText.Height = selectedText.ActualHeight;
Similiar ScrollViewer - TextBlock combination in another PivotItem which is declared in xaml works just fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现真正破坏 ScrollViewer 的是我设置为在文本更新时触发的过渡动画。我在上面遇到了一点打字错误,不知何故,虽然转换有效,但滚动查看器却没有。
I found what actually broke the ScrollViewer, it was transition animation which i had set to trigger on text update. I had a little typing error on it and somehow while the transition worked, scrollviewer didn't.