如何在UWP中实现查找页面 [英] How to implement find in page in UWP

查看:76
本文介绍了如何在UWP中实现查找页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在我的页面中有不同的UI元素,我必须实现搜索功能,我必须在该页面中突出显示所有搜索文本。我开始了解find-in-page但不知道如何实现它。

My application has different UI elements in my page, i have to implement search functionality in which I have to highlight all occurrence of searched text in that page. I came to know about find-in-page but not sure how to implement it.

推荐答案

你好,

首先考虑到的是文字 在XAML框架中呈现。如果我们可以找到文本渲染器容器,那么我们可以突出显示它们。在XAML中,这些是TextBlock,RichTextBlock(Inline like "< Run>,< Hyperlink>,..
"刚刚附加到文本 )或TextBox,  RichEditBox。其他控件使用其中一个文本可视化工具在其演示者中显示文本。这意味着我们应该通过VisualTree找到所有文本可视化工具,然后如果它的文本
匹配搜索关键字 突出他们。以下是示例代码, 但出于复杂性原因 我只为TextBlock编写它。

The first which is cumming to mind is how text  is rendered in XAML framework. If we can find text renderer containers then maybe we can highlight them. In XAML these is TextBlock, RichTextBlock (Inlines like  "<Run> , <Hyperlink>,.. " just attached to the text ) or by TextBox, RichEditBox. Other Controls use one of these text visualizers to display text inside its presenters. This mean than we should go through VisualTree and find all text visualizers, then if its text match to search key  highlight them. Below is sample code,  But for complexity reason  I write it just for TextBlock.

 private Dictionary<TextBlock, object> searchMatchedControls = new Dictionary<TextBlock, object>();

        private void ResetSearch()
        {
            foreach (var item in searchMatchedControls)
            {
                if(item.Value is BindingExpression)
                {
                    BindingExpression epr = (BindingExpression)item.Value;
                    item.Key.SetBinding(TextBlock.ForegroundProperty, epr.ParentBinding);
                }
                else
                {
                    item.Key.SetValue(TextBlock.ForegroundProperty, item.Value);
                }
            }

            searchMatchedControls.Clear();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ResetSearch();

            foreach (TextBlock tb in FindVisualChildren<TextBlock>(this)) // this = Root Control where you want to search for example MainPage
            {
                
                if (!string.IsNullOrEmpty(tb.Text) && !string.IsNullOrEmpty(SearchBox1.Text) && tb.Text.Contains(SearchBox1.Text))
                {
                    object value = null;
                    value = tb.GetBindingExpression(TextBlock.ForegroundProperty);
                    if(value == null)
                    {
                        value = tb.GetValue(TextBlock.ForegroundProperty);
                    }
                    searchMatchedControls.Add(tb, value);
                    tb.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }


这篇关于如何在UWP中实现查找页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆