开发者

Accessing derived class from base class object issue

开发者 https://www.devze.com 2023-01-10 11:31 出处:网络
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

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 u开发者_运维百科srctrlSimulator : 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 ?


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 called ApplyTemplate(). What would be the meaning of this in csAlgorithm? You can also look at it from a different angle: Would it be reasonable to use csAlgorithm wherever you could use UserControl, e.g. when invoking UserControl.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 the usrctrlSimulator 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:

public partial class usrctrlSimulator : UserControl
{
   public usrctrlSimulator()
   {
      this.InitializeComponent();          
   }

   public void StartSimulator()
   {
      _algorithm= new csAlgorithm();
      _algorithm.InitializeSimulator();
      timer1.Start();
   }

   private csAlgorithm _algorithm;
}

public class csAlgorithm // not a UserControl anymore
{
   public csAlgorithm()
      {
      }

   public void InitializeSimulator()
   {
      txtblkSimulatorStatus.Text = "Started"; // this element would be from the user control
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消