MVVM Light ViewModelLocator + ResourceDictionaries MVVM Light ViewModelLocator和ResourceDictionaries结合使用。

7
我最初在MVVM Light CodePlex页面上发布了这条信息,但还没有收到回复,所以我希望这里的某个人能帮助我。以下是问题:
我最近开始尝试使用MVVM(也是WPF的新手-对于所有这些内容来说,学习曲线相当陡峭),我的ViewModelLocator实例和绑定设计时间与VS2010一起工作得很好,直到我开始使用CodePlex上提供的MetroToolkit之前。在利用工具包之前,我有以下内容:
<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>    

我尝试为资源字典添加一个键,将该行代码放在<Application.Resources>区域之外(上方和下方 - 会抛出令人讨厌且无用的错误),但无法找到我的VM Locator。如果我从App.xaml中删除<ResourceDictionary>块,它会立即起作用,但基于我对WPF的非常有限的了解,如果我希望样式对应用程序中的所有视图都可用,就需要这些。

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

1个回答

14

没错...我前些天也看到了这个问题...你需要在里面放置一个资源字典...

    <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属性或其他类型的关键字”错误。当我给它一个关键字时,它就停止工作 - 要么数据上下文只是null,要么它会抛出一个找不到名为“Locator”的东西的错误。这是我添加它的方式: - Scott Salyer
我尝试将我的数据上下文设置为“VMLocator.Locator”,以为它是分层的,但也没有成功。 - Scott Salyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接