gemini
gemini copied to clipboard
Slow startup in VS2017 possibly binding related
Created a new project in VS2017 community. got it to run, but its seems to hang on startup for a minute before continuing. I do not remember this in 2015.
Once its done spinning i get this output.
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
So, we can remove the warnings as follows:
namespace Gemini.Modules.MainMenu.Views
{
/// <summary>
/// Interaction logic for MainMenuView.xaml
/// </summary>
public partial class MainMenuView : UserControl
{
public MainMenuView()
{
InitializeComponent();
#if DEBUG
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;
#endif
}
}
}
Instead of just suppressing these warnings, has anyone managed to find te cause for this and fix it?
Found it. I think it's actually related to the control template of the default menu item, which expects to be in an itemscontrol.
In the Gemini project, navigate to Themes->VS2013->Controls->Menu.xaml.
All the way at the bottom you can find the default styles for both the MenuItem and the xcad:MenuItemEx. Simply modify them to force the alignment.
<!-- Set default styles -->
<Style BasedOn="{StaticResource MetroSeparator}" TargetType="Separator" />
<Style BasedOn="{StaticResource MetroMenuItem}" TargetType="MenuItem">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<Style BasedOn="{StaticResource StandardMenu}" TargetType="Menu" />
<Style BasedOn="{StaticResource MetroContextMenu}" TargetType="ContextMenu" />
<Style BasedOn="{StaticResource MetroContextMenu}" TargetType="xcad:ContextMenuEx" />
<Style BasedOn="{StaticResource MetroMenuItem}" TargetType="xcad:MenuItemEx">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
My guess is the performance slowdown is actually related to MEF. In particular, you might add timing information to AppBootstrapper.Configure
to check how long it takes to load MEF. For comparison, I assume Visual Studio caches MEF information to improve loading time, although occasionally you see the "Updating MEF Components" busy dialog during startup. Once Configure
and MEF is finished loading, then I think the UI starts and you would start seeing the binding warnings.
I'm using the latest Nuget and didn't want to branch the code just to fix this (still need to figure out the unnamed "Resource not found" warnings). Was able to figure out a fix for the MetroMenuItem errors by adding the code below to my App.xaml.
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gemini="http://schemas.timjones.io/gemini"
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<gemini:AppBootstrapper x:Key="bootstrapper" />
</ResourceDictionary>
<!-- Fix based on https://github.com/tgjones/gemini/issues/291#issuecomment-443011486
Basically hack in the fix to Geimini resources to inhibit WPF from spewing about 10 Error traces on startup:
System.Windows.Data Error: Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='http://System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='http://System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'MenuItemEx' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
-->
<ResourceDictionary Source="pack://application:,,,/Gemini;component/Themes/VS2013/Controls/Menu.xaml">
<Style BasedOn="{StaticResource MetroMenuItem}" TargetType="MenuItem" x:Key="MetroMenuItemFix">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<Style BasedOn="{StaticResource MetroMenuItem}" TargetType="xcad:MenuItemEx" x:Key="MetroMenuItemExFix">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>