Xamarin.forms 选项卡式页面标题视图中的搜索栏-xamarin表单

Xamarin.forms 选项卡式页面标题视图中的搜索栏-xamarin表单,xamarin.forms,Xamarin.forms,我有一个xamarin.forms应用程序,它使用一个选项卡式页面。选项卡式页面的子页面是两个内容页面。两个内容页面中的每一个都有一些列表。我想在选项卡式页面标题视图中添加搜索栏。我可以将其添加为选项卡式页面的标题视图。但是,如何使用这个搜索栏在子页面中搜索两个不同的列表呢 我的选项卡式页面 <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:Views="clr-namesp

我有一个xamarin.forms应用程序,它使用一个选项卡式页面。选项卡式页面的子页面是两个内容页面。两个内容页面中的每一个都有一些列表。我想在选项卡式页面标题视图中添加搜索栏。我可以将其添加为选项卡式页面的标题视图。但是,如何使用这个搜索栏在子页面中搜索两个不同的列表呢

我的选项卡式页面

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"      
             xmlns:Views="clr-namespace:Sample.Views"
             BarBackgroundColor="{DynamicResource NavigationBarColor}"
             mc:Ignorable="d"
             SelectedTabColor="White"          
             UnselectedTabColor="LightGray"
             NavigationPage.HasNavigationBar="True"                  
             x:Class="Sample.Views.TimeSheetsTab">
    <NavigationPage.TitleView>
        <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal" >
            <Label FontSize="Medium" Text="Timesheets" HorizontalTextAlignment="Start"  VerticalTextAlignment="Center" TextColor="{DynamicResource SecondaryTextColor}"></Label>
            <SearchBar  HorizontalOptions="EndAndExpand"  Margin="0,2,10,2">

            </Image>
        </StackLayout>
    </NavigationPage.TitleView>
    <TabbedPage.Children>
        <Views:PendingTimesheets Title="Pending" IconImageSource="icon_pending.png"/>
        <Views:ApprovedTimesheets Title="Approved" IconImageSource="icon_approved.png"/>
    </TabbedPage.Children>
</TabbedPage>

您可以使用来实现它。在每个选项卡页面中,当
SearchBar
textchanged时,向
ViewModel
发送消息以更改模型数据

关于选项卡页面:

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:views="clr-namespace:TabbedPageDemo.Views" x:Class="TabbedPageDemo.Views.MainPage">
    <TabbedPage.Children>
        <NavigationPage Title="Browse">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_feed.png" />
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:ItemsPage />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage Title="About">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_about.png" />
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:AboutPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>
一页的Xaml:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TabbedPageDemo.Views.ItemsPage" Title="{Binding Title}" x:Name="BrowseItemsPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Clicked="AddItem_Clicked" />
    </ContentPage.ToolbarItems>
    <NavigationPage.TitleView>
                <SearchBar x:Name="sbSearch" WidthRequest="500" HeightRequest="100" />
    </NavigationPage.TitleView>
    <StackLayout>
        <ListView x:Name="ItemsListView" ItemsSource="{Binding FilteredItems}" VerticalOptions="FillAndExpand" HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected">
            <d:ListView.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>First Item</x:String>
                    <x:String>Second Item</x:String>
                    <x:String>Third Item</x:String>
                    <x:String>Fourth Item</x:String>
                    <x:String>Fifth Item</x:String>
                    <x:String>Sixth Item</x:String>
                </x:Array>
            </d:ListView.ItemsSource>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="10">
                            <Label Text="{Binding Text}" d:Text="{Binding .}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="16" />
                            <Label Text="{Binding Description}" d:Text="Item description" LineBreakMode="NoWrap" Style="{DynamicResource ListItemDetailTextStyle}" FontSize="13" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

第一项
第二项
第三项
第四项
第五项
第六项
更改搜索栏文本时,发送消息:

    private void SbSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        MessagingCenter.Send<ItemsPage, TextChangedEventArgs>(this, "FilterItems", e);

    }
private void SbSearch\u TextChanged(对象发送者,textchangedventargs e)
{
发送(这是“FilterItems”,e);
}

以下是供参考的选项。

在Xamarin表单中实现它似乎并不容易,也许您可以尝试在本机Android中使用自定义TabPage。如果您有其他好的解决方案,请在这里分享。@JuniorJiang MSFT thanksHi,我已经在Xamarin表单中找到了解决方案,并更新了答案,您可以在有时间时查看它。:)@好的,如果答案有帮助,请让我知道:)圣诞快乐!
    private void SbSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        MessagingCenter.Send<ItemsPage, TextChangedEventArgs>(this, "FilterItems", e);

    }