【问题标题】:The name "Class1" does not exist in the namespace "clr-namespace:WpfApplication2"名称空间“clr-namespace:WpfApplication2”中不存在名称“Class1”
【发布时间】:2023-03-07 04:39:01
【问题描述】:

我有一个标题中的问题 为什么<v:Class1 x:Key="con" /> 字段中的文件 Edycja.xaml 收到错误消息

错误 7 命名空间“clr-命名空间:WpfApplication2”中不存在名称“Class1”

我在每个文件中都有使用,这需要这个。

Edycja.xaml

<Window 
   x:Class="AllSportsBets102.Edycja" 
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   Title="Edycja" 
   SizeToContent="WidthAndHeight" 
   WindowStartupLocation="CenterScreen" 
   x:Name="Window" 
   xmlns:v="clr-namespace:WpfApplication2">
   <Window.Resources>
      <v:Class1 x:Key="con" />
   </Window.Resources>
   <Grid>
      <Grid>
         <Grid.RowDefinitions>
            <RowDefinition Height="129*" />
            <RowDefinition Height="47*" />
         </Grid.RowDefinitions>
         <DataGrid Grid.Row="0" ItemsSource="{Binding Path=people, ElementName=Window}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
               <DataGridTextColumn Header="ClientName" Binding="{Binding clientName}" IsReadOnly="True"/>
               <DataGridTextColumn Header="ClientType" Binding="{Binding clientType}" IsReadOnly="True"/>
               <DataGridComboBoxColumn Header="Product" Width="120" CanUserSort="False" SelectedValueBinding="{Binding product}" >
                  <DataGridComboBoxColumn.ElementStyle>
                     <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=clientType,Converter={StaticResource con}}"/>
                        <Setter Property="SelectedValue" Value="{Binding Path=product}"/>
                     </Style>
                  </DataGridComboBoxColumn.ElementStyle>
                  <DataGridComboBoxColumn.EditingElementStyle>
                     <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=clientType,Converter={StaticResource con}}" />
                        <Setter Property="SelectedValue" Value="{Binding Path=product}"/>
                     </Style>
                  </DataGridComboBoxColumn.EditingElementStyle>
               </DataGridComboBoxColumn>
            </DataGrid.Columns>
         </DataGrid>
      </Grid>
   </Grid>
</Window>

Class1.cs(这是转换器)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AllSportsBets102;
using System.Windows.Data;
namespace WpfApplication2
{
    public class Class1 : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
            {
                if (value.ToString() == "National")
                    return Edycja.nationalProducts;
                return Edycja.interNationalProducts;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

Edycja.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
using System.Net;
using System.Xml;
using System.Xml.Schema;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Net.Mail;
using Xceed.Wpf.Toolkit;
using AllSportsBets102.DataGridHelpers;
using AllSportsBets102;
using System.ComponentModel;
namespace AllSportsBets102
{
    public class person
    {
        public string product
        {
            get;
            set;
        }
        public string clientType
        {
            get;
            set;
        }
        public string clientName
        {
            get;
            set;
        }
    }

    public partial class Edycja : Window
    {
        Funkcje FK;
        EdycjaKursow EK;
        public List<string> ListaDyscyplina = new List<string>();
        public List<string> ListaSystemRozgrywek = new List<string>();
        public List<string> ListaKraj = new List<string>();
        public List<string> ListaNazwaLigi = new List<string>();
        public List<string> ListaKolejka = new List<string>();
        public List<string> ListaSystemTypowan = new List<string>();
        public List<string> ListaGospodarze = new List<string>();
        public List<string> ListaGoscie = new List<string>();

        public static List<Data2> dataSourcetmpXXX = new List<Data2>();

        public ObservableCollection<person> people
        {
            get;
            set;
        }

        public static ObservableCollection<string> nationalProducts
        {
            get;
            set;
        }

        public static ObservableCollection<string> interNationalProducts
        {
            get;
            set;
        }

        public Edycja()
        {
            people = new ObservableCollection<person>()
            {
                new person(){product = "a",clientType = "National", clientName="mbt"},
                new person(){product = "p",clientType = "International", clientName="patni"},
                new person(){product = "b",clientType = "National", clientName="igate"},
                new person(){product = "r",clientType = "International", clientName="cgi"}

            };
            nationalProducts = new ObservableCollection<string>() { "a", "b", "c", "d", "e" };
            interNationalProducts = new ObservableCollection<string>() { "p", "q", "r", "s", "t" };
            InitializeComponent();
        }
    }
}

【问题讨论】:

  • Class1 是否标记为公开?您说命名空间是正确的,但请检查 Class1 是否在 WpfApplication2 命名空间中,并且与此 xaml 文件位于同一程序集中。
  • 所以 Class1 是公共的,Class1 在命名空间 WpfApplication2 namespace WpfApplication2 { public class Class1 : IValueConverter
  • 是 XAML 解析器中的错误还是构建错误?通常构建会更新底层符号文件,错误就会消失。
  • 我遇到了一些其他错误,并且在他们的删除项目启动后。谢谢人:)
  • 很高兴为您提供帮助。我发表了我的评论作为答案。如果您认为它有用或将来对其他人有用,请随时投票/接受。

标签: c# .net wpf xaml namespaces


【解决方案1】:

创建新类并在 XAML 中声明它时通常会出现此错误。尝试构建,如果错误是真实的,它仍然存在。

但大多数情况下,这会导致验证错误消失,您的应用程序将准备就绪。

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 2016-11-29
    • 2013-04-19
    • 2014-01-16
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多