简体   繁体   English

如何修复Winform中的“由于其保护级别而无法访问”错误?

[英]How to fix “inaccessible due to its protection level” error in winform?

I have a Form class 我有一堂课

    partial class ProgressMainForm : Form
    {
      public ProgressMainForm()
      {
        InitializeComponent();
      }
    }

And then a class that uses that class and contains all functionality for the user 然后是一个使用该类并包含用户所有功能的类

    public class ProgressForm
    {
        public ProgressMainForm myProgressForm;

        public ProgressForm(string title)
        {
            myProgressForm = new ProgressMainForm();
            myProgressForm.Text = title;
        }

        public void SetProgressBar(int min, int max)
        {
        ....
    }

I then use this ProgressForm class in my project like this 然后,我像这样在我的项目中使用这个ProgressForm类

            progresswindow = new ProgressForm("Replacing All Strings");

This way progresswindow only contains members that are related to the functionality of the ProgressForm and all those Form members are hidden from the user. 这样, progresswindow仅包含与ProgressForm功能相关的成员,而所有这些Form成员对用户都是隐藏的。

But sometimes I need to access those Form members, for example when I need Invoke method. 但是有时我需要访问那些Form成员,例如,当我需要Invoke方法时。

Is there a way to make myProgressForm in ProgressForm accessible to users without making ProgressMainForm public? 有没有一种方法可以使用户可以访问ProgressForm myProgressForm而无需公开ProgressMainForm

Or is this approach wrong? 还是这种方法是错误的?

Is there a way to make myProgressForm in ProgressForm accessible to users without making ProgressMainForm public? 有没有一种方法可以使用户可以访问ProgressForm中的myProgressForm而无需公开ProgressMainForm?

Yes, you can create some public properties on ProgressForm that expose specific properties of ProgressMainForm . 是的,你可以创建一些公共属性ProgressForm暴露的特定属性ProgressMainForm

private ProgressMainForm myProgressForm;

public int SomeProperty
{
  get { return myProgressForm.IntProp; }
  set { myProgressForm.IntProp = value; }
}

For readonly properties, omit the set , and for any types that are reference types, you may want to return a clone or copy (to ensure the client can't change it). 对于只读属性,请省略set ,对于任何引用类型的类型,您可能希望返回一个克隆或副本(以确保客户端无法更改它)。

Wrap or Expose the Methods you need. 包装或暴露所需的方法。 But somehow i don't like the approach, restricting the access is not a bad idea but should not be the whole purpose of this kind of abstraction. 但是以某种方式我不喜欢这种方法,限制访问权限不是一个坏主意,但不应成为这种抽象的全部目的。 Try to make the acess easier, not restrictive. 尝试使访问更容易,而不是限制性的。

In my opinion you should not work with the form directly. 我认为您不应直接使用该表格。 If I read your setup correctly, you want to show progress indicator while some job is being done. 如果我正确阅读了您的设置,则您想在完成某些工作时显示进度指示器。 ProgressForm should expose methods to set the counters and increment them; ProgressForm应该公开设置计数器并递增计数器的方法。 as you run it on another thread, form manipulation should be done from inside the methods of ProgressForm. 当您在另一个线程上运行它时,应该从ProgressForm方法内部进行表单操作。 Your Invokes belong there, wrapped in suitable methods. 您的Invokes属于此处,并以合适的方法包装。 If you want to change some visual properties of ProgressMainForm relay those properties to ProgressForm. 如果要更改ProgressMainForm的某些可视属性,请将这些属性中继到ProgressForm。

To resume, calling code should have no clue what ProgressForm does other than setting progress boundaries, starting, setting current percentage and stopping. 要恢复,调用代码除了设置进度边界,开始,设置当前百分比和停止之外,不知道ProgressForm做什么。 This way, if you are asked to port the application to another UI system the amount of code you will need to change will be drastically reduced. 这样,如果系统要求您将应用程序移植到另一个UI系统,则需要更改的代码量将大大减少。

您可以将这些方法声明为internal ,这将允许您从程序集中调用这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM