将 App.xaml 设置为 UserControl WPF(以连接定位器)

Set App.xaml to a UserControl WPF (to connect Locator)

情况:

我目前正在尝试将我的 DataContext 连接到我的 ViewModel。我正在使用 GalaSoftMvvmLight.

但事实是我没有 Window,因为我会将此代码集成到另一个程序中,该程序具有 Window。所以我只有 UserControl.

问题:

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

我收到这个错误 {"Cannot find resource named 'Locator'. Resource names are case sensitive."}

问题:

如何将我的 App.xaml 资源正确连接到我的视图?如果没有 Window 就不可能,我怎么能用这样的东西调用 DataContext

<UserControl.DataContext>
    SOMETHING TO SET DATACONTEXT WITH BINDING !
</UserControl.DataContext>

这是我的代码:

App.xaml

<Application x:Class="SOMETHING.App" xmlns:vm="clr-namespace:SOMETHING.ViewModel" StartupUri="ApplicationView.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

ApplicationView.xaml

<UserControl x:Class="SOMETHING.View.ApplicationView"
         <!-- THIS ONE DOESN'T WORK -->
         DataContext="{Binding ApplicationVM, Source={StaticResource Locator}}">

    <!-- THIS ONE DOESN'T WORK IF I SET <vm:ViewModelLocator x:Key="Locator" /> IN STYLE.XAML, BUT I CAN'T USE IT IN USERCONTROL PARAMETERS (LIKE ABOVE) -->
    <UserControl.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries >
                <ResourceDictionary Source="Style.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
         <Label Content="{Binding Title}" />
    </Grid>
</UserControl>

ViewModelLocator

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>();
    }
}

假设 ViewModelLocator class 和 UserControl 位于同一个 project/assembly 中,您可以在 [=15] 中定义 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:

<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>