Bind to Xamarin.Forms.Maps.Map from ViewModel

后端 未结 5 1659
失恋的感觉
失恋的感觉 2020-12-29 12:49

I\'m working on a Xamarin.Forms app using a page that displays a map. The XAML is:


    ...

<
5条回答
  •  情歌与酒
    2020-12-29 13:19

    If you don't want to break the MVVM pattern and still be able to access your Map object from the ViewModel then you can expose the Map instance with a property from your ViewModel and bind to it from your View.

    Your code should be structured like described here below.

    The ViewModel:

    using Xamarin.Forms.Maps;
    
    namespace YourApp.ViewModels
    {
        public class MapViewModel
        {
            public MapViewModel()
            {
                Map = new Map();
            }
    
            public Map Map { get; private set; }
        }
    }
    

    The View (in this example I'm using a ContentPage but you can use whatever you like):

    
    
        
                    
                    
        
    
    

    I didn't show how, but the above code snipped can only work when the ViewModel is the BindingContext of your view.

提交回复
热议问题