如何在xamarin.forms中的viewmodel中使用参数

sla*_*r35 5 c# mvvm visual-studio xamarin xamarin.forms

我试图将参数传递给 Xamarin.Forms 中的视图模型。但是我也想在加载页面时使用它,以使用该参数调用休息服务。但是在加载事件参数中始终为 null。(如果我可以看到该参数将其绑定到标签等...)我怎样才能使其正常工作并在视图模型的加载事件中使用该参数?任何帮助表示赞赏,谢谢

这是按钮单击事件并导航到 Samplepage。

 private void OntaptedBildirimItem(string param)
    {
      this._navigationService.NavigateTo(nameof(Views.SamplePage), param);
    }
Run Code Online (Sandbox Code Playgroud)

这是示例页面视图模型

      private string _id;
    public string id
    {
        get
        {
            return _id;
        }
        set
        {
            Set(() => id, ref _id, value);
        }
    }    
public SamplePageViewModel(INavigationService navigationService) : base(navigationService)
    {
        this._navigationService = navigationService;
        this.Title = "Sample Page=" + id; // here the id is always null,but if I use it to bind a label in xaml ,it has a value.
    }
Run Code Online (Sandbox Code Playgroud)

这是示例页面代码

     public SamplePage(string parameter)
    {
        InitializeComponent();
        var viewModel = this.BindingContext as ViewModels.SamplePageViewModel;
        if(viewModel != null && parameter != null)
        {
            viewModel.bildirimid = parameter;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mig*_*ñoz 2

我们需要查看 NavigationService 。

您应该在 NavigationService 中而不是在 SamplePage 中添加绑定上下文。

此示例中,使用带有参数的 NavigationService 和更好的 NavigationService 结构。你应该看到它。

请发送反馈。