MVVM Light ViewModelLocator + ResourceDictionaries

时间:2011-05-27 18:14:18

标签: wpf mvvm-light resourcedictionary

我最初在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>    

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

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

1 个答案:

答案 0 :(得分: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>