名称" X"命名空间中不存在" clr-namespace:Y"

时间:2014-05-14 06:20:20

标签: c# wpf xaml user-interface

我正在尝试创建一个简单的GUI应用程序,它在GUI界面中更改字符串时会更改字符串的值。 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;

    namespace TryBinding
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();           
        }

        consle myconsole = new consle();
    }

    public class consle
    {
        public string mainstr {get; set;}
        private int change = 23;

        public void consle()
        { 
        }

        public void wrStr()
        {
            change = 44;
        }
    }
}

XAML代码如下:

<Window x:Class="TryBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TryBinding"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
    <local:consle x:Name="Data" mainstr="maintry"/>
</Window.DataContext>            

    <Grid>

        <Button Content="Button" HorizontalAlignment="Left" Margin="244,37,0,0" VerticalAlignment="Top" Width="75"/>

    <TextBlock HorizontalAlignment="Left" Margin="131,43,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Path=mainstr}" />

        <CheckBox x:Name="ok" Content="CheckBox" HorizontalAlignment="Left" Margin="131,76,0,0" VerticalAlignment="Top"/>

    </Grid>    

但我收到了错误:

Error 1 The name "consle" does not exist in the namespace "clr-namespace:TryBinding".

Error 2 'consle': member names cannot be the same as their enclosing type   

我无法理解为什么它会发生,因为类consle存在于命名空间中,而consle()是一个构造函数。

1 个答案:

答案 0 :(得分:2)

你的构造函数看起来像

 public consle()
    {
    }

构造函数没有数据类型。