简体   繁体   中英

Inject object created in code-behind to ViewModel

Im creating an app using Bing Map and I create an Instance of the map in my XAML-file like this:

<maps:Map x:Name="MyMap"/>

I need to pass this instance of the Map to my viewModel, which is in another project(PCL), in order to "use" it. I have

I assume that the Map gets created in this.InitializeComponent() in the codebehind.

public MainPage()
{            
    this.InitializeComponent();                     
}

This is also where my DI gets triggered:

 public ViewModelLocator()
        {            
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            SimpleIoc.Default.Register<MainViewModel>();            
            SimpleIoc.Default.Register<IMap, MyBingMap>();                    
        }

MyBingMap is the implementation of IMAP that I wish to pass to the VM. But before I do that I need it to have the instance of the Map that gets created in the code-behind.

I hope my questions is clear enough for someone. Im open to all suggestions that can pass myMap to the VM. Thank you!

地图本身有一个Loaded事件,您可以在载入地图后使用它来获取地图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM