Skip to content
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

"System.Windows.ResourceDictionary Warning: 9 : Resource not found" when changing themes #29

Closed
Coke21 opened this issue Jan 30, 2020 · 3 comments

Comments

@Coke21
Copy link

Coke21 commented Jan 30, 2020

Hi, I'm using 0.8 ModernWpf and 0.8 MahApps and for some reason every time I change themes I get spammed "System.Windows.ResourceDictionary Warning: 9 : Resource not found".
This is my View:

                    <ui:RadioButtons SelectedIndex="{Binding SelectedThemeIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        <ui:RadioButtons.Header>
                            <StackPanel Orientation="Horizontal">
                                <iconPacks:PackIconMaterial Kind="ThemeLightDark" VerticalAlignment="Center"/>
                                <Label Content="Theme Mode" Margin="2 0 0 0"/>
                            </StackPanel>
                        </ui:RadioButtons.Header>

                        <system:String>Light</system:String>
                        <system:String>Dark</system:String>
                    </ui:RadioButtons>

ViewModel:

        private int _selectedThemeIndex;
        public int SelectedThemeIndex
        {
            get { return _selectedThemeIndex; }
            set
            {
                _selectedThemeIndex = value;

                ThemeManager.Current.ApplicationTheme = SelectedThemeIndex switch
                {
                    0 => ApplicationTheme.Light,
                    1 => ApplicationTheme.Dark
                };

                NotifyOfPropertyChange(() => SelectedThemeIndex);
            }
        }

App.xaml:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="Bootstrapper"/>
                </ResourceDictionary>

                <ui:ThemeResources />
                <ui:XamlControlsResources />

                <ui:ThemeResources>
                    <ui:ThemeResources.ThemeDictionaries>
                        <ResourceDictionary x:Key="Light">
                            <ResourceDictionary.MergedDictionaries>
                                <ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
                            </ResourceDictionary.MergedDictionaries>
                        </ResourceDictionary>
                        <ResourceDictionary x:Key="Dark">
                            <ResourceDictionary.MergedDictionaries>
                                <ResourceDictionary Source="/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
                            </ResourceDictionary.MergedDictionaries>
                        </ResourceDictionary>
                    </ui:ThemeResources.ThemeDictionaries>
                </ui:ThemeResources>

                <ui:MahAppsColorPaletteResources />
                <ResourceDictionary Source="/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="/MahApps.Metro;component/Styles/Fonts.xaml" />

                <ui:XamlControlsResources />
                <ResourceDictionary Source="/ModernWpf.MahApps;component/Styles/Controls.xaml" />

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Application.Resources>

When I change themes my app freezes for like 2 seconds and then I get spammed with the above warning and the theme changes. I don't know what's wrong here.

@Kinnara
Copy link
Owner

Kinnara commented Jan 31, 2020

Thanks for the report. There's nothing wrong with your code. The warnings are caused by code changes in 0.8, and will be fixed in the next version. While the warnings make debugging slower, the app should run fine.

@Coke21
Copy link
Author

Coke21 commented Jan 31, 2020

Thank you for quick help. I have one more question. I'm trying to use your ui:NumberBox instead of MahApps' mah:NumericUpDown The second control had TextAlignment property which was really useful, can I somehow align text with your control?

@Kinnara
Copy link
Owner

Kinnara commented Feb 1, 2020

The TextAlignment property will eventually be added according to the proposal. Before that happens, you can use the following:

public class NumberBoxEx : NumberBox
{
    public static readonly DependencyProperty TextAlignmentProperty = Block.TextAlignmentProperty.AddOwner(typeof(NumberBoxEx));

    public TextAlignment TextAlignment
    {
        get => (TextAlignment)GetValue(TextAlignmentProperty);
        set => SetValue(TextAlignmentProperty, value);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        if (GetTemplateChild("InputBox") is TextBox textBox)
        {
            textBox.SetBinding(TextBox.TextAlignmentProperty,
                new Binding
                {
                    Path = new PropertyPath(TextAlignmentProperty),
                    RelativeSource = RelativeSource.TemplatedParent
                });
        }
    }
}

@Kinnara Kinnara closed this as completed Feb 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants