我正在制作一个采用 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 namespaceusing:WishLister.Services
.
我还尝试在斜体代码的 stad 中使用 clr-namespace:WishLister.Services
但出现以下两个错误:
The name
IocContainer
does not exist in the namespaceclr-namespace:WishLister.Services
.Unknown type
IocContainer
in XML namespaceclr-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 的评论找到的。他或她说:
- Remove
<services:IocContainer x:Key="ioc" />
and anything that references the services xmlns.- Build your solution.
- Fix anything preventing this.
- Then clean, restart Visual Studio, and rebuild.
- If all is well, try to add your IocContainer into the xaml again.
Also, if
IocContainer
isn't defined in the same assembly asWishLister.App
you'll need to do some other stuff.
关于c# - App.xaml 找不到命名空间 IocContainter MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34181859/