C# 将App.xaml设置为UserControl WPF(用于连接定位器) 情况:

C# 将App.xaml设置为UserControl WPF(用于连接定位器) 情况:,c#,wpf,xaml,mvvm,viewmodellocator,C#,Wpf,Xaml,Mvvm,Viewmodellocator,我当前正在尝试将我的DataContext连接到我的ViewModel。我用的是GalasoftVMLight 但事实上,我没有窗口,因为我将把这些代码集成到另一个有窗口的程序中。所以我只有用户控制 问题: 我不知道为什么,但我无法在UserControl中连接我的DataContext 我收到此错误{找不到名为“Locator”的资源。资源名称区分大小写。} 问题: 如何将App.xaml资源正确连接到我的视图?如果没有窗口是不可能的,我怎么能用这样的东西调用DataContext呢 <

我当前正在尝试将我的DataContext连接到我的ViewModel。我用的是GalasoftVMLight

但事实上,我没有窗口,因为我将把这些代码集成到另一个有窗口的程序中。所以我只有用户控制

问题: 我不知道为什么,但我无法在UserControl中连接我的DataContext

我收到此错误{找不到名为“Locator”的资源。资源名称区分大小写。}

问题: 如何将App.xaml资源正确连接到我的视图?如果没有窗口是不可能的,我怎么能用这样的东西调用DataContext呢

<UserControl.DataContext>
    SOMETHING TO SET DATACONTEXT WITH BINDING !
</UserControl.DataContext>
这是我的密码: App.xaml ApplicationView.xaml 视图模型定位器 如果ViewModelLocator类和UserControl位于同一项目/程序集中,则可以在ResourceDictionary中定义ViewModelLocator资源,并将其合并到UserControl中,如下所示:

<UserControl ...>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Global.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Path="ApplicationVM" Source="{StaticResource Locator}" />
    </UserControl.DataContext>
    <Grid>

    </Grid>
</UserControl>
Global.xaml:

如果ViewModelLocator类和UserControl位于同一项目/程序集中,则可以在ResourceDictionary中定义ViewModelLocator资源,并将其合并到UserControl中,如下所示:

<UserControl ...>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Global.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Path="ApplicationVM" Source="{StaticResource Locator}" />
    </UserControl.DataContext>
    <Grid>

    </Grid>
</UserControl>
Global.xaml:


ViewModelLocator类在哪里定义?如果UserControl是在一个独立的类库中定义的,那么它当然不能引用ViewModelLocator资源,如果它不是在这个库或UC库引用的库中定义的。因此,是的,它是一个独立的UserControl,没有对VML的引用,但是它们都在同一个项目中!ViewModelLocator和UserControl位于同一个项目中?是的,它们位于同一个项目中。我在一个只能使用UserControl的项目上工作,但我想用MVVM进行编码。因此,我需要将UC连接到VML。ViewModelLocator类在哪里定义?如果UserControl是在一个独立的类库中定义的,那么它当然不能引用ViewModelLocator资源,如果它不是在这个库或UC库引用的库中定义的。因此,是的,它是一个独立的UserControl,没有对VML的引用,但是它们都在同一个项目中!ViewModelLocator和UserControl位于同一个项目中?是的,它们位于同一个项目中。我在一个只能使用UserControl的项目上工作,但我想用MVVM进行编码。所以我需要将UC连接到VMLI,我正在寻找这个:!!非常感谢,花了2天的时间^^“如果资源Global.xaml位于用户控件的根文件夹中,并且用户控件视图位于诸如i.e.\GUI\Views之类的文件夹中,是否存在问题?我正在查找此信息:!!非常感谢,花了2天时间^^“如果资源Global.xaml位于用户控件的根文件夹中,并且用户控件视图位于类似于for i.e.\GUI\Views的文件夹中,是否存在问题?
public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else
        {
            SimpleIoc.Default.Register<IDataService, DataService>();
        }

        SimpleIoc.Default.Register<ApplicationViewModel>();
    }

    public ApplicationViewModel ApplicationVM
    {
        get { return SimpleIoc.Default.GetInstance<ApplicationViewModel>();
    }
}
<UserControl ...>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Global.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Path="ApplicationVM" Source="{StaticResource Locator}" />
    </UserControl.DataContext>
    <Grid>

    </Grid>
</UserControl>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfControlLibrary1">
    <local:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>