【问题标题】:App.xaml can not found namespace IocContainter MVVMApp.xaml 找不到命名空间 IocContainter MVVM
【发布时间】:2015-12-09 14:53:12
【问题描述】:

我正在使用 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" /> <!--> error happens here -->

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

但它在标记线上给了我这个错误:

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

我也尝试在斜体代码的 stad 中使用 clr-namespace:WishLister.Services,但出现以下两个错误:

名称IocContainer 不存在于命名空间clr-namespace:WishLister.Services

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

但是我已经做了一个课程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);
        }
    }
}

这段代码有什么问题?

【问题讨论】:

  • 删除 &lt;services:IocContainer x:Key="ioc" /&gt; 和任何引用 services xmlns 的内容。构建您的解决方案。修复任何阻止这种情况的东西。然后清理,重新启动VS,并重建。如果一切正常,请尝试再次将您的 IocContainer 添加到 xaml 中。此外,如果 IocContainer 未在与 WishLister.App 相同的程序集中定义,则您需要做一些其他事情。

标签: c# xaml mvvm win-universal-app


【解决方案1】:

我通过@Will 的评论找到了它。他或她说:

  1. 删除 &lt;services:IocContainer x:Key="ioc" /&gt; 和任何引用服务 xmlns 的内容。
  2. 构建您的解决方案。
  3. 解决任何阻止此问题的问题。
  4. 然后清理,重新启动 Visual Studio,然后重新构建。
  5. 如果一切正常,请尝试再次将您的 IocContainer 添加到 xaml 中。

此外,如果 IocContainer 未在与 WishLister.App 相同的程序集中定义,则您需要做一些其他的事情。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • (这篇文章似乎没有为问题提供quality answer。请编辑您的答案,或者将其作为对问题的评论发布)。
    • 我不能写评论,因为我没有足够的 50 个声望。
    【解决方案3】:

    试试:

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

    【讨论】:

    • 很抱歉也没有用。请参阅我的问题以了解更多详细信息。我已经更新了。
    • 我已经复制了你的代码,并且使用“clr-namespace:WithLister.Services”编译没有问题……也许你的服务项目因为错误而没有编译?顺便说一句..你应该改变你的 xmlns:local="using:WishLister" 为 xmlns:local="clr-namespace:WishLister"
    • 另一件事..如果服务在您解决方案的另一个项目中,您当然应该导入它
    • 我不明白这不能提供答案。是因为太短了,还是因为没有说明为什么要改这个有问题的代码?我不是刻薄,只是想学习。谢谢
    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 2015-06-20
    • 2012-07-14
    • 2011-03-10
    • 2017-08-13
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多