【问题标题】:WPF Custom User Control - "The member X is not recognized or accessible"WPF 自定义用户控件 - “成员 X 无法识别或无法访问”
【发布时间】:2015-09-04 23:09:53
【问题描述】:

我有这个自定义控件,但无法从我的 Main 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; }
    }

这就是我在 Main XAML 中收到的错误消息“无法识别或访问成员 RegisterTitle”:

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

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

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

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

    例如:

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

    【讨论】:

    • 谢谢,我做到了.. 我会尝试上面的建议
    【解决方案2】:

    尝试将您的属性更改为Dependency Properties

    这应该会有所帮助,因为它有示例代码 - Why am I seeing a “member is not recognized or is not accessible” error on my WPF User Control?

    更新

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

    • 在您的控制下,确保以&lt;/UserControl&gt; 结束它
    • Controls:RegisterTextBox 更改为使用小写“c”,因此controls:RegisterTextBox

    【讨论】:

    • 谢谢!我可能没有正确遵循它,所以它仍然对我不起作用。我是否做出了正确的更改:&lt;Label Name="LabelRegisterTitle"/&gt; public static readonly DependencyProperty TestMeDependency = DependencyProperty.Register("LabelRegisterTitle", typeof(string), typeof(RegisterTextBox)); public string RegisterTitle { get { return (string)GetValue(TestMeDependency); } set { SetValue(TestMeDependency, value); } }
      对不起,我无法让换行符工作......
    • 我刚刚在不使用Dependency Properties 的情况下尝试了您的代码,它工作正常。只是检查一下,您的用户控件末尾是否有&lt;/UserControl&gt;?另外,将&lt;Controls:RegisterTextBox RegisterTitle="This Is A Test"/&gt; 更改为使用小写的“c”,因此&lt;controls:RegisterTextBox RegisterTitle="This Is A Test"/&gt;
    • 是的,我最后确实有&lt;/UserControl&gt;,只是错过了复制/粘贴。但由于某种原因,我仍然无法让它工作。顺便说一句,如果它被封装在namespace FireflyMmwDiagnostics.Controls中,为什么要更改为小写'c'?它不能用较低的 'c' 编译。
    • 即使我在 Main XAML 中将其更改为较低的 'c',仍然没有运气:( xmlns:controls="clr-namespace:FireflyMmwDiagnostics.Controls" &lt;controls:RegisterTextBox RegisterTitle="This Is A Test"/&gt;.... 我只需要挖掘一下,也许我有一些其他问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2012-11-09
    • 2022-11-22
    相关资源
    最近更新 更多