【问题标题】:How to access methods of User Controls passed as parameter to a Constructor如何访问作为参数传递给构造函数的用户控件的方法
【发布时间】:2017-07-17 17:49:56
【问题描述】:

我将 UserControl 的实例作为 t​​his 传递给继承 Page Class 和 Itemplate Interface 的类的构造函数,我正在保存 userControl 类型的实例,现在我如何访问用户 Control 的方法。 例如,MyUserControl 文件名为 uc_test 和 Class datagridtemplate:Page, Itemplate

我的 UserControl 有部分类名 uc_test 并且有方法

public int addtwonumbers()
{
 return 10+20;
}

datadatagridtemplate x = new datagridtemplate(this); //new instance 

在datagridtemplate.cs文件中:

public datagridtemplate(UserControl uc){}

在datagridtemplate类的一种方法中我想做以下

int sum = uc.addtwonumbers();

现在我想访问 uc_test 中定义的所有方法和属性,我该如何实现呢?

【问题讨论】:

  • 在构造函数中将用户控件转换为您的类型:uc = uc as MyType
  • 这里 Mytype 是你指的类文件?即 uc_test ?
  • 如果你提供一个最小的例子,每个人都会更清楚:stackoverflow.com/help/mcve
  • 我想是的,从您在此处发布的内容无法判断。无论您将类定义为什么,即public class MyType : UserControl
  • 它说可以找到类型或命名空间(缺少指令或程序集引用),

标签: c# user-controls itemplate


【解决方案1】:

我的 UserControl 有部分类名 uc_test

按照 Jack Miller 的建议,将您的通用 UserControl 引用转换为您创建的正确类型:

uc_test ucTst = (uc_test)uc;
int sum = ucTst.addtwonumbers();

【讨论】:

  • 我尝试这样做,但它说找不到,我尝试调用命名空间并包含在 c# 文件中,但它会引发错误。
猜你喜欢
  • 2011-09-04
  • 2014-09-10
  • 2011-06-28
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
相关资源
最近更新 更多