为什么我在WPF用户控件上看到“成员无法识别或无法访问”错误?

时间:2021-07-09 06:03:12

I've got a custom user control with a public property that I'd like to be able to set in XAML. Here it is below.

我有一个自定义用户控件,其中包含我希望能够在XAML中设置的公共属性。它在下面。

TestControl.xaml

TestControl.xaml

<UserControl x:Class="Scale.Controls.TestControl"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

TestControl.xaml.cs

TestControl.xaml.cs

using System.Windows.Controls;

namespace MyProject.Controls
{
    public partial class TestControl : UserControl
    {
        public string TestMe { get; set; }
        public TestControl()
        {
            InitializeComponent();
        }
    }
}

Then, in my MainWindow.xaml file, I try to include this:

然后,在我的MainWindow.xaml文件中,我尝试包含这个:

<controls:TestControl TestMe="asdf" />

However, even though Visual Studio autocompletes the TestMe property, I then see things with a squiggly underline that says "The member "Test Me" is not recognized or is not accessible," as seen below.

但是,即使Visual Studio自动填充了TestMe属性,我也会看到带有波浪线下划线的内容,表示“成员”测试我“无法识别或无法访问”,如下所示。

为什么我在WPF用户控件上看到“成员无法识别或无法访问”错误?

I could have sworn I've done something like this before in other projects. How can I access (i.e. set) the public properties via XAML like this?

我可以发誓我之前在其他项目中做过类似的事情。如何通过XAML访问(即设置)公共属性?

5 个解决方案

#1


3  

You need to declare your property as Dependency Properties

您需要将属性声明为依赖属性

namespace MyProject.Controls
{
    public partial class TestControl : UserControl
    {
        //Register Dependency Property

        public static readonly DependencyProperty TestMeDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestControl));

        public string MyCar
        {
            get
            {

                return (string)GetValue(TestMeDependency);

            }
            set
            {
                SetValue(TestMeDependency, value);
            }
        }

        public TestControl()
        {
            InitializeComponent();
        }
    }
}

#2


13  

Visual Studio 2017

Visual Studio 2017

I had exactly the same problem. It was compiling one day ... and then it wasn't. I wasn't using DependencyProperty which shouldn't be needed as above. The properties were appearing in Intellisense but gave the same message when inserted. I cleaned, built, rebuilt, restarted VS, rebooted etc. All to no avail.

我有完全相同的问题。它有一天正在编译......然而事实并非如此。我没有使用上面不需要的DependencyProperty。属性出现在Intellisense中,但在插入时给出相同的消息。我清理,建造,重建,重新启动VS,重新启动等等都无济于事。

Last ditch try ... I removed all the offending attributes and got a clean compile. Then I put them back and it compiled. I really wasn't expecting that. Somehow VS had gotten its knickers in a twist.

最后一次尝试...我删除了所有违规的属性并得到了一个干净的编译。然后我把它们放回去编译。我真的没想到。不知怎的,VS已经扭曲了它的短裤。

#3


1  

Change the build target from AnyCPU to either x86 or x64. Unsure why AnyCPU does not work.

将构建目标从AnyCPU更改为x86或x64。不确定为什么AnyCPU不起作用。

#4


-1  

instead of setting multiple dependency properties for the control, I would use reflection.

而不是为控件设置多个依赖项属性,我会使用反射。

    public static readonly DependencyProperty UserControlProperty = DependencyProperty.Register("UserControl", 
typeof(object), typeof(CustomUserControl), new PropertyMetadata(null));

   public object UserControl
   {
            get { return GetValue(UserControlProperty); }
            set { SetValue(UserControlProperty, value); }
   }

#5


-1  

In my case, there're no errors originally, after I modify my class, there're some errors in my class then the Xaml Error member is not recognized show. After solving errors in my class, I pass building the projects, all projects are built without errors, but the errors still show in the Error List Window. In the end, I restart Visual Studio, the errors disappear.

在我的情况下,最初没有错误,在我修改我的类之后,我的类中出现了一些错误,然后Xaml Error成员无法识别show。在我的类中解决错误后,我通过构建项目,所有项目都构建没有错误,但错误仍显示在错误列表窗口中。最后,我重新启动Visual Studio,错误消失了。

#1


3  

You need to declare your property as Dependency Properties

您需要将属性声明为依赖属性

namespace MyProject.Controls
{
    public partial class TestControl : UserControl
    {
        //Register Dependency Property

        public static readonly DependencyProperty TestMeDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestControl));

        public string MyCar
        {
            get
            {

                return (string)GetValue(TestMeDependency);

            }
            set
            {
                SetValue(TestMeDependency, value);
            }
        }

        public TestControl()
        {
            InitializeComponent();
        }
    }
}

#2


13  

Visual Studio 2017

Visual Studio 2017

I had exactly the same problem. It was compiling one day ... and then it wasn't. I wasn't using DependencyProperty which shouldn't be needed as above. The properties were appearing in Intellisense but gave the same message when inserted. I cleaned, built, rebuilt, restarted VS, rebooted etc. All to no avail.

我有完全相同的问题。它有一天正在编译......然而事实并非如此。我没有使用上面不需要的DependencyProperty。属性出现在Intellisense中,但在插入时给出相同的消息。我清理,建造,重建,重新启动VS,重新启动等等都无济于事。

Last ditch try ... I removed all the offending attributes and got a clean compile. Then I put them back and it compiled. I really wasn't expecting that. Somehow VS had gotten its knickers in a twist.

最后一次尝试...我删除了所有违规的属性并得到了一个干净的编译。然后我把它们放回去编译。我真的没想到。不知怎的,VS已经扭曲了它的短裤。

#3


1  

Change the build target from AnyCPU to either x86 or x64. Unsure why AnyCPU does not work.

将构建目标从AnyCPU更改为x86或x64。不确定为什么AnyCPU不起作用。

#4


-1  

instead of setting multiple dependency properties for the control, I would use reflection.

而不是为控件设置多个依赖项属性,我会使用反射。

    public static readonly DependencyProperty UserControlProperty = DependencyProperty.Register("UserControl", 
typeof(object), typeof(CustomUserControl), new PropertyMetadata(null));

   public object UserControl
   {
            get { return GetValue(UserControlProperty); }
            set { SetValue(UserControlProperty, value); }
   }

#5


-1  

In my case, there're no errors originally, after I modify my class, there're some errors in my class then the Xaml Error member is not recognized show. After solving errors in my class, I pass building the projects, all projects are built without errors, but the errors still show in the Error List Window. In the end, I restart Visual Studio, the errors disappear.

在我的情况下,最初没有错误,在我修改我的类之后,我的类中出现了一些错误,然后Xaml Error成员无法识别show。在我的类中解决错误后,我通过构建项目,所有项目都构建没有错误,但错误仍显示在错误列表窗口中。最后,我重新启动Visual Studio,错误消失了。