.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Implementing plugin system with image notifications
Ask a questionAsk a question
 

AnswerImplementing plugin system with image notifications

  • Sunday, November 01, 2009 6:55 PMAttila Fogel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello Again,

    i encountered a new problem. What i wanna do is that i have a plugin system (found over the net) and to be able to use images as notifications from the plugin.

    I wrote a weather plugin (TrayGadget) that checks for weather updates in every minute. I have a main wpf window on which i have a stackpanel control (TrayGadgetStack). I want to add the Gadget's IconImage as an image to the stack and i want my gadget to update this image on the main window when the weather (and the matching image) changes. Eg i want to display clouds if the weather is cloudy, but when the sun comes out, i want the image to be changed to a sun image - and the code should be in the plugin's code.

    Here's some code:

    the plugin interface:

    Imports System.Windows.Controls
    
    Public Interface TrayGadget
    
        Sub LoadFunction()
        Property IconImage() As Image
    
    End Interface
       
    
    Now the WeatherPlugin as follows:

    Imports System.Windows.Threading
    Imports System.Runtime.InteropServices
    Imports System.Windows
    Imports System.Windows.Media.Imaging
    Imports System.Xml
    Imports System.Windows.Controls
    
    Public Class TrayGadget
    
        Implements TrayGadgetInterface.TrayGadget
    
        Dim timer As New System.Windows.Threading.DispatcherTimer
        Dim _IconImage As New Image
        
        Private Sub Update()
             'do the update of the current conditions and get the image that matches, raise and image changed event to be able to change the image on the parent form
        End Sub
    
       Private Sub TimerTick(ByVal sender As Object, ByVal e As System.EventArgs)
            Try
                Me.Update()
            Catch ex As Exception
                Me.IconImageSource = My.Application.Info.DirectoryPath & "\images\no_network.png"
            End Try
        End Sub
    
        Public Sub LoadFunction() Implements TrayGadgetInterface.TrayGadget.LoadFunction
            IconImageSource = "1.png"
            Me.timer.Interval = New TimeSpan(0, 1, 0)
            AddHandler timer.Tick, AddressOf TimerTick
            timer.Start()
            Update()
        End Sub
    
       Property IconImage() As Image Implements TrayGadgetInterface.TrayGadget.IconImage
            Get
                Return _IconImage
            End Get
            Set(ByVal value As Image)
                _IconImage = value
            End Set
        End Property
    
    End Class
    
    
    

    and this is how i call the plugin:

     Private Function RunGadget(ByVal path As String)
            Dim Gadget As TrayGadgetInterface.TrayGadget = LoadGadget(path)
            If Gadget IsNot Nothing Then
                Gadget.LoadFunction()
                Me.TrayGadgetStack.Children.Add(Gadget.IconImage)
            End If
    End Function
    
    Private Function LoadGadget(ByVal LoadPath As String) As TrayGadgetInterface.TrayGadget
            Dim NewGadget As TrayGadgetInterface.TrayGadget
            Try
            Dim PlugInAssembly As Reflection.Assembly = Reflection.Assembly.LoadFrom(LoadPath)
            Dim Types() As Type
            Dim FoundInterface As Type
            Types = PlugInAssembly.GetTypes
            For Each GadgetType As Type In Types
                FoundInterface = GadgetType.GetInterface("TrayGadgetInterface.TrayGadget")
                If FoundInterface IsNot Nothing Then
                    NewGadget = DirectCast(PlugInAssembly.CreateInstance(GadgetType.FullName), TrayGadgetInterface.TrayGadget)
                End If
            Next
            Catch ex As Exception
                 'handle exceptions here
            End Try
            Return NewGadget
        End Function
    
    Thanks a lot
    Attila Fogel
    • Moved byJeff ShanMSFTTuesday, November 03, 2009 10:17 AMwpf question (From:Visual Basic General)
    •  

Answers

  • Wednesday, November 04, 2009 10:48 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Attila Fogel,

    The basic scenario here is that you need to refresh the image notification when the weather changes, based on my experience, when you change the data(weather), you raise a event to inform the image to update its source, doing this way, the image notification will be updated according to the weather.

    Thanks.
    Sincerely.

    Jim Zhou -MSFT

All Replies

  • Monday, November 02, 2009 12:03 AMkaymaf Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Try WPF forum http://social.msdn.microsoft.com/Forums/en-US/wpf/threads

    kaymaf
    If that what you want, take it. If not, ignored it and no complain
  • Wednesday, November 04, 2009 10:48 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Attila Fogel,

    The basic scenario here is that you need to refresh the image notification when the weather changes, based on my experience, when you change the data(weather), you raise a event to inform the image to update its source, doing this way, the image notification will be updated according to the weather.

    Thanks.
    Sincerely.

    Jim Zhou -MSFT
  • Friday, November 06, 2009 9:42 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Attila Fogel,

    Have you tried the suggestion I posted above?Did that make sense in your scenario? If you still have any issues with this, please feel free to feed back.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
  • Sunday, November 22, 2009 8:35 PMAttila Fogel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hey,

    sorry for not posting till now, but yeah, i figured it out by myself, and what you mention is exactly what i'm doing. :) thnx
    Attila Fogel
  • Monday, November 23, 2009 9:02 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Attila Fogel,

    Glad to see that your issue was resolved yet, any questions regarding WPF, please feel free to post on this forum, then we can discuss it.

    Thanks.
    Sincerely.

    Jim Zhou -MSFT