尝试将我的导航栏移动到 MAUI 屏幕底部,但这并不像我想象的那么简单。
我正在按照这篇文章的解决方案:这里
然而,我得到了The type or namespace name 'Platforms' does not exist in the namespace 'Project' (are you missing an assembly reference?)
。
此错误与此行有关 handlers.AddHandler<Shell, ShellHandler>();
我发现了几个有同样问题的帖子,但似乎没有一个提供解决方案。
有什么建议吗?
应用程序.xaml.cs
using Project._ViewModels.SharedViewModel;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Hosting;
using CommunityToolkit.Maui;
using Project.Helpers;
using Project.Platforms; ///this is where the error is
namespace Project
{
/// <summary>
/// Represents the main application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
/// </summary>
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<Shell, ShellHandler>(); });
return builder.Build();
}
}
}
ShellHandler.cs(路径:Project\Platforms\Android\ShellHandler.cs)
using Android.Content;
using AndroidX.CoordinatorLayout.Widget;
using Google.Android.Material.BottomNavigation;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
using Svg;
using System;
namespace Project.Platforms.Android
{
public partial class ShellHandler : ShellRenderer
{
public ShellHandler(Context context) : base(context) { }
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(Microsoft.Maui.Controls.ShellItem shellItem)
{
return new ShellBottomNavViewAppearanceTracker(this, shellItem.CurrentItem);
}
public class ShellBottomNavViewAppearanceTrackerEx : ShellBottomNavViewAppearanceTracker
{
public ShellBottomNavViewAppearanceTrackerEx(IShellContext shellContext, Microsoft.Maui.Controls.ShellItem shellItem)
: base(shellContext, shellItem) { }
public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
base.SetAppearance(bottomView, appearance);
}
}
}
}
仅当针对 Android 设备构建项目时,Android 文件夹中的文件才会被编译并包含在应用程序中。同样,仅当针对这些平台时,才会包含 iOS 或 Windows 文件夹中的文件。
由于
ShellHandler
位于 Android 文件夹内,因此只有在针对 Android 平台编译应用程序时才可以访问此文件中定义的命名空间。在上面的代码中,您尝试访问共享代码中特定于平台的文件
App.xaml.cs
。您无法直接执行此操作,其他平台将无法直接访问此文件。您必须使用条件编译了解更多...