xamarin MAUI导航返回Null

n8ghc7c1  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(152)

所以我有一个代码,我应该转换为MAUI从XAMARIN.问题是,我写了一个代码,有点使自己的页面与信息.所有的功能在信息部分工作正常.问题是,当它应该打开的页面,它充满了信息,基本上页面不会打开。在XAMARIN版本中,我们创建了一个NavigationManager来导航页面。每当它来到导航方法时,它说它是null。这不是我第一次遇到这个问题。当我第一次遇到这个问题时,我下载了Microsoft.Maui.Controls附带的NavigatableElement NuGet包。这是我最初修复它的方法:

private async void DataPacketsButton_Clicked(object sender, EventArgs e)
        {
                //This is the old version that I used the NavigationManager That I have Created.
                //var manageDataPacketsPage = new ManageDataPacketsPage();
                //await NavigationManager.Instance.Navigation.PushAsync(manageDataPacketsPage, true);               
                //This is the method that I used the NuGet Package NavigatableElement.
                await Navigation.PushAsync(new ManageDataPacketsPage());
        }

这是我试图转换为NavigatableElement的一个:

public async Task OpenParcelPage(string plotId)
        {
                var page = new ParcelPage();
                page.Fill(plotId);

                //This does not see the NavigatableElement Package. So its returning the "Navigation" Object null.
                await NavigationManager.Instance.Navigation.PushAsync(page);
        }

这是我创建的NavigationManager:

public class NavigationManager
    {
        public INavigation Navigation { get; private set; }

        #region Singleton
        private static readonly object _lock = new object();
        private static readonly object _updateLock = new object();
        private static NavigationManager instance = null;

        public static NavigationManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                        {
                            instance = new NavigationManager();
                        }
                    }
                }
                return instance;
            }
        }

我该如何解决这个问题?

uidvcgyl

uidvcgyl1#

或者,利用INaviagtion的功能,您可以编写自己的视图服务来导航页面。
下面是代码片段,供您在导航从第一页到第二页时参考:
1.创建视图服务:ViewServices.cs

public static class ViewServices 
{
       private static IServiceCollection Services;

       public static MauiAppBuilder UseViewServices(this MauiAppBuilder builder)
       {
            Services = builder.Services;
            return builder;
       }

        public static ContentPage ResolvePage<TService>(params object[] inputParameters)
        {
            var implementationType = Services
                                    .Where(service => service.ServiceType == typeof(TService) && service.ImplementationType != null)
                                    .Select(service => service.ImplementationType)
                                    .First();

           return (Activator.CreateInstance(implementationType, inputParameters)) as ContentPage;
        }
}

1.分别创建两个接口:

public interface IFirstPage 
{
}
public interface ISecondPage 
{
}

1.在后面的第一页代码中,注意第二页也需要实现ISecondPage

public partial class FirstPage : ContentPage,IFirstPage 
{
      public FirstPage()
      {
            InitializeComponent();

            BindingContext = new FirstPageViewModel(this.Navigation);
      }
}

首页.xaml:

<VerticalStackLayout> 
        <Label 
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />

        <Button
            Text="Take me to 2nd Page"
            Command="{Binding NavigateToSecondPageCommand}" />
</VerticalStackLayout>

1.FirstPageViewMode.cs

public partial class FirstPageViewModel 
{
    private INavigation _navigationService;

    public FirstPageViewModel(INavigation navigation) 
    { 
            this._navigationService= navigation; 
    }

    [RelayCommand]
    async void NavigateToSecondPage()
    {
         await _navigationService.PushAsync(ViewServices.ResolvePage<ISecondPage>());
     }
}

5.在App.xaml.cs中,修改起始页,如下所示:

MainPage = new NavigationPage(ViewServices.ResolvePage<IFirstPage>());

1.MauiProgram.cs

public static MauiApp CreateMauiApp() 
{
       var builder = MauiApp.CreateBuilder();
       builder
                  .UseMauiApp<App>()
                  .ConfigureFonts(fonts =>
                  {
                        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                        fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                  })
                  //don't forget adding the view service
                 .UseViewServices();

        builder.Services.AddSingleton<IFirstPage, FirstPage>();
        builder.Services.AddTransient<ISecondPage, SecondPage>();
        return builder.Build();
}
vsdwdz23

vsdwdz232#

导航管理器返回null的原因是因为我没有在主页面中实现它。

public MainPage()
        {
            InitializeComponent();

            //This is the one.
            NavigationManager.Instance.SetNavigation(Navigation);
        }

我知道不是很多人都在制作自己的导航管理器。我应该做的是阅读并制作 Alexandria 在上面告诉我的东西。但是如果有人偶然发现这个问题,他们可以像这样解决它。

相关问题