将类添加到XamlPage时出现问题

jgzswidk  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(66)

在将我的类连接到xamlpage时遇到了一个小问题。。VS给我发短信说程序看不到命名空间

但我有另一个类在这个文件夹,其中一个是正常工作

我想给你们看一段xaml页面的代码

<Page x:Class="ClinicApp.XamlPages.ListOfPatientsPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:ClinicApp.XamlPages"
      xmlns:localModels="clr-namespace:ClinicApp.EntityModels" 
      xmlns:local1="clr-namespace:ClinicApp.HelperClasses"
      mc:Ignorable="d" 
      d:DesignHeight="300" Width="1200"
      Title="ListOfPatientsPage" Loaded="Page_Loaded" x:Name="page" Background="White">
    <Page.Resources>
        <local:EnumDescriptionConverter x:Key="EnumDescriptionConverter"/>
        <local:AgeConverter x:Key="AgeConverter" />
    </Page.Resources>

这一个是不工作,并显示错误local:AgeConverter x:Key="AgeConverter"这错误是在开始的问题
我也可以给你看工人阶级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClinicApp.EntityModels;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Reflection;
using System.ComponentModel;
using System.Globalization;

namespace ClinicApp.XamlPages
{
    //takes descriprion from gender and requestType enums and place is in datagrid
    //instead of showing the real values 
    public class EnumDescriptionConverter : IValueConverter
    {
        private string GetEnumDescription(Enum enumObj)
        {
            FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

            object[] attribArray = fieldInfo.GetCustomAttributes(false);

            if (attribArray.Length == 0)
            {
                return enumObj.ToString();
            }
            else
            {
                DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
                return attrib.Description;
            }
        }

        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Enum myEnum = (Enum)value;
            string description = GetEnumDescription(myEnum);
            return description;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Empty;
        }
    }
}

我不明白他为什么抱怨命名空间对不起,如果这个问题是愚蠢的…我只是想了解wpf

wlwcrazw

wlwcrazw1#

在文件EnumDescriptionConverter.cs中,您可能在名称空间中犯了一个错误(它是ClinicApp.XamlPages而不是ClinicApp.HelperClasses)。local被定义为xmlns:local="clr-namespace:ClinicApp.XamlPages",所以它可以工作。我猜AgeConverter在另一个命名空间(ClinicApp.HelperClasses)中,所以应该这样使用:<local1:AgeConverter x:Key="AgeConverter" />
只需修复EnumDescriptionConverter.cs中的名称空间并使用local1(您可能应该将其重命名为helperClasses),它应该可以工作。

相关问题