简体   繁体   English

如何更改Xamarin Studio中GUI设计器生成的字段的访问级别

[英]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.

相关问题 如何识别由Visual Studio的GUI设计器生成的代码? - How to recognize code generated by Visual Studio's GUI designer? 如何访问相关记录的属性? (O / R设计人员生成的实体) - How can I access properties of related records? (O/R designer generated entities) 如何访问由Designer(XAML)创建的xamarin(C#,Visual Studio 2015)中的控件 - How to access controls in xamarin (C#, Visual Studio 2015) created by Designer (XAML) 找不到Visual Studio GTK GUI设计器/源按钮 - Can not find Visual Studio GTK GUI designer/source button 如何告诉Visual Studio不填充设计器代码中的字段? - How can I tell Visual Studio to not populate a field in the designer code? 如何在没有表单的情况下使用 Visual Studio 设计器 - How can I use Visual Studio designer without form Xamarin iOS Designer-辅助功能字段 - Xamarin iOS Designer - Accessibility fields 如何更改 C# 项目中的.Designer.cs? - How can I change .Designer.cs in C# project? 如何使用设计人员生成的数据表为AUTOINCREMENT列手动设置ID? - How can I manually set an ID for an AUTOINCREMENT column using a designer-generated DataTable? 如何添加自己的代码以及表单设计器生成的代码? - How can I add my own code along with the code generated by the form designer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM