【问题标题】:xaml viewmodel declaration in prismprism中的xaml viewmodel声明
【发布时间】:2020-02-27 22:30:29
【问题描述】:

我将 PRISM 与 xamarin 表单一起使用,并且我喜欢在 XAML 中声明我的视图模型,

xmlns:local="clr-namespace:MyProyect.ViewModels" 

……

<ContentPage.BindingContext>
    <local:RegistroPageViewModel />
</ContentPage.BindingContext>

所以我可以拥有 XAML 智能,在这个示例中,我的 RegistroPageViewModel 构造函数有一个参数,因为它需要基类,但我不知道如何在 xaml 中传递它

public class RegistroPageViewModel : ViewModelBase
{
    public RegistroPageViewModel(INavigationService navigationService):base(navigationService)
    {
        registro = new RegistroInfo();
        Title = "Perfil de usuario";
    }

我的具体问题是:如果 viewmodel 有参数,我如何仍然使用 XAML viemodels 的声明?如何在 XAML 声明中传递参数?

提前考虑

【问题讨论】:

标签: xamarin.forms prism


【解决方案1】:

要让 XAML 了解 ViewModel,请启用 XamlCCompiled Bindings。文档提供了如何正确启用和使用它们。

XamlC 检查一般编译时错误,如属性名称和打开关闭匹配标记等...

Compiled Bindings 检查是否存在任何绑定的属性

【讨论】:

    【解决方案2】:

    您可以使用视图模型定位器 (ViewModelLocator.AutowireViewModel="True") 为您创建视图模型,并自动注入所有依赖项。

    将视图模型设置为设计数据上下文 (d:DataContext={d:DesignInstance local:RegistroPageViewModel}) 应该会给您智能感知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多