首页 文章

App.xaml找不到命名空间IocContainter MVVM

提问于
浏览
0

我在MVVM模式下制作了一个Windows 10通用应用程序 . 我将此代码放入App.xaml文件中:

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

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

</Application>

但它在粗线上给我这个错误:

命名空间中名称IocContainer不存在:WishLister.Services .

我也尝试在斜体代码中使用 clr-namespace:WishLister.Services ,但我有两个错误:

命名空间clr-namespace中不存在名称IocContainer:WishLister.Services . XML命名空间中的未知类型IocContainer clr-namespace:WishLister.Services; assembly = WishLister,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null

但我已经上了一堂课 WishLister.Services.IocContainer . 这是代码:

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

这段代码有什么问题?

3 回答

  • -1

    尝试:

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

    我是通过@Will的评论找到的 . 他或她说:

    删除<services:IocContainer x:Key =“ioc”/>以及引用服务xmlns的任何内容 . 构建您的解决方案修复任何阻止这种情况然后清理,重新启动Visual Studio,然后重建 . 如果一切正常,请尝试再次将您的IocContainer添加到xaml中 . 此外,如果IocContainer未在与WishLister.App相同的程序集中定义,则您需要执行其他一些操作 .

  • 1

    尝试清理并重建您的项目 .

相关问题