如何从 UserControl class 调用方法?

How to call a method from a UserControl class?

我有一个 Windows 表单应用程序,我想在 UserControl class 中调用一个方法。

我的方法放在Desktop:

public Desktop(StringWrapper stringWrapper, AppConfigManager appConfigManager, RecoveryManager recoveryManager, IProfileManager profileManager, IScanPerformer scanPerformer, IScannedImagePrinter scannedImagePrinter)
{ 
  public void ScanDefault()
   {
     if (profileManager.DefaultProfile != null)
      {
        scanPerformer.PerformScan(profileManager.DefaultProfile, new ScanParams(), this, notify, ReceiveScannedImage());
        Activate();
      }
      else if (profileManager.Profiles.Count == 0)
      {
         ScanWithNewProfile();
      }
      else
      {
         ShowProfilesForm();
      }
   }
}

在同一个文件夹中我创建了一个用户控件 class:

public partial class UserControlTest : UserControl
{
    public UserControlTest()
    {
        InitializeComponent();
    }
}

在这个 UserControlTest 中,我想调用一个 ScanDefault 方法。

ScanDefault() 是一个内联函数。

它就像一个局部变量。

您不能从 Desktop() 范围之外调用它。

要调用它,您必须将其作为class方法并在签名中添加您要使用的参数。

public Desktop(StringWrapper stringWrapper,
               AppConfigManager appConfigManager,
               RecoveryManager recoveryManager,
               IProfileManager profileManager,
               IScanPerformer scanPerformer,
               IScannedImagePrinter scannedImagePrinter)
{
  ScanDefault(profileManager, scanPerformer);
}

public void ScanDefault(IProfileManager profileManager, IScanPerformer scanPerformer)
{
  // ...
}