XAML .Net Maui Shell导航-是否可以传递查询参数并自动填充页面?

fnvucqvd  于 2022-12-07  发布在  .NET
关注(0)|答案(3)|浏览(159)

我需要通过将Shell导航参数传递给ViewModel/方法来自动填充页面,并调用服务以从Web服务返回单个记录。本质上是一个穿透钻取页面。我的问题是我需要调用数据检索命令,"取得字段效能异步"(注意[ICommand]会将此转换为"GetFieldPerformanceCommand")。这是必需的,因为在加载页面之前,不会在ViewModel中设置Shell导航参数。我当前无法从 * OnNavigatedTo * 进行命令调用,需要有关如何完成此操作的建议。
谢谢你!
页面背后的代码:

public partial class FieldPerformancePage : ContentPage
{
   public FieldPerformancePage(FieldPerformanceViewModel viewModel)
   {
       InitializeComponent();
       BindingContext = viewModel;
    
       //works with parameter hard-coded in ViewModel
       //viewModel.GetFieldPerformanceCommand.Execute(null);
   }

   FieldPerformanceViewModel viewModel;
   protected override void OnNavigatedTo(NavigatedToEventArgs args)
   {
       base.OnNavigatedTo(args);
       //this does not work 
       viewModel.GetFieldPerformanceCommand.Execute(null);
   }
}

检视模型

namespace TrackMate.ViewModels;

[QueryProperty(nameof(FieldAssignedWbs), nameof(FieldAssignedWbs))]
public partial class FieldPerformanceViewModel : BaseViewModel
{
   [ObservableProperty]
   FieldAssignedWbs fieldAssignedWbs;

   [ObservableProperty]
   FieldPerformance fieldPerformance;

   FieldPerformanceService fieldPerformanceService;

   public FieldPerformanceViewModel(FieldPerformanceService fieldStatusService)
   {
       Title = "Status";
       this.fieldPerformanceService = fieldStatusService;
   }

   [ICommand]
   async Task GetFieldPerformanceAsync()
   {
       if (IsBusy)
           return;
       try
       {
           IsBusy = true;

           int wbsId = fieldAssignedWbs.WbsId;

           var fieldPerformanceList = await fieldPerformanceService.GetFieldPerformanceList(wbsId);

           if (fieldPerformanceList.Count != 0)
               FieldPerformance = fieldPerformanceList.First();
       }
       catch (Exception ex)
       {
           Debug.WriteLine(ex);
           await Shell.Current.DisplayAlert("Error!",
               $"Undable to return records: {ex.Message}", "OK");
       }
       finally
       {
           IsBusy = false;
       }
   }
}
svujldwt

svujldwt1#

我想我明白了...
通过在“DetailsPage”代码隐藏中的OnNavigatedTo方法内添加ViewModel绑定,可以对页面的ViewModel进行命令调用以在 shell 导航参数之后执行数据检索方法(在此场景中为对象)。注意,由于查询参数来源于ViewModel,因此传递的是null。如果您像我一样是.Net Maui的新手,我推荐James Montemagno在.Net Maui Shell Navigation上的视频。

namespace TrackMate.Views;

public partial class FieldPerformancePage : ContentPage
{
   public FieldPerformancePage(FieldPerformanceViewModel viewModel)
   {
      InitializeComponent();
      BindingContext = viewModel;
   }

   protected override void OnNavigatedTo(NavigatedToEventArgs args)
   {
       FieldPerformanceViewModel viewModel = (FieldPerformanceViewModel)BindingContext;
       viewModel.GetFieldPerformanceCommand.Execute(null);

       base.OnNavigatedTo(args);
   }
}
ymdaylpp

ymdaylpp2#

对我来说,只有在组件初始化之前赋值BindingContext,并且在OnNavigatedTo中的基调用之后调用方法时,它才起作用

public partial class OccurrencePage : ContentPage
{

    public OccurrencePage(OccurrenceViewModel model)
    {
        BindingContext = model;
        InitializeComponent();
    }

  

    protected override void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);

        OccurrenceViewModel viewModel = (OccurrenceViewModel)BindingContext;
        viewModel.GetFieldsCommand.Execute(null);
    }
}
ppcbkaq5

ppcbkaq53#

While overriding OnNavigatedTo works fine, there is one more simple technique to run something once your query param is set, given you do not need to run anything asynchronous inside the method: implementing partial method OnFieldAssignedWbsChanged, auto-generated for your convenience by mvvm toolkit

partial void OnFieldAssignedWbsChanged(FieldAssignedWbs value)
    {
        // run synchronous post query param set actions here 
    }

Less amount of code and less code-behind and viewModel dependencies, but works fine for non-async operations only.

相关问题