WPF:对象并不存在于命名空间中

时间:2015-10-31 10:00:23

标签: wpf namespaces

我在App.xaml.cs有一个对象,你可以在这里看到

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Test_Project
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //Startup
            Window main = new MainWindow();
            main.Show();
        }
    }
    /// <summary>
    /// Global values for use during application runtime
    /// </summary>
    public class runtimeObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }

        }
     ....

我试图在我的应用程序运行期间创建一个对象实例。我在App.xaml

中的XAML中创建了此实例
<Application x:Class="Test_Project.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Test_Project">
    <Application.Resources>
        <local:runtimeObject x:Key="runtimeVariables" />

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles/Generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我收到的错误是说我的班级在命名空间中不存在。但是xmlns:local确实引用了包含对象的命名空间,任何想法?

1 个答案:

答案 0 :(得分:0)

Woops,<local:runtimeObject x:Key="runtimeVariables" />必须进入<ResourceDictionary>然后我不得不重建解决方案。