ViewModelLocator reference in XAML?

MaximeDumasMaximeDumas CAMember
edited April 2015 in Xamarin.Forms

Hi,

I am using MVVM Light with the ViewModelLocator. I would like to avoid setting the view model in the code behind using a line such as this:

BindingContext = ((ViewModelLocator)Application.Current.Resources["ViewModelLocator"]).Menu;

I would have expected this line to work:

ContentPage xmlns="http://xamarin.com/schemas/2014/forms" [...]
            BindingContext="{x:DynamicResource ViewModelLocator.Menu}">

The ViewModelLocator is resolved, but as soon as I put .Menu at the end, the binding is not resolved.

Also, is it possible to have some debug informations on binding? In WPF, we have diag:PresentationTraceSources.TraceLevel=High. Is there something similar in Xamarin?

Thank you!

Answers

  • MarkDavis.8627MarkDavis.8627 USMember, University

    Where you able to get the mvvm locator to bind? I'm having a similar need

  • NMackayNMackay GBInsider, University mod
    edited September 2015

    @MarkDavis.2107

    Hi,

    This works fine. I can be set the BindingContext from XAML.

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:XamFormsTestApp.CustomRenderers;assembly=XamFormsTestApp"
                xmlns:mvvm="clr-namespace:XamFormsTestApp;assembly=XamFormsTestApp"
                BindingContext="{Binding Source={x:Static mvvm:App.Locator}, Path=Details}"
                Title="{Binding SelectedVessel.VesName}"
                x:Class="XamFormsTestApp.View.DetailsPage">
      <TabbedPage.Children>
    

    I can send you a sample app if you can't get it to work, just PM me.

  • MikeGringauzMikeGringauz ILMember
    edited September 2015

    Hi,

    I have the same problem.

    I'm using Application.Resources in App.xaml to hold ViewModelLocator that I want to be able to bind to later on from ContentPage in MainPage.xaml.

    But I'm doing something wrong as the program fails with exception:
    StaticResource not found for key Locator

    How to write binding expression correctly?

    App.xaml:

    <Application xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                 xmlns:local="clr-namespace:Currency;assembly=Currency"
                 x:Class="Currency.App">
        <Application.Resources>
    
            <ResourceDictionary>
                <local:ViewModelLocator x:Key="Locator" />
            </ResourceDictionary>
    
        </Application.Resources>
    </Application>
    

    MainPage.xaml:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                 xmlns:local="clr-namespace:Currency;assembly=Currency"
                 x:Class="Currency.MainPage"
                 BindingContext="{Binding Main, Source={StaticResource Locator}}">
        <ContentPage.Content>
    
            <StackLayout>
                <Label Text="Hello to Currency MVVM!" />
            </StackLayout>
    
        </ContentPage.Content>
    </ContentPage>
    

    ViewModelLocator.cs:

        public class ViewModelLocator
        {
            public static ViewModelLocator Instance
            {
                get { return Application.Current.Resources["Locator"] as ViewModelLocator; }
            }
    
            public MainViewModel Main
            {
                get { return App.Container.Resolve<MainViewModel>(); }
            }
    
            public ViewModelLocator()
            {
            }
        }
    

    App.xaml.cs:

        public partial class App : Application
        {
            public static IContainer Container { get; set; }
    
            public App ()
            {
                // Register individual components
                var builder = new ContainerBuilder();
                builder.RegisterType<RestCurrencyService>().As<ICurrencyService>();
                builder.RegisterType<MainViewModel>();
                App.Container = builder.Build();
    
                // The root page of the application
                MainPage = new MainPage ();
            }
        }
    

    Regards,
    Mike.

Sign In or Register to comment.