【发布时间】: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);
}
}
}
这段代码有什么问题?
【问题讨论】:
-
删除
<services:IocContainer x:Key="ioc" />和任何引用servicesxmlns 的内容。构建您的解决方案。修复任何阻止这种情况的东西。然后清理,重新启动VS,并重建。如果一切正常,请尝试再次将您的 IocContainer 添加到 xaml 中。此外,如果 IocContainer 未在与 WishLister.App 相同的程序集中定义,则您需要做一些其他事情。
标签: c# xaml mvvm win-universal-app