从基类对象访问派生类问题
我有一种奇怪的情况...
我在 WPF 中有一个用户控件,而它又附加了一些其他用户控件,然后我有一个巨大的 C# 代码文件,其中包含需要访问用户控件 UI 元素的大型算法和方法,这个洞过程与计时器一起工作,计时器将数据从用户控件发送到 C# 代码文件算法,它需要从控件返回和更新 UI 元素,还需要访问它的方法...
现在的问题是我不想将这个巨大的算法放在控件的代码隐藏文件中,而是想从该代码文件访问控件的 UI 元素和声明的方法...
到目前为止我尝试的是实际派生代码文件的类从我使用的用户控件来看,这工作得很好,但要访问派生类,我需要创建它的一个新对象,并且我显示的 UI 不会更新,因为它还创建了一个我相信的新基类对象。 ..
所以我有这样的事情:
public partial class usrctrlSimulator : UserControl
{
public usrctrlSimulator()
{
this.InitializeComponent();
}
public void StartSimulator()
{
Algorithm = new csAlgorithm();
Algorithm.InitializeSimulator();
timer1.Start();
}
}
public class csAlgorithm : usrctrlSimulator
{
public csAlgorithm()
{
}
public void InitializeSimulator()
{
txtblkSimulatorStatus.Text = "Started"; // this element would be from the user control
}
}
所以我的问题是:如何在不实例化派生类的新对象的情况下调用派生类,因为这将导致创建新的用户控件对象并且显示的 UI 将不会更新..或者如果我不派生 Algorithm 类,我有什么可能访问用户控制元素和方法?
I have a kind of weird situation ...
I have a User Control in WPF witch in turn has some other User Controls attached to it, then I have a huge C# code file with a big algorithm which needs access to the User Control UI Elements and methods, this hole process works with a Timer which sends data to the C# code file algorithm from the User Control and it needs to return and update the UI elements from the control and also to access it's methods...
Now the thing is I don't want to put this huge algorithm in the codebehind file of my control, instead I would like to access the control's UI elements and declared methods from that code file ...
What I tried so far is to actually derive the code file's class from the User Control I use, this works fine and dandy but to access the derived class I need to create a new object of it and the UI that I get shown does not get updated since it also creates a new base class object I believe ...
so I have something like:
public partial class usrctrlSimulator : UserControl
{
public usrctrlSimulator()
{
this.InitializeComponent();
}
public void StartSimulator()
{
Algorithm = new csAlgorithm();
Algorithm.InitializeSimulator();
timer1.Start();
}
}
public class csAlgorithm : usrctrlSimulator
{
public csAlgorithm()
{
}
public void InitializeSimulator()
{
txtblkSimulatorStatus.Text = "Started"; // this element would be from the user control
}
}
So my question is : how do I call the derived class without instantiating a new object of it, since that will cause a new user control object to be created and the displayed UI will not be updated ... or if I don't derive the Algorithm class, what possibility do I have to access the user control elements and methods ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想坚持使用该控件的一个实例,并且仍然可以访问派生类中的功能,那么您需要使用派生类作为控件。因此,您可以在任何地方使用 csAlgorithm,而不是
usrctrlSimulator
的实例。但是,我不确定这种设计是否是您的场景中的最佳方法。该算法并不是真正的用户控件,因此从
usrctrlSimulator
派生可能不是理想的选择。例如:UserControl
有一个名为ApplyTemplate()
的方法。这在csAlgorithm
中意味着什么?您还可以从不同的角度来看待它:在可以使用UserControl
的地方使用csAlgorithm
是否合理,例如在调用UserControl.AddLogicalChild(csAlgorithm) 时?
另一种选择是将算法实例化为 usrctrlSimulator 中的成员变量(复合)。在这种情况下,您仍然可以在
usrctrlSimulator
中使用它,但您将清楚地分离两个概念:一方面是 UserControl,另一方面是算法的实现。此外,您可以更改其中一个,而对另一个的影响有限。在这种情况下,您的代码将如下所示:
If you want to stick with one instance of the control and still have access to the functionality in the derived class then you need to use the derived class as the control. So instead of an instance of
usrctrlSimulator
, you'd use csAlgorithm everywhere.However, I'm not sure whether this design is the best approach in your scenario. The algorithm is not really a user control so maybe deriving from
usrctrlSimulator
is not the ideal option. For example:UserControl
has a method calledApplyTemplate()
. What would be the meaning of this incsAlgorithm
? You can also look at it from a different angle: Would it be reasonable to usecsAlgorithm
wherever you could useUserControl
, e.g. when invokingUserControl.AddLogicalChild(csAlgorithm)
?A different option would be to instantiate the algorithm as a member variable in
usrctrlSimulator
(composite). In that case you could still use it inside theusrctrlSimulator
but you would have a clear separation of two concepts: A UserControl on one hand, and the implementation of an algorithm on the other hand. In addition you could then change either one of them with only limited impact on the other.In that case your code would look as follows: