C# 如何在Xamarin.Forms中的其他页面(标签)上执行文本搜索

C# 如何在Xamarin.Forms中的其他页面(标签)上执行文本搜索,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我是Xamarin公司的新员工。我希望你能以一种简单的方式解决我的问题 我用xamarin开发了一个跨平台应用程序。在应用程序的一个页面中,我希望能够搜索一个单词,并将用户重定向到他想要的页面。 我将向你描述情况 我有一个带有列表视图的页面(listview.xaml)。列表视图的每一项都像按钮一样用于进入其他页面(page1.xaml;page2.xaml;page3.xaml;等等)。在本页中有一些文本(在标签中)。因此,我希望如果我在listpage.xaml中搜索单词“example”,

我是Xamarin公司的新员工。我希望你能以一种简单的方式解决我的问题

我用xamarin开发了一个跨平台应用程序。在应用程序的一个页面中,我希望能够搜索一个单词,并将用户重定向到他想要的页面。 我将向你描述情况

我有一个带有列表视图的页面(listview.xaml)。列表视图的每一项都像按钮一样用于进入其他页面(page1.xaml;page2.xaml;page3.xaml;等等)。在本页中有一些文本(在标签中)。因此,我希望如果我在listpage.xaml中搜索单词“example”,我会收到所有存在单词“example”的页面作为结果,并使用结果导航到单词“example”所在的正确页面

如果可以的话,请给我一个简单的解决方案。谢谢

这是我的listview.xaml


我会尽力帮助你:)


命名空间跨平台应用程序
{  
公共部分类主页:ContentPage
{  
公共列表数据;
公共主页()
{  
初始化组件();
数据();
list.ItemsSource=tempdata;
}  
公共void数据()
{  
//所有的临时数据
tempdata=新列表{
新联系人(){Name=“umair”,Num=“2323423”},
新联系人(){Name=“saleh”,Num=“23423”},
新联系人(){Name=“umair”,Num=“233423423”},
新联系人(){Name=“sanat”,Num=“2423”},
新联系人(){Name=“jawad”,Num=“323423”},
新联系人(){Name=“shan”,Num=“2323423”},
新联系人(){Name=“ahmed”,Num=“2323423”},
新联系人(){Name=“abc”,Num=“2323423”},
新联系人(){Name=“umair”,Num=“2323423”},
新联系人(){Name=“etc”,Num=“2323423”},
};  
}  
私有无效搜索栏\u TextChanged(对象发送者,textchangedventargs e)
{  
//这就是你需要搜索的全部内容
if(string.IsNullOrEmpty(e.NewTextValue))
{  
list.ItemsSource=tempdata;
}  
其他的
{  
list.ItemsSource=tempdata.Where(x=>
x、 Name.StartsWith(e.NewTextValue));
}  
}  
}  
免责声明:

这只是实现原始帖子(OP)中设置的要求的一种方式,并不意味着它所涉及的代码是最好的,或者是与产品类似的代码。它只是实现OP要求的一种方式


让我们创建一个页面ListView.xaml),其中包含一个列表视图,该列表视图列出了一本书章节的两个项目。单击每个项目后,将启动一个包含该章节内容的页面

此外,在该列表视图的顶部有一个
搜索栏
:当您进行搜索时,应用程序将检查项目引用的任何页面是否包含搜索文本

ListView.xaml:

现在您只需要包含以下文本的页面:Page1Page2

首先,我定义了这些页面将实现的接口
接口1
。该接口的使用是定义一个属性:
PageText
。通过在Page1Page2中实现该接口,我能够将列表视图中的每个项目强制转换为
i事件处理程序中的interface1
SearchBar\u搜索按钮在列表视图.xaml.cs中按下

接口1.cs

interface Interface1
{
    string PageText { get; set; }
}
public partial class Page1 : ContentPage,Interface1
{

    public String PageText { get; set; }

    public Page1()
    {

        PageText =
           "This is the display of the inquiry of Herodotus of Halicarnassus, " +
           "so that things done by man not be forgotten in time, and that great " +
           "and marvelous deeds, some displayed by the Hellenes, some by the " +
           "barbarians, not lose their glory, including among others what was the " +
           "cause of their waging war on each other. ";

        InitializeComponent();

        BindingContext = this;
    }

}
现在是页面:

Page1.xaml

同样地

Page2.xaml


但是如果你必须自己阅读标签怎么办?好吧,这不是一个好的做法(因为这意味着你要引用这些对象并占用资源),我无法想象这种情况会发生在什么情况下

但是如果我必须这样做,我认为一种方法是在Page2中将标签定义为
Public
Public static
。在第一种情况下,您必须保留对
Page2 myPage2=new Page2()
的引用才能读取其Public标签(
myPage2.myPublicLabel
)。在第二种情况下,只有标签仍然被引用,然后可以作为
Page2.mystaticLabel
检索。您自然首先必须检查标签是否已分配,等等


到目前为止,OP中描述的问题应该得到解决,但可能值得尝试一种不同的方法:

您可能不想创建Page1Page2等,而是想扩展您的对象(为列表视图提供信息并在上面的示例中调用
内容的对象)包含包含要在其他页面中显示的文本的
字符串
属性
ChapterText
。如果这样做,您可以创建一个通用页面,其构造函数将这些对象之一作为参数,并使用
ChapterText
作为页面的文本
namespace crossplatformapp  
{  
public partial class MainPage : ContentPage  
{  
    public List<Contacts> tempdata;  
    public MainPage()  
    {  
        InitializeComponent();  
        data();  
        list.ItemsSource = tempdata;  

    }  

    public void data()  
    {  
        // all the temp data  
        tempdata = new List<Contacts> {  
            new Contacts(){ Name = "umair", Num = "2323423"},  
            new Contacts(){ Name = "saleh", Num = "23423"},  
            new Contacts(){ Name = "umair", Num = "233423423"},  
            new Contacts(){ Name = "sanat", Num = "2423"},  
            new Contacts(){ Name = "jawad", Num = "323423"},  
            new Contacts(){ Name = "shan", Num = "2323423"},  
            new Contacts(){ Name = "ahmed", Num = "2323423"},  
            new Contacts(){ Name = "abc", Num = "2323423"},  
            new Contacts(){ Name = "umair", Num = "2323423"},  
            new Contacts(){ Name = "etc", Num = "2323423"},  
        };  
    }  

    private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)  
    {  
        //thats all you need to make a search  

        if (string.IsNullOrEmpty(e.NewTextValue))  
        {  
            list.ItemsSource = tempdata;  
        }  

        else  
        {  
            list.ItemsSource = tempdata.Where(x => 
 x.Name.StartsWith(e.NewTextValue));  
        }  
    }  
}  
<StackLayout>

    <SearchBar SearchButtonPressed="SearchBar_SearchButtonPressed" TextChanged="SearchBar_TextChanged"/>

    <ListView x:Name="listView"
              ItemTapped="listView_ItemTapped">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding Chapter}"
                          Detail="{Binding Description}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</StackLayout>
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;

namespace SearchContent
{
    public partial class ListView : ContentPage
    {

        ObservableCollection<Content> contents { get; set; }
        public ListView()
        {
            InitializeComponent();
        }

        protected override void OnAppearing()
        {

            base.OnAppearing();

            contents = new ObservableCollection<Content>()
            {
                new Content("Chapter 1", "Description of Chapter 1", new Page1()),
                new Content("Chapter 2", "Description of Chapter 2", new Page2()),
            };

            listView.ItemsSource = contents;

        }


        private void listView_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            Navigation.PushAsync(((Content)e.Item).ChapterPage);
        }

        private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
        {
            String textToSearch = ((SearchBar)sender).Text;

            var contentsFiltered = contents.Where(item => ((Interface1)item.ChapterPage).PageText.Contains(textToSearch));

            ObservableCollection<Content> newContents = new ObservableCollection<Content>(contentsFiltered);

            listView.ItemsSource = null;
            listView.ItemsSource = newContents;

        }

        private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(String.IsNullOrEmpty(e.NewTextValue))
            {
                listView.ItemsSource = null;
                listView.ItemsSource = contents;
            }
        }
    }


    public class Content
    {
        public String Chapter { get; set; }

        public String Description { get; set; }

        public Page ChapterPage { get; set; }

        public Content(String chapter, String description, Page chapterPage)
        {

            Chapter = chapter; 
            Description = description;
            ChapterPage = chapterPage;

        }

    }

}
interface Interface1
{
    string PageText { get; set; }
}
<StackLayout>
    <Label Text="{Binding PageText}"
           VerticalOptions="CenterAndExpand" 
           HorizontalOptions="CenterAndExpand" />
</StackLayout>
public partial class Page1 : ContentPage,Interface1
{

    public String PageText { get; set; }

    public Page1()
    {

        PageText =
           "This is the display of the inquiry of Herodotus of Halicarnassus, " +
           "so that things done by man not be forgotten in time, and that great " +
           "and marvelous deeds, some displayed by the Hellenes, some by the " +
           "barbarians, not lose their glory, including among others what was the " +
           "cause of their waging war on each other. ";

        InitializeComponent();

        BindingContext = this;
    }

}
<StackLayout>
    <Label Text="{Binding PageText}"
           VerticalOptions="CenterAndExpand" 
           HorizontalOptions="CenterAndExpand" />
</StackLayout>
public partial class Page2 : ContentPage, Interface1
{
    public String PageText { get; set; }

    public Page2()
    {
        PageText =
        "The Persian learned men say that the Phoenicians were the cause " +
        "of the dispute. These (they say) came to our seas from the sea " +
        "which is called Red,1 and having settled in the country which " +
        "they still occupy, at once began to make long voyages. Among other " +
        "places to which they carried Egyptian and Assyrian merchandise, " +
        "they came to Argos";

        InitializeComponent();

        BindingContext = this;
    }
}