c# - C#表单类事件
问题描述
所以我有两个类“SplashScreenForm”和“Program”,并且在事件中想要运行“MainForm”。它看起来像这样:
public partial class SplashScreenForm : Form
{
public event EventHandler<int> RaiseUpdateEvent;
........
并在“程序”中定义这样的类
static class Program
{
static Form SplashScreen;
static Form MainForm;
SplashScreen = new SplashScreenForm();
.........
但问题是,当我尝试访问事件时,它说“SplashScreenForm”不包含该事件。我怎么解决这个问题?
SplashScreen.RaiseUpdateEvent += SplashScreen_RaiseUpdateEvent;
错误说:CS1061 C# 'Form' 不包含定义,并且找不到接受“Form”类型的第一个参数的可访问扩展方法(您是否缺少 using 指令或程序集引用?)
解决方案
错误说,名为des 的类Form
没有名为的事件RaiseUpdateEvent
只有班级SplashScreenForm
有那个事件。
因此,您应该将您的字段定义为类型SplashScreenForm
:
static SplashScreenForm SplashScreen;
更高级的替代方案:
如果出于某种原因您希望变量SplashScreen
具有类型Form
(可能是您从无法控制的库中获取类型),您实际上仍然可以使用它,因为您知道它是SplashScreenForm
当您调用new SplashScreenForm()
.
在这种情况下,您可以像这样将其转换为您的类型:
((SplashScreenForm)SplashScreen).RaiseUpdateEvent += SplashScreen_RaiseUpdateEvent;
现在您告诉编译器,虽然它被声明为Form
,但它实际上是 aSplashScreenForm
并且可以用作那个 - 这意味着该cast
类型确实有一个RaiseUpdateEvent
事件。
推荐阅读
- matplotlib-basemap - 我们如何在没有互联网的情况下使用分辨率好的地图在树莓派上映射经度?
- oracle - 如何在使用 SSMA 进行数据迁移时忽略列的大小写敏感性
- jquery - 如何在 jquery-3.4.1.min.js 中调试活动
- html - 无论如何我可以根据它包含的文本行数调整 LI 元素的高度吗?(更多行 = 更高的 LI)(没有 jQuery)
- c# - LINQ 读取和格式化 XML 文件然后创建文本文件
- python - Python“FileHandler”到 Azure BlobStorage - 有没有这样的事情
- r - 简单线性回归的循环
- grep - Count of no of files which contain a particular string in its name
- discord.js - 如何在 discord.js 中提及消息的作者?
- ios - 如何在情节提要中快速添加“HMSegmentedControl”并带有约束