[英]How can I change access level of fields generated by GUI designer in Xamarin Studio
I'm writing my code in Gtk# using Xamarin Studio. 我正在使用Xamarin Studio在Gtk#中编写代码。 When I create a new window and a TreeView in it, the access level will be private. 当我在其中创建一个新窗口和一个TreeView时,访问级别将是私有的。 I want to use it (TreeView) in an other class so I would like to change the access level to internal, but I couldn't find how can I make that. 我想在其他类中使用它(TreeView),所以我想将访问级别更改为内部,但我找不到如何实现。 Thanks for any help in advance. 在此先感谢您的帮助。 Here is the code which I want to change by the GUI designer (not writing inside to the code, because it will be overwritten by the GUI designer...) 这是我想要由GUI设计者更改的代码(不在代码内部写入,因为它将被GUI设计器覆盖......)
// This file has been generated by the GUI designer. Do not modify.
namespace XX_xxxx
{
public partial class Settings
{
private global::Gtk.VBox vbox1;
private global::Gtk.ScrolledWindow GtkScrolledWindow;
private global::Gtk.TreeView settingsTreeView;
private global::Gtk.HBox hbox1;
private global::Gtk.ToggleButton saveAndCloseButton;
private global::Gtk.ToggleButton closeButton;
protected virtual void Build ()
{
Here is where I want to use (in an other class where I use an instance of the Settings class): settings.settingsTreeView.Model = settingsListStore;
这是我想要使用的地方(在我使用Settings类的实例的其他类中): settings.settingsTreeView.Model = settingsListStore;
The error message is: 错误消息是:
Error CS0122: `XX_xxxxx.Settings.settingsTreeView' is inaccessible
due to its protection level (CS0122) (XX_xxxx_GUI)
I've got this answer in Xamarin forum: 我在Xamarin论坛得到了这个答案:
The supported way of doing this is to remove the property from the .designer.cs file and place it in the main .cs file. 支持的方法是从.designer.cs文件中删除该属性并将其放在主.cs文件中。 Then remove the [DesignerGenerated] attribute from the declaration. 然后从声明中删除[DesignerGenerated]属性。
The code generation in the designer will recognise that the [Outlet] exists in the actual .cs file and will not place one in the .designer.cs file. 设计器中的代码生成将识别[Outlet]存在于实际的.cs文件中,并且不会将其放在.designer.cs文件中。 Once you do this, you can add the public/internal modifiers as you want. 完成此操作后,您可以根据需要添加公共/内部修改器。
Generally the controls are marked as private to stop code in other modules from reaching in and changing attributes of the class directly. 通常,控件被标记为私有,以阻止其他模块中的代码直接进入和更改类的属性。 The good news is that there is another (IMHO) better way to do what you are trying to do. 好消息是,还有另一种(恕我直言)更好的方式来做你想做的事情。 Since the generated class is marked as partial you can use a non generated partial Settings class with whatever internal methods you add for what your other class needs to interact with the UI. 由于生成的类被标记为partial,因此您可以使用非生成的部分Settings类,以及您为其他类与UI交互所需的内部方法添加的内容。
This approach is generally considered superior as it will allow you to control how the other class interacts with the Settings class's private members (the controls). 这种方法通常被认为是优越的,因为它允许您控制其他类如何与Settings类的私有成员(控件)交互。 So you could add a method like this: 所以你可以添加这样的方法:
public partial class Settings
{
internal void SetModel(ModelType model)
{
// Check if valid model and throw some type of argument exception if not
settingsTreeView.Model = model;
}
}
and then call it like this: 然后像这样调用它:
settings.SetModel(settingsListStore);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.