benruehl / adonis-ui

Lightweight UI toolkit for WPF applications offering classic but enhanced windows visuals
https://benruehl.github.io/adonis-ui/
MIT License
1.71k stars 143 forks source link

Getting "Value cannot be null" error in App.xaml with ResourceDictionary #10

Closed rushdrive closed 5 years ago

rushdrive commented 5 years ago

Using .NET Framework 4.6.1 and I have installed AdonisUI and AdonisUI.ClassicTheme latest versions via NuGet and they are referenced correctly in the project.

App.xaml

<Application x:Class="ExampleApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:adonisUi="clr-namespace:AdonisUI;assembly=AdonisUI"
             xmlns:local="clr-namespace:ExampleApp"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
                <ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window x:Class="ExampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExampleApp"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800">
    <Window.Style>
        <Style TargetType="Window"
               BasedOn="{StaticResource {x:Type Window}}" />
    </Window.Style>
    <Grid>
        <StackPanel Margin="10">
            <Button Content="Click Me"
                    HorizontalAlignment="Center" />
        </StackPanel>
    </Grid>
</Window>

Issues:

<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.DarkColorScheme}" />
<ResourceDictionary Source="{x:Static adonisUi:ResourceLocator.ClassicTheme}" />

Both lines in App.xaml are getting the following error:

Value cannot be null. Parameter name: item.

I have tried multiple fresh projects, building and rebuilding and I keep getting this error. I am able to build the project and I can see the styles (DarkColorScheme) correctly applied on MainWindow. However the styles don't appear on the designer window, I'm not sure if it's related to the error I'm getting or not.

Any ideas what could cause this?

benruehl commented 5 years ago

If I understood you correctly, the error appears only at design time and does not lead to failed builds or runtime errors. I created a fresh project myself and can verify that the error shows up when I activate the XAML designer. I had it permanently disabled because in my opinion it brings more problems than it solves especially in large applications. I will try to find the reason for the error message though.

Instead of using the ResourceLocator syntax here, you can also replace the two lines with the following ones:

<ResourceDictionary Source="pack://application:,,,/AdonisUI;component/ColorSchemes/Dark.xaml" />
<ResourceDictionary Source="pack://application:,,,/AdonisUI.ClassicTheme;component/Resources.xaml" />

They have the completely identical effect. I just prefer the ResourceLocator syntax as it is more readable.

rushdrive commented 5 years ago

Adding the resources like you've suggested solves all the issues for me. Thank you!

And yeah, it does not cause any build/run-time errors. I use the Designer for visual feedback, and styles not applying there was confusing. This seems like a false positive from Visual Studio with a pretty bad error message.

If the error is indeed due to a VS bug, do you think the alternative syntax you suggested should be mentioned in the readme/docs?

benruehl commented 5 years ago

Glad it helped you.

Unfortunately, I cannot spot what produces the error. I mean, it is only a static property containing a Uri. Not much I can play around with. I agree that it might be better to mention this behavior in the readme or maybe replace the two lines with the working ones. Too bad the XAML designer failes here. I was glad I found a way to hide the pack syntax, but it's not much worth if it does not work correctly.

benruehl commented 5 years ago

Changed it in the readme and the demo application as I think there are many people using the XAML designer. Thanks for spotting and reporting it!

SuperBrain commented 4 years ago

This seems to be related to #25 and it's still present in VS2019 (16.4).