简体   繁体   English

如何在Xamarin.Forms中将多个页面的BindingContext设置为相同的ViewModel?

[英]How to set BindingContext of multiple pages to the same ViewModel in Xamarin.Forms?

I'm new to Xamarin.Forms and I'd like to create a cross-platform app using MVVM pattern and XAML. 我是Xamarin.Forms的新手,我想使用MVVM模式和XAML创建一个跨平台的应用程序。 In my forms project (pcl) I'd like to set the BindingContext of my MainPage and also multiple pages in the future to the same ViewModel. 在我的表单项目(pcl)中,我想将MainPageBindingContext以及将来的多个页面设置为同一ViewModel。 Is this possible? 这可能吗? Let me show what I'm talking about. 让我看看我在说什么。 Below is a code snippet from an earlier WPF project of mine ( App.xaml.cs ): 下面是我的早期WPF项目( App.xaml.cs )的代码片段:

public partial class App : Application
{
    private MainWindow _MainWindow;
    private MyViewModel _ViewModel;

    public App()
    {
        _ViewModel = new MyViewModel();

        _ViewModel.SomeEvent += new System.EventHandler(ViewModel_SomeEvent);
    }
}



protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    _MainWindow = new MainWindow();
    _MainWindow.DataContext = _ViewModel;
    _MainWindow.Show();         
}

private void ViewModel_SomeEvent(object sender, EventArgs e)
{
     //Do something
}

The contents of the ViewModel are not important. ViewModel的内容并不重要。 With this structure I was able to set the same _ViewModel object as the DataContext of multiple windows. 通过这种结构,我可以设置与多个窗口的DataContext相同的_ViewModel对象。 Is there an equivalent to this in Xamarin.Forms? Xamarin.Forms中有与此等效的东西吗?

Here is a simple code from my pcl project ( App.cs ): 这是我的pcl项目( App.cs )中的简单代码:

public class App
{
    public static Page GetMainPage ()
    {   
        return new MainPage();
    }
} 

And the code from MainPage.xaml.cs : 以及来自MainPage.xaml.cs的代码:

public partial class MainPage : ContentPage
{   
    public MainPage ()
    {
        InitializeComponent ();
        BindingContext = new MyViewModel ();
    }
}

I understand this is a proper way to set the BindingContext of a page but I'm wondering if this will result in creating a new ViewModel object with default values every time I open MainPage . 我知道这是设置页面的BindingContext的正确方法,但是我想知道这是否会导致每次我打开MainPage都使用默认值创建一个新的ViewModel对象。 And also I don't understand how other pages will be able to use the same ViewModel object as MainPage . 而且我也不明白其他页面将如何使用与MainPage相同的ViewModel对象。 In my opinion the above WPF project code is logical and simple. 在我看来,以上WPF项目代码是合理且简单的。 One ViewModel object and that's it. 一个ViewModel对象就是这样。 Do I have to create different ViewModel classes to every page? 我是否必须为每个页面创建不同的ViewModel类? That's just seems wrong to me. 对我来说,这似乎是错误的。

So is there a way to somehow create one ViewModel object - maybe in App.cs ? 那么有没有办法以某种方式创建一个ViewModel对象-也许在App.cs中 I doubt it. 我对此表示怀疑。 - somewhere to be used by every page I might want to add to the project later. -以后我可能要添加到项目中的每个页面都可以使用的地方。 I hope I've been clear and thank you in advance! 我希望我已经很清楚了,并预先感谢您!

Yes, you can certainly set the BindingContext of a page to an object that your application manages; 是的,您当然可以将页面的BindingContext设置为应用程序管理的对象。 the ViewModel does not have to be created (or even set) inside the constructor; 不必在构造函数内部创建(或设置)ViewModel; that just happens to be what a lot of sample code does. 恰好是许多示例代码所做的。

There are several approaches you can take for: a ViewModelLocator that creates a single ViewModel and exposes it to any view that uses that ViewModelLocator to wire up the binding context, a dependency injection container (like the SimpleIOC that MvvmLight provides) with the ViewModel registered as a singleton, manually setting it in a Page factory, and so on. 您可以采用几种方法:一个ViewModelLocator创建一个ViewModel并将其公开给使用该ViewModelLocator连接绑定上下文的任何视图,一个依赖项注入容器(例如MvvmLight提供的SimpleIOC),该ViewModel注册为单例,在Page工厂中手动设置它,依此类推。

A simple example using a ViewModelLocator referenced in the ctor would be: 使用ctor中引用的ViewModelLocator的一个简单示例为:

public static class ViewModelLocator
{
    private static MyViewModel _myViewModel = new MyViewModel();
    public static MyViewModel MainViewModel
    {
        get
        {
            return _myViewModel;
        } 
    } 
}

...

public partial class MainView : ContentPage
{
    public MainView()
    {
        BindingContext = ViewModelLocator.MainViewModel;
    }
}

...

public partial class SomeOtherView : ContentPage
{
    public SomeOtherView()
    {
        BindingContext = ViewModelLocator.MainViewModel;
    }
}

You can also have it so that the _myViewModel member is set through a setter on the ViewModelLocator, pull it from an IOC container, etc. 您也可以使用它,以便通过ViewModelLocator上的设置器设置_myViewModel成员,将其从IOC容器中拉出,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Xamarin.Forms上的XAML中将BindingContext设置为ViewModel - Set BindingContext to ViewModel in XAML on Xamarin.Forms MVVM中的Xamarin.Forms bindingContext - Xamarin.Forms bindingContext in MVVM 如何在 xamarin 形式的新 mvvm 中将相同的视图模型设置为两个视图 - How to set same viewmodel to two views in fresh mvvm in xamarin forms 在Xamarin.Forms中,如何将同一个viewmodel的变化通知回上一页? (可以跳转到第二页,但不能返回) - In Xamarin.Forms, how to notify the changes of the same viewmodel back to the previous page? (can pass to the second page, but not back) 如何在 xamarin.forms 中的 viewcell 中设置按钮以从内容页面 viewmodel 调用命令? - How do I set a button in viewcell in xamarin.forms to call a command from the content page viewmodel? xamarin BindingContext 与 ViewModel - xamarin BindingContext with ViewModel Xamarin.Forms 从 mvvm ViewModel 设置焦点 - Xamarin.Forms set focus from mvvm ViewModel Xamarin Forms - ViewCell 中 BindingContext 中的多个项目 - Xamarin Forms - Multiple items in BindingContext in ViewCell Xamarin.Forms MVVM ViewModel 如何与数据库交互? - Xamarin.Forms MVVM How does the ViewModel Interact with the Database? 如何从 ViewModel 中删除方法的执行 - Xamarin.Forms - MVVM - How to remove the execution of a method from a ViewModel - Xamarin.Forms - MVVM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM