我在选项卡式毛伊应用程序( Shell.SearchHandler 6)中使用了一个.NET。我将我的应用程序启动到一个特定的页面,在该页面中,您可以通过API搜索一组“外部联系人”。我有一个额外的页面来搜索一组系统用户,再次,通过一个API。当应用程序初始化时。如果我导航到用户页面,它不会更新搜索处理程序,而且搜索功能仍然使用初始页面的模板。
是否可以修改每个页面上的模板?
ContactsPage.xaml
<Shell.SearchHandler>
<controls:ExternalContactSearchHandler Placeholder="Enter last name"
ShowsResults="true"
ItemTemplate="{StaticResource ExternalContactSearchTemplate}"
ExternalContacts="{x:Static data:ExternalContactData.ExternalContacts}"
SelectedItemNavigationTarget="{x:Type views:ContactDetailPage}" />
</Shell.SearchHandler>
Users.xaml
<Shell.SearchHandler>
<controls:UserSearchHandler Placeholder="Enter User Name"
ShowsResults="true"
ItemTemplate="{StaticResource UserSearchTemplate}"
Users="{x:Static data:UserData.Users}"
SelectedItemNavigationTarget="{x:Type views:UserDetailPage}" />
</Shell.SearchHandler>
App.xaml
<DataTemplate x:Key="UserSearchTemplate">
<Grid Padding="10"
ColumnDefinitions="0.15*,0.85*">
<Image Source="{Binding Images[1].ImageUri}"
HeightRequest="40"
WidthRequest="40" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold"
VerticalOptions="Center" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="ExternalContactSearchTemplate">
<Grid Padding="10" ColumnDefinitions="Auto,Auto,Auto">
<Label Grid.Column="0"
Text="{Binding FirstName}"
FontAttributes="Bold"
HorizontalOptions="Start"
VerticalOptions="Center" />
<Label Grid.Column="1"
Text="{Binding LastName}"
FontAttributes="Bold"
HorizontalOptions="Start"
VerticalOptions="Center" />
<Label Grid.Column="2"
Text="{Binding Title}"
FontAttributes="Bold"
HorizontalOptions="End"
VerticalOptions="Center" />
</Grid>
</DataTemplate>
发布于 2022-10-03 16:57:29
这当然很有趣,因为我假设在每个页面中设置SearchHandler会解决这个问题,但是我将Shell.SearchHandler XAML移动到相应页面中的C# (如下所示),而且它仍然只使用了第一个使用的页面。我很困惑。我每次都需要从头开始重建整个Shell吗?在呈现之前,我并不反对将所有这些移到代码与XAML之间,但这似乎是不必要的,只是感觉我遗漏了什么。
Users.xaml.cs
public UsersPage()
{
InitializeComponent();
Shell.SetSearchHandler(this, new UserSearchHandler
{
Placeholder = "Enter first or last name",
ShowsResults = true,
SelectedItemNavigationTarget = typeof(UserDetailPage),
Users = UserData.Users,
ItemTemplate = new DataTemplate(() =>
{
Grid grid = new Grid { Padding = 10 };
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.15, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.85, GridUnitType.Star) });
Image image = new Image { HeightRequest = 40, WidthRequest = 40 };
image.SetBinding(Image.SourceProperty, "Images[1].ImageUri");
Label nameLabel = new Label { FontAttributes = FontAttributes.Bold, VerticalOptions = LayoutOptions.Center };
nameLabel.SetBinding(Label.TextProperty, "Name");
grid.Children.Add(image);
grid.Children.Add(nameLabel);
return grid;
})
});
}
ContactsPage.xaml.cs
public ContactsPage()
{
InitializeComponent();
Shell.SetSearchHandler(this, new ExternalContactSearchHandler
{
Placeholder = "Enter search term",
ShowsResults = true,
SelectedItemNavigationTarget = typeof(UserDetailPage),
ExternalContacts = ExternalContactData.ExternalContacts,
ItemTemplate = new DataTemplate(() =>
{
Grid grid = new Grid { Padding = 10 };
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
Label firstNameLabel = new Label { FontAttributes = FontAttributes.Bold, HorizontalOptions = LayoutOptions.Start, VerticalOptions = LayoutOptions.Center };
firstNameLabel.SetBinding(Image.SourceProperty, "FirstName");
Label lastNameLabel = new Label { FontAttributes = FontAttributes.Bold, HorizontalOptions = LayoutOptions.Start, VerticalOptions = LayoutOptions.Center };
lastNameLabel.SetBinding(Label.TextProperty, "LastName");
Label titleLabel = new Label { FontAttributes = FontAttributes.Bold, HorizontalOptions = LayoutOptions.End, VerticalOptions = LayoutOptions.Center };
titleLabel.SetBinding(Label.TextProperty, "Title");
grid.Children.Add(firstNameLabel);
grid.Children.Add(lastNameLabel);
grid.Children.Add(titleLabel);
return grid;
})
});
}
发布于 2022-10-07 01:07:49
Shell.SetSearchHandler
会影响整个 Shell。它是全球性的。当您调用它时,所有页面都会受到影响。
将其放入构造函数中是无效的,因为在加载Shell时调用了构造函数:所有页面都被“构造”,甚至那些尚未可见的页面也是如此。
未来:当"MAUI OnAppearing未调用选项卡页“是固定的时
将以Shell.SetSearchHandler...
开头的代码移动到每个页面的OnAppearing
方法中。当该页即将可见时,它将运行:
protected override void OnAppearing(...)
{
base.OnAppearing();
Shell.SetSearchHandler...
...
}
替代解决方案
Shell有一个在转到新页面时调用的Navigating
event。使用此选项可设置不同的搜索处理程序,具体取决于导航到:
protected override void OnNavigating(ShellNavigatingEventArgs args)
{
base.OnNavigating(args);
// Test which page is next.
if (args.Target == /*...uri of your page...*/)
{
// ... your code here to set search handler ...
}
}
https://stackoverflow.com/questions/73913765
复制相似问题