H. Pauwelyn
H. Pauwelyn

Reputation: 14310

App.xaml can not found namespace IocContainter MVVM

I'm making a windows 10 universal application whit the MVVM patern. I'm placing this code into the App.xaml file:

<Application
    x:Class="WishLister.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WishLister"
    xmlns:services="using:WishLister.Services"
    RequestedTheme="Light">

    <Application.Resources>
        
        <ResourceDictionary>        
            <services:IocContainer x:Key="ioc" /> <!--> error happens here -->

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Templates/Rescources.xaml" x:Name="recources"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
</Application>

But it give me this error on the marked line:

The name IocContainer does not exist in the namespace using:WishLister.Services.

I've also try to use clr-namespace:WishLister.Services in stad of the italic code but I've got these two errors:

The name IocContainer does not exist in the namespace clr-namespace:WishLister.Services.

Unknown type IocContainer in XML namespace clr-namespace:WishLister.Services;assembly=WishLister, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

But I have made a class WishLister.Services.IocContainer. Here is the code:

using GalaSoft.MvvmLight.Ioc;
using WishLister.ViewModels;

namespace WishLister.Services
{
    public class IocContainer
    {
        public IocContainer Ioc
        {
            get
            {
                return App.Current.Resources["ioc"] as IocContainer;
            }
        }

        public MainPageViewModel MainPageViewModel
        {
            get
            {
                return SimpleIoc.Default.GetInstance<MainPageViewModel>();
            }
        }

        public IocContainer()
        {
            SimpleIoc.Default.Register<MainPageViewModel>(false);
        }
    }
}

What's the problem whit this code?

Upvotes: 2

Views: 592

Answers (3)

Linh Nguyen
Linh Nguyen

Reputation: 51

Try Clean and Rebuild your project.

Upvotes: 0

H. Pauwelyn
H. Pauwelyn

Reputation: 14310

I've found it by a comment of @Will. He or she sayed:

  1. Remove <services:IocContainer x:Key="ioc" /> and anything that references the services xmlns.
  2. Build your solution.
  3. Fix anything preventing this.
  4. Then clean, restart Visual Studio, and rebuild.
  5. If all is well, try to add your IocContainer into the xaml again.

Also, if IocContainer isn't defined in the same assembly as WishLister.App you'll need to do some other stuff.

Upvotes: 3

Pikoh
Pikoh

Reputation: 7713

Try:

xmlns:services="clr-namespace:WishLister.Services"         

Upvotes: -1

Related Questions