Merged dictionaries and local resources

依然范特西╮ 提交于 2019-12-01 00:14:22

问题


I have a Styles.xaml that groups many ResourceDictionarys inside a MergedDictionary.

I imported Styles.xaml in my UserControl.Resources

<UserControl.Resources>
    <ResourceDictionary Source="Dictionaries\Styles.xaml" />
</UserControl.Resources>

but when I try to add a converter

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionaries\Styles.xaml" /> <--! Exception -->
        </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

it raises

ArgumentNullException: Value cannot be null.
Parameter name: item

Wrapping the converter inside another MergedDictionary has no effect.
How can I solve this?
Thank you all!


SOLVED

I eventually figured it out: the Exception was raised inside one the .xaml files, but Visual Studio does not provide enough info to locate the faulty line.
Following code does work.

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionaries\Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

回答1:


Try this

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/PROJECTNAMESPACE (TestProject.Something);component/Dictionaries/Styles.xaml" /> 
        </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</UserControl.Resources>


来源:https://stackoverflow.com/questions/18625928/merged-dictionaries-and-local-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!