【问题标题】:MVVM Light ViewModelLocator + ResourceDictionariesMVVM Light ViewModelLocator + ResourceDictionaries
【发布时间】:2023-10-24 05:35:01
【问题描述】:

我最初在 MVVM Light CodePlex 页面上发布了这条消息,但还没有收到回复,所以我希望这里有人可以帮助我。问题来了:

我最近开始使用 MVVM(也是 WPF 的新手——所有这一切的学习曲线都相当不错),在我开始使用 CodePlex 上提供的 MetroToolkit 之前,我的 ViewModelLocator 实例和 VS2010 的绑定设计时一切都很好。在使用该工具包之前,我有以下内容:

<Application.Resources>
    <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>

我所有的观点都很好,一切看起来都很好。我很惊讶没有 MVVM(或 MVC)经验的人可以轻松启动和运行。然后我遇到了需要合并资源字典的 MetroToolkit 的障碍,现在无论我尝试什么,我都无法让 VS 在 App.xaml 中再次找到我的 ViewModelLocator。这是新的标记:

<Application.Resources>

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Colors.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Animations.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Geometry.xaml"/>
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Generic.xaml"/>
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Buttons.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Scrollbar.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Scrollviewer.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/RadioButton.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/ProgressBar.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/ContextMenu.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Tooltip.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Checkbox.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Headings.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Textbox.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Combobox.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Slider.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Expander.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/TabControl.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

        </ResourceDictionary>
    </Application.Resources>    

我尝试为资源字典提供一个键,在该区域之外添加行(上方和下方 - 引发讨厌且非常无用的错误)并且无法找到我的 VM 定位器。当我从 App.xaml 中删除该块时,它会立即起作用,但基于我对 WPF 的非常有限的了解,如果我希望这些样式可用于我的应用程序中的所有视图,我需要这些。

有什么想法吗?这已经让我发疯了几个小时了。

【问题讨论】:

    标签: wpf mvvm-light resourcedictionary


    【解决方案1】:

    是的...我前几天才看到这个...你必须在里面放一个资源字典...

        <ResourceDictionary>
    
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Colors.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Animations.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Geometry.xaml"/>
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Generic.xaml"/>
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Buttons.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Scrollbar.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Scrollviewer.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/RadioButton.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/ProgressBar.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/ContextMenu.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Tooltip.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Checkbox.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Headings.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Textbox.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Combobox.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Slider.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Expander.xaml" />
                        <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/TabControl.xaml" />
     <ResourceDictionary>
                         <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
                    </ResourceDictionary>
                    </ResourceDictionary.MergedDictionaries>
    
            </ResourceDictionary>
    

    **编辑 抱歉……现在修好了……我是从记忆中走出来的……这是我的样子。

    <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Dictionaries/converters.xaml" />
                    <ResourceDictionary Source="assets/styles.xaml" />
                    <ResourceDictionary Source="assets/sdkstyles.xaml" />
                    <ResourceDictionary Source="assets/corestyles.xaml" />
                    <ResourceDictionary>
                        <local:ApplicationResources x:Key="ApplicationResources" />
                    </ResourceDictionary>
    
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    

    【讨论】:

    • 我试过了,但没有用。它给了我一个“添加到 IDictionary 的所有对象都必须具有 Key 属性或与之关联的其他类型的键”错误。当我给它一个键时它停止工作 - 数据上下文只是空,或者它抛出一个错误,它找不到任何称为“定位器”的东西。这是我添加它的方法:
    • 我尝试将我的数据上下文设置为“VMLocator.Locator”,认为它是分层的,但也没有运气。