New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException When using Flyout With Theme #3109

Closed
feinstein opened this Issue Nov 21, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@feinstein
Contributor

feinstein commented Nov 21, 2017

I had MahApps.Metro version 1.4.1 and I just upgraded to version 1.5.0 I made a clean rebuild after the update and now my Flyouts are crashing.

Before, on version 1.4.1 everything was fine, but right now on version 1.5.0 if I define the Flyout with the Theme="Accent" I get a NullReferenceException with the following stack trace:

at MahApps.Metro.Controls.Flyout.OverrideFlyoutResources(ResourceDictionary resources, Boolean accent) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Flyout.cs:line 389
at MahApps.Metro.Controls.Flyout.ChangeFlyoutTheme(Accent windowAccent, AppTheme windowTheme) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Flyout.cs:line 329
at MahApps.Metro.Controls.Flyout.UpdateFlyoutTheme() in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Flyout.cs:line 308
at MahApps.Metro.Controls.Flyout.ThemeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Flyout.cs:line 619
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.Baml2006.WpfMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue(Object obj, XamlMember property, Object value)
at System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent)
at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteEndMember()
at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at _3M.Desktop.Views.MainWindow.InitializeComponent() in C:\MyProjects\MyApp\MyApp.Desktop\Views\MainWindow.xaml:line 1
at _3M.Desktop.Views.MainWindow..ctor() in C:\MyProjects\MyApp\MyApp.Desktop\Views\MainWindow.xaml.cs:line 13

Removing the Theme attribute, or setting any other option that's not Accent, makes the issue disappear.

I reverted back to version 1.4.1 and the problem vanished, as expected. I also tested with version 1.4.3 and it's working, which implies the bug was added during version 1.5.0 release.

Environment

  • MahApps.Metro v1.5.0
  • Windows OS 10
  • Visual Studio 2015
  • .NET Framework 4.5
@punker76

This comment has been minimized.

Member

punker76 commented Nov 21, 2017

@feinstein Do you have a custom accent resource dictionary? It seems that the AccentBaseColor could not be find. I will fix and catch this in v1.6.0 (maybe also as hofix for v1.5.0).

@punker76 punker76 self-assigned this Nov 21, 2017

@punker76 punker76 added the Bug label Nov 21, 2017

@feinstein

This comment has been minimized.

Contributor

feinstein commented Nov 21, 2017

Yes, I do have a custom accent. It's defined as:

protected override void OnStartup(StartupEventArgs e)
{
	// add custom accent and theme resource dictionaries
	ThemeManager.AddAccent("NavyBlueAccent", new Uri("pack://application:,,,/3M;component/Resources/NavyBlueAccent.xaml"));
																			
	// get the theme from the current application
	var theme = ThemeManager.DetectAppStyle(Application.Current);

	// now use the custom accent
	ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.GetAccent("NavyBlueAccent"), theme.Item1);

	base.OnStartup(e);
}

So pretty much a copy-paste from the docs.

The resource itself is:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                mc:Ignorable="options">

<!--ACCENT COLORS-->
<Color x:Key="HighlightColor">#FF002B58</Color>

<!--80%-->
<Color x:Key="AccentColor">#FF064B92</Color>
<!--60%-->
<Color x:Key="AccentColor2">#99119EDA</Color>
<!--40%-->
<Color x:Key="AccentColor3">#66119EDA</Color>
<!--20%-->
<Color x:Key="AccentColor4">#33119EDA</Color>

<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush4" Color="{StaticResource AccentColor4}" options:Freeze="True" />

<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />

<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5" options:Freeze="True">
    <GradientStop Color="{StaticResource HighlightColor}" Offset="0" />
    <GradientStop Color="{StaticResource AccentColor3}" Offset="1" />
</LinearGradientBrush>

<SolidColorBrush x:Key="CheckmarkFill" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="RightArrowFill" Color="{StaticResource AccentColor}" options:Freeze="True" />

<Color x:Key="IdealForegroundColor">White</Color>
<SolidColorBrush x:Key="IdealForegroundColorBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
<SolidColorBrush x:Key="IdealForegroundDisabledBrush" Color="{StaticResource IdealForegroundColor}" Opacity="0.4" options:Freeze="True" />
<SolidColorBrush x:Key="AccentSelectedColorBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

<!-- DataGrid brushes -->
<SolidColorBrush x:Key="MetroDataGrid.HighlightBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.HighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.MouseOverHighlightBrush" Color="{StaticResource AccentColor3}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.FocusBorderBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightBrush" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

</ResourceDictionary>

@punker76 punker76 added this to the 1.5.1 milestone Nov 21, 2017

@punker76 punker76 modified the milestones: 1.5.1, 1.6.0 Dec 9, 2017

punker76 added a commit that referenced this issue Dec 9, 2017

(#3109) Fix NullReferenceException when using Flyout with theme
  The `AccentBaseColor` color could not be found which was introduced with 1.3.0 (which was a breaking change, if users has their own accents)

punker76 added a commit that referenced this issue Dec 9, 2017

Merge pull request #3120 from MahApps/feature/3109_NullReferenceExcep…
…tion_Flyout_AccenBaseColor

(#3109) Fix NullReferenceException when using Flyout with theme

punker76 added a commit that referenced this issue Dec 13, 2017

(#3109) Fix NullReferenceException when using Flyout with theme
  The `AccentBaseColor` color could not be found which was introduced with 1.3.0 (which was a breaking change, if users has their own accents)

punker76 added a commit that referenced this issue Dec 13, 2017

Merge pull request #3120 from MahApps/feature/3109_NullReferenceExcep…
…tion_Flyout_AccenBaseColor

(#3109) Fix NullReferenceException when using Flyout with theme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment