如何更改 Xamarin Studio 中 GUI 设计器生成的字段的访问级别
How can I change access level of fields generated by GUI designer in Xamarin Studio
我正在使用 Xamarin Studio 在 Gtk# 中编写代码。
当我在其中创建一个新的 window 和一个 TreeView 时,访问级别将是私有的。我想在另一个 class 中使用它 (TreeView),所以我想将访问级别更改为内部,但我找不到该怎么做。
感谢您提前提供帮助。
这是我想由 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 ()
{
这里是我想要使用的地方(在另一个 class 中我使用了设置实例 class):
settings.settingsTreeView.Model = settingsListStore;
错误信息是:
Error CS0122: `XX_xxxxx.Settings.settingsTreeView' is inaccessible
due to its protection level (CS0122) (XX_xxxx_GUI)
通常,控件被标记为私有,以阻止其他模块中的代码直接进入和更改 class 的属性。好消息是还有另一种(恕我直言)更好的方法来做你想做的事情。由于生成的 class 被标记为部分,您可以将非生成的部分设置 class 与您为其他 class 需要与 UI 交互而添加的任何内部方法一起使用.
这种方法通常被认为是优越的,因为它允许您控制其他 class 如何与设置 class 的私有成员(控件)交互。所以你可以添加这样的方法:
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;
}
}
然后这样称呼它:
settings.SetModel(settingsListStore);
我在 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. Then remove the [DesignerGenerated] attribute from the declaration.
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. Once you do this, you can add the public/internal modifiers as you want.
我正在使用 Xamarin Studio 在 Gtk# 中编写代码。 当我在其中创建一个新的 window 和一个 TreeView 时,访问级别将是私有的。我想在另一个 class 中使用它 (TreeView),所以我想将访问级别更改为内部,但我找不到该怎么做。 感谢您提前提供帮助。 这是我想由 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 ()
{
这里是我想要使用的地方(在另一个 class 中我使用了设置实例 class):
settings.settingsTreeView.Model = settingsListStore;
错误信息是:
Error CS0122: `XX_xxxxx.Settings.settingsTreeView' is inaccessible
due to its protection level (CS0122) (XX_xxxx_GUI)
通常,控件被标记为私有,以阻止其他模块中的代码直接进入和更改 class 的属性。好消息是还有另一种(恕我直言)更好的方法来做你想做的事情。由于生成的 class 被标记为部分,您可以将非生成的部分设置 class 与您为其他 class 需要与 UI 交互而添加的任何内部方法一起使用.
这种方法通常被认为是优越的,因为它允许您控制其他 class 如何与设置 class 的私有成员(控件)交互。所以你可以添加这样的方法:
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;
}
}
然后这样称呼它:
settings.SetModel(settingsListStore);
我在 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. Then remove the [DesignerGenerated] attribute from the declaration.
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. Once you do this, you can add the public/internal modifiers as you want.