"Resource with name {Locator} not found" Error using mvvm-light user control

I am using the mvvm light toolkit to build a WPF application. I created a user control and the corresponding ViewModel. I created the ViewModel property in ViewModelLocator. I have bound datacontext user controls to a property in the Locator class. When I edit User Control in Blend or VS Designer, everything seems to work, since I can see the development time data.

When I try to use my user control in the main window, which is created by the wpf toolkit template, I get the error "Resource with name {Locator} could not be found", and the line with my user control in mainwindow.xaml is marked with a red line in Blend. In Visual Studio, the same line is marked with the error: "Unable to create an instance of type MyView."

Edit: This is the source code for app.xaml

<Application.Resources>
    <!--Global View Model Locator-->
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True">
</Application.Resources>

This is the code of EditCustomerView.xaml

<UserControl.DataContext>
    <Binding Path="EditCustomer" Source="{StaticResource Locator}" />
</UserControl.DataContext>

This is the code in the main window

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Window.DataContext>
    <Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>

<Grid x:Name="LayoutRoot" Background="{DynamicResource BasicBackground}">
    <Grid.RowDefinitions>
        <RowDefinition Height="0.927*"/>
        <RowDefinition Height="0.073*"/>
    </Grid.RowDefinitions>
    <ListBox Margin="4" SelectedItem="{Binding Main.SelectedCustomer, Mode=Default, Source={StaticResource Locator}}" ItemTemplate="{DynamicResource CustomerTemplate}" ItemsSource="{Binding Customers, Mode=Default}"/>
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Content="Edit" Grid.Row="1" Command="{Binding EditCustomerCommand, Mode=Default}"/>
    <Border x:Name="border" Opacity="0.75" Grid.RowSpan="2" Background="#FF706F6F" BorderBrush="Black" BorderThickness="1" Visibility="{Binding EditViewVisibility, Mode=Default}">
        <views:EditCustomerView HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Border>
</Grid>

The application compiles and runs. The error only occurs during development.

Can you tell me what I'm doing wrong?

Thanks in advance.

+3
source share
4 answers

. Blend - .

ViewModelLocator .

<Window.Resources>
    <vm:ViewModelLocator x:Key="Locator" 
                         d:IsDataSource="True"> 
</Window.Resources>

ViewModel.

​​

fooobar.com/questions/317869/...

, Blend 4

+3

, , , Blend 4:

XAML UserControl , Blend:

public partial class OrdersControl : UserControl
{
    public OrdersControl()
    {
        //  MUST do this BEFORE InitializeComponent()
        if (DesignerProperties.GetIsInDesignMode(this))
        {
             if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
            {
                // load styles resources
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute);
                Resources.MergedDictionaries.Add(rd);

                // load any other resources this control needs such as Converters
                Resources.Add("booleanNOTConverter", new BooleanNOTConverter());
            }
        }

        // initialize component
        this.InitializeComponent();

}

, , , . , , , , .

App.xaml.cs:

    internal static void CreateStaticResourcesForDesigner(Control element)
    {
        if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
        {
            // load styles resources
            ResourceDictionary rd = new ResourceDictionary();
            rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute);
            element.Resources.MergedDictionaries.Add(rd);

            // load any other resources this control needs
            element.Resources.Add("booleanNOTConverter", new BooleanNOTConverter());
        }
    }

Component():

     // create local resources
     if (DesignerProperties.GetIsInDesignMode(this))
     {
         App.CreateStaticResourcesForDesigner(this);
     }
+3

, , .

, . , App.xaml

<Application.Resources>
    <local:NameOfMyViewModelLocatorClass x:Key="Locator" />
</Application.Resources>

.

0
source

Well, to remove the problem even during development, you need to include the following condition in the ViewModelLocator constructor:

if (ViewModelBase.IsInDesignModeStatic)
{
}
else
{
    //Include function to create ViewModel here like the following
    CreateMain();
}

Hope this solves the problem.

-1
source

Source: https://habr.com/ru/post/1756177/


All Articles