WPF 自定义用户控件 - “成员 X 无法识别或无法访问”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32399202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:23:34  来源:igfitidea点击:

WPF Custom User Control - "The member X is not recognized or accessible"

c#wpfxaml

提问by SYB

I have this custom control but cannot access its members from my Main XAML

我有这个自定义控件,但无法从我的主 XAML 访问它的成员

<UserControl x:Class="FireflyMmwDiagnostics.Controls.RegisterTextBox"
         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">
<Grid>
    <Label Name="LabelRegisterTitle"/>
</Grid>

public partial class RegisterTextBox : UserControl
{
    public RegisterTextBox()
    {
        InitializeComponent();
    }

    public string RegisterTitle
    {
        get { return this.LabelRegisterTitle.Content.ToString(); }
        set { this.LabelRegisterTitle.Content = value; }
    }

And this is what I have in Main XAML where I get the error stating "The member RegisterTitle is not recognized or accessible":

这就是我在 Main XAML 中遇到的错误,指出“成员 RegisterTitle 无法识别或无法访问”:

<Controls:RegisterTextBox RegisterTitle="This Is A Test"/>

I watched a couple of YouTube videos and this is exactly how they did it and it worked for them for some reason. Please advice on what could be the issue here. Thank you!

我看了几个 YouTube 视频,这正是他们所做的,并且出于某种原因对他们有用。请就此处可能出现的问题提供建议。谢谢!

回答by Stuart

Try changing your property to be declared as Dependency Properties

尝试将您的财产更改为声明为 Dependency Properties

This should help as it has example code - Why am I seeing a “member is not recognized or is not accessible” error on my WPF User Control?

这应该会有所帮助,因为它有示例代码 -为什么我在 WPF 用户控件上看到“成员无法识别或无法访问”错误?

Update

更新

Your code works OK for me without using Dependency Properties, so a couple of things to try:

您的代码在不使用依赖属性的情况下对我来说可以正常工作,因此可以尝试以下几点:

  • In your control, make sure you end it with </UserControl>
  • Change Controls:RegisterTextBoxto use a lower case 'c', so controls:RegisterTextBox
  • 在您的控制下,确保以 </UserControl>
  • 更改Controls:RegisterTextBox为使用小写的“c”,因此controls:RegisterTextBox

回答by Jamie P

You might need to rebuild in order for it to see RegisterTitle.

您可能需要重建才能看到RegisterTitle.

If that doesn't work, make sure you have defined in XAML where Controlsis in your project, otherwise it definitely won't be able to see it!

如果这不起作用,请确保您在 XAML 中定义了Controls项目中的位置,否则它肯定无法看到它!

For example:

例如:

<Window xmlns:Controls="clr-namespace:FireflyMmwDiagnostics.Controls.RegisterTextBox">