UWP/C# 如何将文本从文本框滚动到选定的文本?

发布于 2025-01-12 01:52:04 字数 134 浏览 2 评论 0原文

我有一个具有文本转语音功能的 UWP 桌面应用程序。其中,我有一个文本框来包含将由语音合成器执行的文本。在执行期间,应用程序选择当前执行的短语。但是,由于文本比文本框大,我需要滚动文本,以便用户可以看到执行和选择的短语。如何做到这一点?非常欢迎任何帮助。

I have a UWP Desktop application with Text to Speech capabilities. In it, I have a TextBox to contain the text that will be executed by the Speech Synthesizer. During execution, the application selects the currently executed phrase. However, since the text is larger than the TextBox, I need to scroll the text so that the executed and selected phrase is visible to the user. How to do this? Any help is most welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2025-01-19 01:52:05

UWP/C# 如何将文本从文本框滚动到所选文本?

恐怕您无法滚动到 UWP TextBox 中的特定位置。它看起来不包含 ScrollTo 方法。但是,您可以获取 TextBox 的内部 ScrollViewer 然后调用 ChangeView 方法滚动到您想要的位置。

例如。

public void ScrollToSp(TextBox control, string text)
{
    var grid = (Grid)VisualTreeHelper.GetChild(control, 0);
    var position = control.Text.Contains(text);
    if (control.Text.Contains(text))
    {
        var index = control.Text.IndexOf(text);
        var rect =  control.GetRectFromCharacterIndex(index, true);

        for (var i = 0; i <= VisualTreeHelper.GetChildrenCount(grid) - 1; i++)
        {
            object obj = VisualTreeHelper.GetChild(grid, i);
            if (!(obj is ScrollViewer)) continue;
            ((ScrollViewer)obj).ChangeView(0.0f, rect.Top, 1.0f);
            break;
        }
    }         
}

UWP/C# How to scroll text from a TextBox to selected text?

I'm afraid you can't scroll to specific position wihtin UWP TextBox. It looks does not contains ScrollTo method. However, you could get TextBox's internal ScrollViewer then call ChangeView method to scroll to your wanted position.

For example.

public void ScrollToSp(TextBox control, string text)
{
    var grid = (Grid)VisualTreeHelper.GetChild(control, 0);
    var position = control.Text.Contains(text);
    if (control.Text.Contains(text))
    {
        var index = control.Text.IndexOf(text);
        var rect =  control.GetRectFromCharacterIndex(index, true);

        for (var i = 0; i <= VisualTreeHelper.GetChildrenCount(grid) - 1; i++)
        {
            object obj = VisualTreeHelper.GetChild(grid, i);
            if (!(obj is ScrollViewer)) continue;
            ((ScrollViewer)obj).ChangeView(0.0f, rect.Top, 1.0f);
            break;
        }
    }         
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文