3

I'm having a hard time to decouple two classes.

I have my code behind (will call it "class A") that I use to tweak the interface (defined in xaml).

Next I have a class B that is only logic. But while the logic is executing I have to update the UI.

My problem is that I can't "return" from the class B back to A to update the UI because B has not finished working. And I can't give the view itself to modifiy to B because it would couple A and B.

I suppose that I have to use some interfaces logic but I don't know how.

For example :

Class A
{
     private void OnClickEvent()
     {
         var B = new(B);
         b.work();
     }

     private void UpdateUI()
     {
        ...
     }
}


Class B
{
    public void work()
    {
        while (...)
        {
             ...
             //Here, how to call A.UpdateUI() ?
             ...
        }
    }
}

Thanks !

Doctor
  • 131

3 Answers3

1

You would probably execute work() asynchronously and then use events to signal the changes which might cause the UI to update. The benefit of events is that the event source does not know anything about the subscribers, so you can still keep the business logic (B) decoupled from the view (A).

JacquesB
  • 61,955
  • 21
  • 135
  • 189
0

Simply introduce an interface:

class A : BUpdateTarget
{
     private void OnClickEvent()
     {
         var B = new(B);
         b.work(this);
     }

     public void UpdateUI()
     {
        ...
     }
}


interface BUpdateTarget
{
    UpdateUI();
}

class B
{
    public void work(BUpdateTarget updateTarget)
    {
        while (...)
        {
             //...
             updateTarget.UpdateUI()
             //...
        }
    }
}
Euphoric
  • 38,149
0

It seems to me that your B class represents the logic of some user interface, while the A represents the code-behind class of the same user interface.

If you just can't use MVVM, I would recommend make the B class implement the INotifyPropertyChanged interface, so it can notify the client (the A class) about changes in its state (like updates in a collection, for instance). Once the change occurs, A gets notified, and can do whatever it needs to do.