ViewLocator GetOrCreateViewType inconsistency in performing InitializeComponent for Xamarin.Forms #326

Closed
Yakyyaky opened this Issue May 15, 2016 · 1 comment

Projects

None yet

2 participants

@Yakyyaky

The issue is the delegate 'public static Func<Type, UIElement> GetOrCreateViewType' in ViewLocator.cs
The two implementation is inconsistent when using IoC to obtain the view versus using Activator to instantiate a view.

if (view != null) {
    InitializeComponent(view);
    return view;
}

versus

view = (UIElement)System.Activator.CreateInstance(viewType);
#if !XFORMS
            InitializeComponent(view);
#endif
            return view;

As you can see form the above InitializeComponent is called when using IoC that actually registers their views. Now this is usually not so much of a problem in platforms such as WPF, UWP as they actually guard re entrant calls. However Xamarin.Form's 'InitializeComponent()' does not perform this guard. As a result, an example that stems from this side effect is doublly MenuItem when declared from their xaml files. As the default template calls in in the constructor. As a workaround, I've removed all the constructor of view.

I personally don't mind if you add or remove the preprocessor to make them consistent as long as they're consistent. From a Pit to success point of view I'd say add. :)
If there's reason as to why they are currently correct the way they are right now. I humbly apologise.

@nigel-sampson
Contributor

Whoops, thanks for finding the inconsistency.

@nigel-sampson nigel-sampson added the bug label May 15, 2016
@nigel-sampson nigel-sampson modified the milestone: v4.0.0, v3.0.2 May 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment