【问题标题】:Xamarin MVVM Reference Content View and Pass Parameters TooXamarin MVVM 参考内容查看和传递参数
【发布时间】:2020-06-05 04:46:13
【问题描述】:

以 xamarin 形式工作。如何从内容页面引用内容视图,但还包括传递信息,如下所示?

await Navigation.PushAsync(new SecondContentPage(new NextViewModel(FirstViewModel.id)));

我尝试过 xaml 绑定上下文,但不知道从那里去哪里,它不断给我关于无参数构造函数的错误。

xmlns:viewmodel="clr-namespace:Project.ViewModels"

       <StackLayout>
            <local:SecondContentView>
                <local:SecondContentView.BindingContext>
                    <viewmodel:NextViewModel></viewmodel:NextViewModel>
                </local:SecondContentView.BindingContext>
            </local:SecondContentView>
        </StackLayout>

我基本上需要传递的 id,以便列表可以在内容视图上运行。谢谢大家

更新 - 我创建了一些新的示例代码。我创建了一个页面以使用 Listview 嵌套第二个页面。在我尝试使用 x:Arguments 或在 ViewModel 构造函数中从第一页到第二页传递参数之前效果很好。

首页

**<StackLayout>

        <StackLayout>
            <Label Text="First Page Content"></Label>
        </StackLayout>

        <StackLayout>
            <local:SecondContentView>
                <local:SecondContentView.BindingContext>
                    <viewmodel:SecondViewModel>
                        <x:Arguments>102</x:Arguments>
                    </viewmodel:SecondViewModel>
                </local:SecondContentView.BindingContext>
            </local:SecondContentView>
        </StackLayout>

    </StackLayout>**

第二页

        <ContentView.Content>

    <StackLayout>

        <Label Text="Second Page"></Label>

        <ListView x:Name="FirstListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="10">
                            <Label Text="{Binding pType}"></Label>
                            <Label Text="{Binding fDepartment}"></Label>
                            <Label Text="{Binding Description}"></Label>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>

    </StackLayout>

</ContentView.Content>

后面的第二页代码

SecondViewModel ViewModel;

    public SecondContentView(SecondViewModel viewmodel)
    {
        InitializeComponent();

        BindingContext = ViewModel = viewmodel;

        FirstListView.ItemsSource = ViewModel.TypeList;
    }

第二页的视图模型

**public List<TypeModel> TypeList;

    public SecondViewModel(int parameter)
    {
        var p = parameter;

        TypeList = new List<TypeModel>()
        {
            new TypeModel { pType = 1, Title = "First Type", Description = "First Description", Version = "9.9.9", fDepartment = 101 , Comments = "None"},
            new TypeModel { pType = 2, Title = "Second Type", Description = "Second Description", Version = "9.9.9", fDepartment = 101 , Comments = "None"},
            new TypeModel { pType = 3, Title = "Third Type", Description = "Third Description", Version = "9.9.9", fDepartment = 102 , Comments = "None"},
            new TypeModel { pType = 4, Title = "Fourth Type", Description = "Fourth Description", Version = "9.9.9", fDepartment = 102 , Comments = "None"}
        };

    }**

【问题讨论】:

    标签: xaml xamarin mvvm data-binding


    【解决方案1】:

    您只需将 SecondContentPageBindingContext 设置为 NextViewModel

    SecondContentPage.xaml.cs构造函数中:

    public SecondContentPage(NextViewModel vm)
    {
        InitializeComponent();
        localVm = vm;
        BindingContext = localVm;
    }
    
    private readonly NextViewModel localVm;
    

    然后,您将使用绑定访问 xaml 中 FirstViewModel.Id 存储在 NextViewModel 中的属性。在本例中,我们会说 ID 存储在 NextViewModel.Id 中。使用Label 的示例:

    <Label Text="{Binding Id}"/>
    

    【讨论】:

    • 我编辑了我的问题。您提供的帮助效果很好,但我正在尝试传递一个参数,所以我基本上可以过滤列表。
    【解决方案2】:

    我能够通过另一个模拟示例找到解决方案。关键是 x:Argument 和一个覆盖方法。

    首先,如果你想传递简单的类型值,我们需要明确指出它的类型。试试这个格式:

    <StackLayout>
        <local:SecondContentView>
            <local:SecondContentView.BindingContext>
                <local:SecondViewModel>
                    <x:Arguments>
                        <x:Double>102</x:Double>
                    </x:Arguments>
                </local:SecondViewModel>
            </local:SecondContentView.BindingContext>
        </local:SecondContentView>
    </StackLayout>
    

    改变它的构造函数:

    public SecondViewModel(double parameter)
    {
        var p = parameter;
    
        TypeList = new List<TypeModel>()
        {
            new TypeModel { pType = 1, Title = "First Type", Description = "First Description", Version = "9.9.9", fDepartment = 101 , Comments = "None"},
            new TypeModel { pType = 2, Title = "Second Type", Description = "Second Description", Version = "9.9.9", fDepartment = 101 , Comments = "None"},
            new TypeModel { pType = 3, Title = "Third Type", Description = "Third Description", Version = "9.9.9", fDepartment = 102 , Comments = "None"},
            new TypeModel { pType = 4, Title = "Fourth Type", Description = "Fourth Description", Version = "9.9.9", fDepartment = 102 , Comments = "None"}
        };
    }
    

    其次,您没有将参数传递给第二个视图的构造函数。相反,您使用了默认的 null 参数构造函数并直接设置其绑定上下文。所以不会触发 public SecondContentView(SecondViewModel viewmodel)。我们可以直接在您的第二个视图中使用绑定上下文,例如:

    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
    
        ViewModel = BindingContext as SecondViewModel;
        FirstListView.ItemsSource = ViewModel.TypeList;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多