[英].Net Maui/XAML QueryParameter is NULL in the viewmodel Constructor but usable in XAML, how do I access it in the viewmodel?
[英]How to pass QUERYPARAMETER to ViewModel constructor?
我正在學習如何在 maui 應用程序上使用,我使用 AppShell 將數據從一個視圖 model 傳遞到下一個頁面視圖模型,以及使用 CommunityToolkit.Mvvm。 但我不知道如何將返回查詢參數傳遞給下一頁視圖模型方法。
下一頁的視圖模型
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 方法?
如果要將參數傳遞到下一頁,可以使用 Shell。 您可以將數據放入字典中並將其傳遞到下一頁。
async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Animal animal = e.CurrentSelection.FirstOrDefault() as Animal;
var navigationParameter = new Dictionary<string, object>
{
{ "Bear", animal }
};
await Shell.Current.GoToAsync($"nextpage", navigationParameter);
}
在下一頁中,可以通過為每個基於字符串的查詢參數和基於對象的導航參數裝飾接收 class 的QueryPropertyAttribute
來接收導航數據QueryPropertyAttribute
的第一個參數指定將接收數據的屬性的名稱,第二個參數指定參數 id。
[QueryProperty(nameof(Bear), "Bear")]
public partial class BearDetailPage : ContentPage
{
Animal bear;
public Animal Bear
{
get => bear;
set
{
bear = value;
OnPropertyChanged();
}
}
public BearDetailPage()
{
InitializeComponent();
BindingContext = this;
}
}
有關更多詳細信息,您可以參考: https://docs.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation#pass-data
不能,因為在將 QueryParameter 傳遞給 ViewModel 之前會調用構造函數。
但是,您有很多方法可以實現您的目標。 一種方法是設置頁面的 Appearing 方法並調用 ViewModel 的 function。
在您的 xaml 頁面集中
Appearing="Page_Appearing"
然后在您的 page.cs 文件集中,確保您可以訪問 ViewModel。 (例如在 XAML 中定義或使用定位器/容器服務)
public AddProjectPage()
{
InitializeComponent();
this.BindingContext = ViewModel;
}
private async void Page_Appearing(object sender, EventArgs e)
{
base.OnAppearing();
await this.ViewModel.InitAsync(); //
}
在你的 ViewModel 中定義一個名為 InitAsync 的InitAsync
來完成你所有的初始化工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.