我不知道如何为 Shell 对象创建自定义 ItemTemplate
AppShell.xaml.cs
using AppStockAndOrdersMAUI.Views.Orders;
using CommunityToolkit.Maui.Markup;
namespace AppStockAndOrdersMAUI;
public partial class AppShell : Shell
{
public AppShell()
{
Items.Add(new ShellContent()
{
Title = "Home",
ContentTemplate = new DataTemplate(() => new MainPage()),
Route = nameof(MainPage)
});
Items.Add(new ShellContent()
{
Title = "Orders",
ContentTemplate = new DataTemplate(() => new OrdersListPage()),
Route = nameof(OrdersListPage)
});
ItemTemplate = new DataTemplate(() => new Label().Bind(Label.TextProperty, "Title"));
Routing.RegisterRoute(nameof(OrdersListPage), typeof(OrdersListPage));
}
}
AppShell.xaml
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="AppStockAndOrdersMAUI.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AppStockAndOrdersMAUI"
Title="AppStockAndOrdersMAUI">
</Shell>
我也尝试过
ItemTemplate = new DataTemplate(() => new Label().Bind(Label.TextProperty, nameof(ShellContent.Title)));
菜单中不再显示项目
问题解决了,添加了
Grid
,现在一切都按预期显示了
using AppStockAndOrdersMAUI.Views.Orders;
using CommunityToolkit.Maui.Markup;
namespace AppStockAndOrdersMAUI;
public partial class AppShell : Shell
{
public AppShell()
{
Items.Add(new ShellContent()
{
Title = "Home",
ContentTemplate = new DataTemplate(() => new MainPage()),
Route = nameof(MainPage)
});
Items.Add(new ShellContent()
{
Title = "Orders",
ContentTemplate = new DataTemplate(() => new OrdersListPage()),
Route = nameof(OrdersListPage)
});
ItemTemplate = new DataTemplate(() =>
new Grid
{
Padding = 10,
Children =
{
new Label().Bind(Label.TextProperty, "Title").FontSize(20)
.TextColor(Color.FromHex("#00A1B1"))
.CenterVertical()
.Start()
}
});
Routing.RegisterRoute(nameof(OrdersListPage), typeof(OrdersListPage));
}