Bug 45557 - Setting BindingContext using xaml bindings is called multiple times (Same as Bug 27299)
Summary: Setting BindingContext using xaml bindings is called multiple times (Same as ...
Status: RESOLVED DUPLICATE of bug 57081
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.2
Hardware: Other Other
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-10-17 12:02 UTC by Andi Schabus
Modified: 2017-06-14 11:31 UTC (History)
4 users (show)

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

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 DUPLICATE of bug 57081

Description Andi Schabus 2016-10-17 12:02:51 UTC
I try to create a Xamarin.Forms app for Android, where the view as well as the viewModel are within a PCL. 
When I try to Bind the context using a binding expression in XAML, it get's called multiple times (actually 3 times).
The description and a workaround is exactly the same as in bug 27299 - where it is marked as solved.
However, I still see the issue in version 2.3.2
Comment 1 Samantha Houts [MSFT] 2016-10-17 18:34:56 UTC
Thank you for taking the time to submit the bug. We tried to reproduce the issue you reported but were unable given the description. If you could please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue, we would very much appreciate it.  
 
For your convenience, we have created some reproduction best practices viewable here: https://gist.github.com/jassmith/92405c300e54a01dcc6d  

Warm regards, 
Xamarin Forms Team
Comment 2 Andi Schabus 2016-10-17 19:33:03 UTC
This is the shortest repo I came up with:

- In Visual Studio 2015 (latest updates) create "Blank Xaml App (Xamarin.Forms Portable)" - accept all defaults

- Delete the UWP & iOS Project

- Add the following class to the PCL project:
   public class ViewModelLocator {
      public ViewModelLocator() {
      }

      public object MainVM {
         get { return null; }      // put a breakpoint here
      }
   }

- Change the App.Xaml file in the PCL project to:
   <?xml version="1.0" encoding="utf-8" ?>
   <Application xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             	xmlns:local="clr-namespace:App1;assembly=App1"
                x:Class="App1.App">
      <Application.Resources>
         <ResourceDictionary>
            <local:ViewModelLocator
	      	   x:Key="ViewModelLocator" />
         </ResourceDictionary>
      </Application.Resources>
   </Application>

- Change the MainPage.xaml in the PCL project to:
   <?xml version="1.0" encoding="utf-8" ?>
   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                BindingContext="{Binding MainVM, Source={StaticResource  ViewModelLocator}}"
             x:Class="App1.MainPage">

   <Label Text="Welcome to Xamarin Forms!"
           VerticalOptions="Center"
           HorizontalOptions="Center" />

</ContentPage>

- Set a breakpoint in ViewModelLocator::MainVM::get

- Run the app in the android emulator.

- The Breakpoint is hit twice, but it should only be hit once.

I hope this helps?
regards,
Andi
Comment 3 Samantha Houts [MSFT] 2016-10-18 16:32:44 UTC
Thank you!
Comment 4 adrianknight89 2016-10-19 06:21:11 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/470
Comment 5 Stephane Delcroix 2017-06-14 11:31:43 UTC

*** This bug has been marked as a duplicate of bug 57081 ***