【问题标题】:Xamarin.Forms use current ContentPage as CommandProperty in XAMLXamarin.Forms 在 XAML 中使用当前 ContentPage 作为 CommandProperty
【发布时间】:2018-03-09 06:16:32
【问题描述】:

我需要当前可见的 ContentPage,它绑定到视图模型,作为视图模型命令的 CommandParameter

点赞:

<Button Text="Save" 
        Command="{Binding AddObjectAndClosePage}" 
        CommandParameter={Binding MyContentPage} />

但 Xamarin 不支持 ElementNamebinding。我现在如何使用我的 ContentPage 作为参数?

【问题讨论】:

  • 该方法是我的视图模型的属性:)。但是我发现这种方法是完全错误的。

标签: c# xaml xamarin.forms xaml-binding


【解决方案1】:

好吧,这完全是我的错。 我的方法是打破 mvvm 模式的所有规则。

您可以通过编写视图模型的命令方法访问Navigationcontext:

Application.Current.MainPage.Navigation.PopModalAsync();

经验教训。

【讨论】:

  • 考虑使用 Prism,它公开了一个 INavigationService,可以注入到您的视图模型中并完全用于该目的。
  • @PaulKertscher 感谢您的提示。我忽略了这一点,因为我想在添加外部依赖项之前先“了解”基础知识。但也许这是错误的方式。
  • 了解基础知识固然很好,但是一旦涉及到任何现实世界的项目,您就可以使用 Prism 帮自己一个忙 ;)
  • 我明白了:)。感谢您的提示。
猜你喜欢
  • 1970-01-01
  • 2016-12-24
  • 2014-07-24
  • 2019-10-16
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多