【发布时间】:2022-09-28 18:40:12
【问题描述】:
我正在学习如何在使用 AppShell 将数据从一个视图模型传递到下一页视图模型以及使用 CommunityToolkit.Mvvm 的 maui 应用程序上使用。但我不知道如何将返回查询参数传递给下一页 viewmodel 方法。
下一页的视图模型
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using CommunityToolkit.Mvvm.ComponentModel;
using TimesNewsApp.Models;
using TimesNewsApp.Services;
namespace TimesNewsApp.ViewModels
{
[QueryProperty(nameof(SelectedGenre), nameof(SelectedGenre))]
public partial class MovieListGenrePageViewModel : BaseViewModel
{
public ObservableCollection<Result> Movie { get;} = new();
[ObservableProperty]
private Genre selectedGenre;
NewsApiManager apiService;
public Command GetMovieComand { get; }
public MovieListGenrePageViewModel(NewsApiManager apiService)
{
this.apiService = apiService;
Task.Run(async () => await GetMovies(SelectedGenre));
}
async Task GetMovies(Genre SelectedGenre)
{
if (IsBusy)
return;
try
{
IsBusy = true;
if (SelectedGenre == null)
return;
Movie movies = await apiService.GetMovieByGenre(SelectedGenre.Id);
if (Movie.Count != 0)
return;
foreach (var item in movies.results)
Movie.Add(item);
}
catch (Exception ex)
{
Debug.WriteLine($\"Unable to get movie: {ex.Message}\");
await Application.Current.MainPage.DisplayAlert(\"Error!\", ex.Message, \"OK\");
}
finally
{
IsBusy = false;
}
}
}
}
我尝试在构造函数中使用它;
public MovieListGenrePageViewModel(NewsApiManager apiService){
...
this.SelectedGenre = SelectedGenre;
Task.Run(async () => await GetMovies(SelectedGenre));
}
但 SelectedGenre 返回 null。请问如何将 Object SelectedGenre 设置为 GetMovie 方法?