简体   繁体   English

.NET MAUI 自定义 SearchHandler 未应用 ItemTemplate

[英].NET MAUI Custom SearchHandler not applying ItemTemplate

I've been trying to create a custom DataTemplate for ItemSource with the help of .NET MAUI Shell search documentation , but nor the XAML and C# code are not applying the custom template.我一直在尝试借助.NET MAUI Shell 搜索文档为 ItemSource 创建自定义 DataTemplate,但 XAML 和 C# 代码也没有应用自定义模板。 I am running the code on winUI.我在 winUI 上运行代码。

The code I've been trying to accomplish this with:我一直在尝试使用以下代码来完成此操作:

Inside the ContentPage where the SearchHandler is shown:在显示 SearchHandler 的 ContentPage 中:

<Shell.SearchHandler>
    <controls:TagSearchHandler Placeholder="Enter search term"
                               ShowsResults="true">
      <controls:TagSearchHandler.ItemTemplate>
        <DataTemplate>
          <HorizontalStackLayout Padding="10">
            <Label Text="{Binding TagName}"
                   Margin="0,0,10,0" />
            <Label Text="Test" />
          </HorizontalStackLayout>
        </DataTemplate>
      </controls:TagSearchHandler.ItemTemplate>
    </controls:TagSearchHandler>
</Shell.SearchHandler>

TagSearchHandler.cs, inside OnQueryChanged TagSearchHandler.cs,在 OnQueryChanged 里面

protected override void OnQueryChanged(string oldValue, string newValue)
{
    base.OnQueryChanged(oldValue, newValue);

    // Filter data based on the newText
    var filteredData = new List<DataHolderClass> { /*populate*/ };    ItemsSource = filteredData; 
    // Set the ItemSource
    ItemsSource = filteredData;
}

The TagSearchHandler is using DataHolderClass TagSearchHandler 正在使用 DataHolderClass

public class DataHolderClass: ObservableObject
{
    public string TagName { get; set; }
    ...
}

However by only using this, the SearchHandler displays namespace with class name.但是,仅通过使用它,SearchHandler 会显示名称为 class 的名称空间。 SearchHandler results image SearchHandler 结果图像

Am I missing something?我错过了什么吗?

From what I have researched, it would appear that this is an issue on .NET 6 on winUI platform.根据我的研究,这似乎是 .NET 6 on winUI 平台上的一个问题。 What I would suggest is to create a custom SearchControl with a SearchBar and some sort of collection view (or a ListView).我的建议是创建一个带有 SearchBar 和某种集合视图(或 ListView)的自定义 SearchControl。

An example of the custom search control could look something like this自定义搜索控件的示例可能如下所示

<VerticalStackLayout WidthRequest="300">
<SearchBar x:Name="searchBar">
    <SearchBar.Behaviors>
        <xct:UserStoppedTypingBehavior Command="{Binding SearchCommand}"
                                   CommandParameter="{Binding Text, Source={x:Reference searchBar}}"
                                   StoppedTypingTimeThreshold="300"
                                   MinimumLengthThreshold="0"
                                   ShouldDismissKeyboardAutomatically="False" />
    </SearchBar.Behaviors>
</SearchBar>
<ListView MaximumHeightRequest="300"
                      ItemsSource="{Binding SearchItemsSource}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                    <Label Text="{Binding TagName}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Where在哪里

xct:UserStoppedTypingBehavior xct:UserStoppedTypingBehavior

Is from the maui toolkit (https://learn.microsoft.com/en-us/do.net/communitytoolkit/maui/ ) and is used to not trigger search command on every change, but rather after a certain threshold.来自 maui 工具包 (https://learn.microsoft.com/en-us/do.net/communitytoolkit/maui/ ),用于不在每次更改时触发搜索命令,而是在某个阈值之后触发搜索命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM