如何在Xamarin.Forms中将多个页面的BindingContext设置为相同的ViewModel? [英] How to set BindingContext of multiple pages to the same ViewModel in Xamarin.Forms?

查看:263
本文介绍了如何在Xamarin.Forms中将多个页面的BindingContext设置为相同的ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Xamarin.Forms的新手,我想使用MVVM模式和XAML创建一个跨平台的应用程序.在我的表单项目(pcl)中,我想将我的 MainPage BindingContext 以及将来的多个页面设置为同一ViewModel.这可能吗?让我展示我在说什么.以下是我的早期 WPF 项目( App.xaml.cs )的代码段:

I'm new to Xamarin.Forms and I'd like to create a cross-platform app using MVVM pattern and 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. 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):

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
}

ViewModel的内容并不重要.通过这种结构,我可以设置与多个窗口的 DataContext 相同的_ViewModel对象. Xamarin.Forms中有与此等效的东西吗?

The contents of the ViewModel are not important. With this structure I was able to set the same _ViewModel object as the DataContext of multiple windows. Is there an equivalent to this in Xamarin.Forms?

这是来自我的pcl项目( App.cs )的简单代码:

Here is a simple code from my pcl project (App.cs):

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

以及 MainPage.xaml.cs 中的代码:

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

我知道这是设置页面 BindingContext 的正确方法,但是我想知道这是否会导致每次打开MainPage时都使用默认值创建一个新的ViewModel对象.而且我也不明白其他页面将如何使用与MainPage相同的ViewModel对象.我认为上面的 WPF 项目代码是合乎逻辑且简单的.一个ViewModel对象就是这样.我必须为每个页面创建不同的ViewModel类吗?对我来说,这似乎是错误的.

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. And also I don't understand how other pages will be able to use the same ViewModel object as MainPage. In my opinion the above WPF project code is logical and simple. One ViewModel object and that's it. Do I have to create different ViewModel classes to every page? That's just seems wrong to me.

有没有办法以某种方式创建一个ViewModel对象-也许在 App.cs 中?我对此表示怀疑. -稍后我可能要添加到项目中的每个页面都将使用的位置.我希望我已经很清楚了,谢谢你!

So is there a way to somehow create one ViewModel object - maybe in 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!

推荐答案

是的,您当然可以将页面的BindingContext设置为应用程序管理的对象.不必在构造函数内部创建(或设置)ViewModel;恰好是许多示例代码所做的.

Yes, you can certainly set the BindingContext of a page to an object that your application manages; the ViewModel does not have to be created (or even set) inside the constructor; that just happens to be what a lot of sample code does.

您可以采用以下几种方法:一个ViewModelLocator创建一个ViewModel并将其公开给使用该ViewModelLocator连接绑定上下文的任何视图,一个依赖项注入容器(例如MvvmLight提供的SimpleIOC). ViewModel注册为单例,在Page工厂中手动设置它,依此类推.

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.

使用ctor中引用的ViewModelLocator的简单示例为:

A simple example using a ViewModelLocator referenced in the ctor would be:

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;
    }
}

您也可以使用它,以便通过ViewModelLocator上的设置器设置_myViewModel成员,将其从IOC容器中拉出,等等.

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.

这篇关于如何在Xamarin.Forms中将多个页面的BindingContext设置为相同的ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆