(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Silverlight - Dynamically Loading an Assembly

(2 votes)
5 comments   /   aggregated from Mike Taulty's Blog on Apr 14, 2008   /   original article
Categories:   General

Just something that someone asked me about today so sharing here. Imagine that you want to define a portion of your Silverlight application but leave some other piece to run time.

For example - a calculation engine or perhaps something more complicated like a piece of UI with interaction.

You can dynamically load up a .NET assembly in Silverlight at runtime, pull out known types from it and interact with them just like you would in any other .NET application.

I made my example as simple as possible. I defined a core UI which looks like this;

image

The idea is that the red bit is the "host" and the yellow bit is for the plug-in editor. I define an interface to live between my host and my plug-in editor;

  public class TextEventArgs : EventArgs
  {
    public string TheText { get; set; }
  }
  public interface IEditUI
  {
    UIElement GetControls();
    void SetText(string text);
    event EventHandler TextChanged;
  }

 

and then an implementation of that which I put into an assembly called Implementation which is not referenced by my Silverlight application.

  public class Editor : IEditUI
  {
    public Editor()
    {
      textBox = new TextBox();
    }
    public UIElement GetControls()
    {
      StackPanel stackPanel = new StackPanel();
      stackPanel.Margin = new Thickness(5);
      stackPanel.Orientation = Orientation.Horizontal;

      textBox = new TextBox();
      textBox.Width = 100;
      Button button = new Button();
      button.Content = "Click Me";
      button.Click += OnButtonClick;

      stackPanel.Children.Add(textBox);
      stackPanel.Children.Add(button);

      return (stackPanel);
    }
    void OnButtonClick(object sender, RoutedEventArgs e)
    {
      if (TextChanged != null)
      {
        TextChanged(this, new TextEventArgs() { TheText = textBox.Text });
      }      
    }
    public void SetText(string text)
    {
      textBox.Text = text;
    }
    private TextBox textBox;   
    public event EventHandler TextChanged;
  }

 

Then, at runtime I can write code which will load this up and make use of it as in;

  public partial class Page : UserControl
  {
    public Page()
    {
      InitializeComponent();
    }
    void OnLoadDynamicEditor(object sender, EventArgs args)
    {
      WebClient client = new WebClient();
      client.OpenReadCompleted += new OpenReadCompletedEventHandler(OnAssemblyOpened);
      client.OpenReadAsync(new Uri("Implementation.dll", UriKind.Relative));
    }
    void OnAssemblyOpened(object sender, OpenReadCompletedEventArgs e)
    {
      AssemblyPart assemblyPart = new AssemblyPart();
      Assembly assembly = assemblyPart.Load(e.Result);

      editor = assembly.CreateInstance("Implementation.Editor") as IEditUI;

      if (editor != null)
      {
        hostGrid.Children.Add(editor.GetControls());
        editor.TextChanged += OnEditorTextChanged;
      }     
    }
    void OnSendToEditor(object sender, EventArgs args)
    {
      if (editor != null)
      {
        editor.SetText(txtHost.Text);
      }
    }
    void OnEditorTextChanged(object sender, TextEventArgs e)
    {
      txtHost.Text = e.TheText;
    }
    IEditUI editor;
  }

 

And now I have a ( very basic! ) but hosted UI inside of another piece of UI. I put the project file here for download. Note: The Implementation.dll assembly needs to be manually copied to the ClientBin folder of the website project in order for this to work.


Subscribe

Comments

  • -_-

    RE: Silverlight - Dynamically Loading an Assembly


    posted by ed on Jul 16, 2008 11:01

    Thanks. Very helpful.

    Ed.

  • -_-

    RE: Silverlight - Dynamically Loading an Assembly


    posted by Jim Bowen on Jul 30, 2008 09:21

    Great tips!

    It is key for architecture in multiple assemblies. For example: The main app has navigation that loads page-like controls and user controls and custom controls from different libs. The extra assemblies can be sent down as needed, or in a background thread etc. 

    However, it looks like a lot of code. Can it be streamlined? Can it be done declaratively?

    Jim

     

  • -_-

    RE: Silverlight - Dynamically Loading an Assembly


    posted by Matt on Apr 08, 2009 07:27
    I have a doubt here. Can an assembly be loaded from some site where it has been uploaded?
  • -_-

    RE: Silverlight - Dynamically Loading an Assembly


    posted by Shuaiho@live.com on Sep 10, 2010 09:15

    I have a problem!

    The assembly have referense WCF!

    So what can i do!

    Thanks

    My Email:shuaiho@live.com

  • -_-

    RE: Silverlight - Dynamically Loading an Assembly


    posted by shahnawaz on Dec 21, 2010 15:06

    Sir

    can you upload a sample application

    thanks

    ur_saajan@yahoo.com

Add Comment

Login to comment:
  *      *