如何从不同的ViewModel访问ViewModel中的列表

问题描述 投票:0回答:1

当我从逻辑中加载了一个列表并想使用其他viewModel从该列表中选择一些值时,我遇到了一个问题。

public List<ContactsListModel> ExampleList
{
    get => _exampleList;
    set
    {
        _exampleList = value;
        PropertyChanged(this, new PropertyChangedEventArgs(nameof(ExampleList)));
    }
}

上面有我的列表,其中包含一些数据。我不想再次加载它。如何在另一个viewModel中从中获取值?

xamarin xamarin.forms
1个回答
0
投票

您可以将视图模型的实例传递到要使用它的页面,从那里可以访问它。

1。您要访问的Viewmodel:

Pushasync(new YourPage(this));  //Passed the instance of your view model

2。您要访问的页面:

public partial class DemoClass
{
    YourViewModel demo_;

    public DemoClass()
    {
        InitializeComponent();
    }

    public DemoClass(Yourviewmodel demo)
    {
        InitializeComponent();
        demo_ = demo;  // access the instance and all its properties
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.