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

标签 c# xaml mvvm win-universal-app

我正在制作一个采用 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>

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

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

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

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

但是我创建了一个类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);
        }
    }
}

这段代码有什么问题?

最佳答案

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

  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.

关于c# - App.xaml 找不到命名空间 IocContainter MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34181859/

相关文章:

wpf - 依赖于 MouseEventArgs 的单元测试功能?

c# - 使用自定义项目模板在 ComboBox 中显示所选项目

c# - 如何更改 Windows Phone 中 slider 控件的高度?

c# - MVVM 交换控件

c# - 在Windows XP计算机上运行SlimDX C#应用程序时遇到的麻烦

c# - 如何就地重新排序数组以将偶数索引项放在奇数之前?

c# - 从 IOwinContext 获取路由模板

c# - 无法让 WPF ItemsControl 垂直拉伸(stretch)

c# - 使用绑定(bind)的 UI 反馈边框

wpf - MVVM 中没有静态资源的 RelativeSource FindAncestor?