簡體   English   中英

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

[英]App.xaml can not found namespace IocContainter MVVM

我正在使用 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>

但它在標記線上給了我這個錯誤:

名稱IocContainerusing:WishLister.Services IocContainer的命名空間中不存在。

我也嘗試在斜體代碼的 stad 中使用clr-namespace:WishLister.Services WishLister.Services 但我有這兩個錯誤:

名稱IocContainer在名稱空間clr-namespace:WishLister.Services IocContainer中不存在。

XML 命名空間中的未知類型IocContainer clr-namespace:WishLister.Services;assembly=WishLister, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 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);
        }
    }
}

這段代碼有什么問題?

我是通過@Will 的評論找到的。 他或她說:

  1. 刪除<services:IocContainer x:Key="ioc" />和任何引用服務 xmlns 的內容。
  2. 構建您的解決方案。
  3. 修復任何阻止這種情況的東西。
  4. 然后清理、重新啟動 Visual Studio 並重建。
  5. 如果一切順利,請嘗試再次將您的 IocContainer 添加到 xaml 中。

另外,如果IocContainer沒有在同一程序定義為WishLister.App你需要做一些其他的東西。

嘗試清理並重建您的項目。

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM