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
Why is the control inaccessible due to its protection level?
提问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
回答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 internal
by default. Using the x:FieldModifier="public"
has solved the issue.
就我而言,我将 UserControl 放在另一个 DLL 中。WPF 的约定是将所有命名字段设置为internal
默认值。使用x:FieldModifier="public"
已经解决了这个问题。