C# 为什么控件由于其保护级别而无法访问?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1742909/
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-08-06 20:24:40  来源:igfitidea点击:

Why is the control inaccessible due to its protection level?

c#winformscontrols

提问by user164203

I'm trying to access a control's text property from program.cs and it says that it is inaccessible due to protected level. How can I fix this please?

我正在尝试从 program.cs 访问控件的文本属性,它说由于受保护级别而无法访问。请问我该如何解决?

采纳答案by jay_t55

This is the default property for controls and can be solved by:

这是控件的默认属性,可以通过以下方式解决:

  1. Going into Design-View for the Form that contains the specified Control
  2. Then changing the Control's Modifiers property to Public or Internal.
  1. 进入包含指定控件的窗体的设计视图
  2. 然后将控件的修饰符属性更改为公共或内部。

Control Properties >Modifiers Screenshot

控件属性 >修改器屏幕截图

回答by Asad

Control Protection level Resolved

控制保护级别已解决

Go to designer file search control By ID e.g txtModel change protectedmodifier to publicmodifier

转到设计器文件搜索控件按 ID 例如 txtModel 将受保护的修饰符更改为公共修饰符

回答by Asad

The concept behind is the protection level. As we have studied in Object Oriented Paradigm keep your class members variables private and set or get it from Property.Thats why it is not a good approach

背后的概念是保护级别。正如我们在面向对象的范式中研究的那样,让您的类成员变量保持私有并从属性中设置或获取它。这就是为什么它不是一个好方法

回答by Michael G

Use x:FieldModifier="public"e.g.

使用x:FieldModifier="public"例如

<TextBlock x:FieldModifier="public" x:Name="AccessibleTextBlock" />

as explained here: Modifying XAML named field visibility

如此处所述:修改 XAML 命名字段可见性

In my case, I put UserControl in another DLL. WPF's convention is to set all named fields as internalby default. Using the x:FieldModifier="public"has solved the issue.

就我而言,我将 UserControl 放在另一个 DLL 中。WPF 的约定是将所有命名字段设置为internal默认值。使用x:FieldModifier="public"已经解决了这个问题。