Trigger Event from a dynamic DLL to a WPF UserControl

Hi guys,

I have an application and a variety of DLLs.

The application control a WPF GUI.
Each DLL is a stand alone algorithm.

I have to use dynamic DLL's because I want to add to a folder a new DLL and let the user see it in the GUI without the need to recompile the application again.

After the user choose a DLL the application shall go to the DLL to perform the code that is there and return to the application in the end.

I need to create events that will allow me to return to the application from the DLL (midway) and change things like text block or display a custom message box and then continue the DLL or stop in case of an error.

I have used the code that was suggested here:
http://www.experts-exchange.com/Programming/Algorithms/Q_28124370.html

and it is working great, the only problem is that I can't find and use any variables (or the text block name I created in xaml)  when I go to handle the event in the application (in the link the location is where it says // do something here..)

It's like the event got nothing to do with the GUI.

How can I see all the variables and change my text block?

Any help would be highly appreciated.

Thanks in advance!
mayhem1121Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Without a concise and complete example, it's hard to tell. Cause it sounds like a problem of understanding the concept.

But a library does not publish any variables. "Only" methods and classes you may invoke.
0
mayhem1121Author Commented:
Thanks for your reply, I will try to elaborate.  
My WPF application:

In XAML I defined a text block

 <TextBlock x:Name="currentState" HorizontalAlignment="Center" FontSize="16" /> 

Open in new window


 
In the code behind:
 
   
 
{
...
            // creating dll instance
            Assembly asm = Assembly.LoadFile( unitDllPath );
            Type typ = asm.GetType( unitName + ".Class1", true, true );
            unitDll = Activator.CreateInstance( typ );
 
            unitDll.DisplayCustomMessageBox += new EventHandler( eventDisplayCustomMessageBox );
 
            for( i = 0; i < 100; i++ )
            {
                unitDll.Execute(ref messageInput, ref imageInput );
                System.Threading.Thread.Sleep( 55 );
            }
...
}


        static void eventDisplayCustomMessageBox( object sender, EventArgs e )
        {
      
            string strImageSource = ImagesDirectory + "\\" + imageInput +  ".png";
            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.UriSource = new Uri( strImageSource, UriKind.Absolute );
            bmi.EndInit();

            CustomMessageBox.Show( messageInput, bmi );             
        }

Open in new window


 
My dll file: (which is a dynamic - so I can't enter the dll to the reference before compiling the app)

  public class Class1
    {
       
        public event EventHandler DisplayCustomMessageBox;
  
        public Class1()
        {

        }

      
        public void Execute( ref string messageInput, ref string imageInput )
        {

            if( DisplayCustomMessageBox != null )
            {
                messageInput = "dll";
                imageInput = "WW257 CH1 RV1";

                DisplayCustomMessageBox( this, EventArgs.Empty );
            }
       }
  }

Open in new window



I have used ref because I couldn't create my own event for example MyEventHandler (that will pass 2 strings - due to the fact that the application wouldn't know the name of the event I've created in the dll).

Question 1:
Is there a way to get this file to know the name of the event I created in the dll without putting the dll in the reference of the application?


Question 2:
When I enter "DisplayCustomMessageBox" I'm going back to the application.

I want to write inside "eventDisplayCustomMessageBox" the next line:

currentState.Text = messageInput;

 but currentState is unknown to this event.

Who can I resolve that?


Thanks a lot!
0
ste5anSenior DeveloperCommented:
As I said, it's a problem of understanding the concept.

The normal approach is:

Define an interface.
Code the invocation against that interface in your front-end.
Each library needs to implement that interface. Then you would normally use an IoC container like Unity or MEF to load the concrete object from your library.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

mayhem1121Author Commented:
I'm still having difficulties understanding this concept.

Maybe I didn't explained myself correctly? If you understood it the first time please accept my  apology.

I have WPF user control A and a dynamic dll B.
A load B and then calling a function in B.
B is running but can go back to A in order to:
     Change textblock / display custom messagebox and then return to B and continue.
     End in case of an error in B.

If I'm creating interface in A how would B knew it?

Any chance to get a sample code for both the A and dll B?

Thanks for all the help!
0
ste5anSenior DeveloperCommented:
I've attached a simplified sample.

It uses Unity as IoC container. The key of the solution is that the consuming application only knows the contracts. So it is able to work with the instances of interfaces defined there. The contract library also contains the methods for dynamically loading the libraries.

Neither the consumer, nor the contract knows about the implementations of the libraries.

The contract is the minimum you need to define and your libraries need to implement. They are the backbone of your algorithms.

btw, this kind of pattern is called: Strategy pattern. Or here.

As I don't know, what your concrete goals are, you should also take a closer look at MEF. Cause it fits better into the WPF world.

No, I've not.. complete solutions are not accepted.

So,here's it on OneDrive: IoC.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mayhem1121Author Commented:
Thank you very much, now I get it!

Great example.
0
ste5anSenior DeveloperCommented:
You're welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.