Link to home
Start Free TrialLog in
Avatar of mayhem1121
mayhem1121

asked on

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:
https://www.experts-exchange.com/questions/28124370/Create-Trigger-Event-in-a-custom-C-Net-DLL-Not-a-Control-or-Form.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!
Avatar of ste5an
ste5an
Flag of Germany image

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.
Avatar of mayhem1121
mayhem1121

ASKER

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!
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.
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!
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much, now I get it!

Great example.
You're welcome.