Bug 27299 - Setting BindingContext using xaml bindings is called multiple times.
Summary: Setting BindingContext using xaml bindings is called multiple times.
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-02-23 02:38 UTC by Josue Yeray
Modified: 2015-06-11 16:45 UTC (History)
5 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:


Attachments
Repro project (6.91 MB, application/octet-stream)
2015-02-23 02:38 UTC, Josue Yeray
Details
Repro project (1.4.2) (1006.11 KB, application/zip)
2015-05-19 08:37 UTC, Andy Hopper
Details
Repro project (1.4.2) - use this one, not the previous (1.04 MB, application/x-gzip)
2015-05-19 08:53 UTC, Andy Hopper
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Josue Yeray 2015-02-23 02:38:52 UTC
Created attachment 9973 [details]
Repro project

I try to set up the Binding context of my view using a binding expression in XAML.

I created a ViewModel Locator class that exposes my ViewModel as a property:

public class ViewModelLocator
{
    IContainer container;

    public ViewModelLocator()
    {
        ContainerBuilder builder = new ContainerBuilder();

        builder.RegisterType<MainViewModel>();

        this.container = builder.Build();
    }

    public MainViewModel MainVM
    {
        get { return this.container.Resolve<MainViewModel>(); }
    }
}

Then, in app.cs i added this class as a resource in the current app resource dictionary:

public App()
{
    // The root page of your application
    Current.Resources = new ResourceDictionary();
    var locator = new ViewModelLocator();
    Current.Resources.Add("Locator", locator);

    MainPage = new MainView();
}

Finally, in my MainView.xaml i declared the BindingContext with a binding expression:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Converters.View.MainView"
             BindingContext="{Binding MainVM, Source={StaticResource Locator}}">

When running the app, if you put a breakpoint in ViewModelLocator MainVM property get or MainViewModel constructor, you can see it is called constantly and the app didn't pass that point.

If you set the BindingContext in MainView.xaml code behind this way:

public MainView()
{
    InitializeComponent();

    var locator = (ViewModelLocator)App.Current.Resources["Locator"];
    BindingContext = locator.MainVM;
}

It works perfectly, so i think is something related to the binding resolution in xaml what makes the app to query constantly the MainVM property.
Comment 1 Andy Hopper 2015-05-19 08:37:38 UTC
Created attachment 11241 [details]
Repro project (1.4.2)

Ignore the name of the solution/project; I encountered this on the way to generating a repro for a different issue. Put a breakpoint on ViewModelLocator.cs line 14.
Comment 2 Andy Hopper 2015-05-19 08:53:30 UTC
Created attachment 11243 [details]
Repro project (1.4.2) - use this one, not the previous

Reproed on 1.4.2 (ignore the earlier attachment; that was on 1.3.3 and I hadn't updated yet)
Comment 3 Jason Smith [MSFT] 2015-06-11 16:45:20 UTC
Should be fixed in 1.4.3 final