another thing, what if i want to load an entire application with DLL, like Jason said, how can i do that ?
I wrote up an
example of how to do this in my blog. In that example, it is a preloader that downloads and instantiates one or more controls within a DLL on the server. The example can be extended to handle additional DLLs downloaded all at once, although the downloader
logic would need to be enhanced to allow that.
Hope that helps.
Pete
Developer Community Program Manager - XAML, WPF, Silverlight, .NETMF/Gadgeteer
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
i took a look on your application and i downloaded it, well before begin asking you somes questions i want you to know that i am
really new with Silverlight so take me easy[:$].
Well, what i have understood from your application is that the main steps to run an external application with DLL are:
1) download the DLL file with downloader.
2) load the DLL file with Assembly.Load
3) Create an element with the loaded DLL.
4) Add the element to the container.
I hope what i have said is write, if it's can you please give me a more simple exemple to better understand the logic because your application is generics.
I also found that the DLL loaded in your application is already included in the project, in my case i want to load an external application (with xaml, Dll) and run it in my application (like loader in Flash), is that possible ?
Finaly, in a dowleded zip file, may i access to file that are not directly putted in the root for exemple my zip file is App.zip and i want to access to the file App/doc/app.xaml ?
The DLL "LibOne" that I use doesn't have to be in the same solution, I just happen to have it in there. The reason the DLL appears to be in the project is because I set the output directory of the compile to be the main directory of the silverlight app.
That's just laziness on my part :)
So, to answer your question, yes, you can have a DLL that you separately compile and separately place in the download directory. Note that it has to be a usercontrol, not a whole application with a root page. That's really just an implementation detail,
as you can do just about everything in a usercontrol that you could in a page (integrating with page javascript by exposing [Scriptable] elements is the one area that is more difficult - Shawn posted about that elsewhere in these forums). If you check out
my
UserControls as Screens sample, you can see more on creating usercontrols. There are also quickstarts in the "getting started" section here.
I didn't use a zip file for the assembly download - just a straight DLL. I haven't tried your example with subfolders inside a zip file. Maybe someone else here can address that part.
For the more concrete example:
I have a loader application that occupies the user while I download all the other application bits (screens, images, xaml, etc.). The main application itself is all packaged up behind the usercontrol in the downloaded assembly.
Another one:
I have a main application that hosts a number of small sub apps. Think a suite of games with 10-15 games. When the user clicks on the icon on the main app, I download the appropriate game using the methods described above. If they switch to another game,
and then come back to this one, no problem, as the game was already downloaded.
Hope that helps. If not, let me know what else I can do to help out.
Pete
Developer Community Program Manager - XAML, WPF, Silverlight, .NETMF/Gadgeteer
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
About the PeteBrown.SilverlightAppDownload, well, i was able to lunch it and even to load the UserControlPlugin in other project. What i noticed is that if the DLL that you want to load (Assembly.Load) is not in the project directory the Load will not work
even if you download it.
I noticed also, that your UserControlPlugin is implementing an interace IPlugin, then in the instanciation of the Assembly you used this interface:
can you please explain this line and why using an interface and is't possible to instanciate a UserControl that does not implements an interface and if it's, how. I also tried to create a UserControl and to load it but i always get an error when trying to
instanciate it even if i use an interface, i get this error:
Exception has been thrown by the target of an invocation.
May be this not the write message but that is what i get.
Yes, the DLLs must all be in the main project directory. This could be changed to all pull from a different folder, but they must all be together.
I use an interface so that 1. I can inspect the DLL to see what plugins are exposed and 2. the plugin itself doesn't need to be a usercontrol. The interface has a single method (in this example) that simply returns the main object I'll be displaying on the
screen. Here's the excerpt:
The preloader takes a fully qualified assembly name as well as a URL from which to download the assembly. Under the hood, it uses the downloader object to grab the assembly and reflection to load the assembly into memory, iterate its contents, and return
back references to all IPlugin-implementing classes in the assembly. The calling code then calls IPlugin.RootElement to get the instance of the root element (a usercontrol) from the plugin assembly which it then adds to a canvas on the main page. While I confine
the plugin to a small part of the main canvas, you could, and likely would, have it completely overtake the canvas area.
Activator.CreatInstance is a
reflection method for creating an instance of a type. Reflection is not unique to Silverlight, so I was able to use techniques I have used on other .NET projects here.
As to your exception, take a look in your constructor for your usercontrol. Chances are you have an error there, probably in the xaml.
Hope that helps
Pete
Developer Community Program Manager - XAML, WPF, Silverlight, .NETMF/Gadgeteer
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
Sure, but you would need to write your own detected and instantiation code. I assume you would look for a specific control name and instantiate it using that type or using the base Control type. You would either need to know the types ahead of type, downcast
to Control, or use an interface like I did.
In general, however, whenever you're trying to create anything that smells of a plugin-type architecture, you're better off using an interface.
So, to answer your question: using my code directly, it isn't possible. Using my code as a starting point: sure, you just need to make the modifications. I'd suggest starting up a new project and taking over only the bits that make sense for you.
Pete
Developer Community Program Manager - XAML, WPF, Silverlight, .NETMF/Gadgeteer
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
May be i am wrong but i think that you do not need to Download the assembly with a downloader before loading it with Assembly.Load. Well, when i noticed that the assembly must be in the project folder, i had some doubt that the loader is getting the assembly
directly from the project directory and not because it was downloaded. I tried to test that in a new project where i putted my DLL in the project directory then i tried to load it directly with Assembly.Load and i added it to the main Canvas and this work
fine.
I'm affraid that what i said is wrong because in all the article and codes (including Pete and Damoxc codes) i found that the loading is done after downloading the DLL, so could you check out please.
Psychlist1972
Contributor
6802 Points
1079 Posts
Microsoft
Moderator
Re: Re: Using Downloader to load a packaged XAML
Nov 12, 2007 02:35 PM | LINK
I wrote up an example of how to do this in my blog. In that example, it is a preloader that downloads and instantiates one or more controls within a DLL on the server. The example can be extended to handle additional DLLs downloaded all at once, although the downloader logic would need to be enhanced to allow that.
Hope that helps.
Pete
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
bayahi
Member
22 Points
12 Posts
Re: Re: Using Downloader to load a packaged XAML
Nov 12, 2007 05:37 PM | LINK
thanks Pete,
i took a look on your application and i downloaded it, well before begin asking you somes questions i want you to know that i am really new with Silverlight so take me easy[:$].
Well, what i have understood from your application is that the main steps to run an external application with DLL are:
1) download the DLL file with downloader.
2) load the DLL file with Assembly.Load
3) Create an element with the loaded DLL.
4) Add the element to the container.
I hope what i have said is write, if it's can you please give me a more simple exemple to better understand the logic because your application is generics.
I also found that the DLL loaded in your application is already included in the project, in my case i want to load an external application (with xaml, Dll) and run it in my application (like loader in Flash), is that possible ?
Finaly, in a dowleded zip file, may i access to file that are not directly putted in the root for exemple my zip file is App.zip and i want to access to the file App/doc/app.xaml ?
thanks again,
Assem.
Psychlist1972
Contributor
6802 Points
1079 Posts
Microsoft
Moderator
Re: Re: Using Downloader to load a packaged XAML
Nov 12, 2007 05:51 PM | LINK
Yes, the steps you have are correct.
The DLL "LibOne" that I use doesn't have to be in the same solution, I just happen to have it in there. The reason the DLL appears to be in the project is because I set the output directory of the compile to be the main directory of the silverlight app. That's just laziness on my part :)
So, to answer your question, yes, you can have a DLL that you separately compile and separately place in the download directory. Note that it has to be a usercontrol, not a whole application with a root page. That's really just an implementation detail, as you can do just about everything in a usercontrol that you could in a page (integrating with page javascript by exposing [Scriptable] elements is the one area that is more difficult - Shawn posted about that elsewhere in these forums). If you check out my UserControls as Screens sample, you can see more on creating usercontrols. There are also quickstarts in the "getting started" section here.
I didn't use a zip file for the assembly download - just a straight DLL. I haven't tried your example with subfolders inside a zip file. Maybe someone else here can address that part.
For the more concrete example:
I have a loader application that occupies the user while I download all the other application bits (screens, images, xaml, etc.). The main application itself is all packaged up behind the usercontrol in the downloaded assembly.
Another one:
I have a main application that hosts a number of small sub apps. Think a suite of games with 10-15 games. When the user clicks on the icon on the main app, I download the appropriate game using the methods described above. If they switch to another game, and then come back to this one, no problem, as the game was already downloaded.
Hope that helps. If not, let me know what else I can do to help out.
Pete
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
bayahi
Member
22 Points
12 Posts
Re: Re: Using Downloader to load a packaged XAML
Nov 13, 2007 07:30 AM | LINK
hi Pete,
i just want to thank you for your answers, they were very helpfull. For now it's all fine but if i get any problem i will bother you again [:D].
thanks,
Assem.
bayahi
Member
22 Points
12 Posts
Re: Re: Using Downloader to load a packaged XAML
Nov 13, 2007 07:31 AM | LINK
hi Pete,
i just want to thank you for your answers, they were very helpfull. For now it's all fine but if i get any problem i will bother you again [:D].
thanks,
bayahi
Member
22 Points
12 Posts
Re: Re: Using Downloader to load a packaged XAML
Nov 15, 2007 04:38 PM | LINK
Hi, it's me again.
About the PeteBrown.SilverlightAppDownload, well, i was able to lunch it and even to load the UserControlPlugin in other project. What i noticed is that if the DLL that you want to load (Assembly.Load) is not in the project directory the Load will not work even if you download it.
I noticed also, that your UserControlPlugin is implementing an interace IPlugin, then in the instanciation of the Assembly you used this interface:
IPlugin instance = (IPlugin)Activator.CreateInstance(type);
can you please explain this line and why using an interface and is't possible to instanciate a UserControl that does not implements an interface and if it's, how. I also tried to create a UserControl and to load it but i always get an error when trying to instanciate it even if i use an interface, i get this error:
Exception has been thrown by the target of an invocation.
May be this not the write message but that is what i get.
thanks,
Psychlist1972
Contributor
6802 Points
1079 Posts
Microsoft
Moderator
Re: Re: Using Downloader to load a packaged XAML
Nov 15, 2007 04:58 PM | LINK
I cover some or most of that in the article I wrote on the topic.
Yes, the DLLs must all be in the main project directory. This could be changed to all pull from a different folder, but they must all be together.
I use an interface so that 1. I can inspect the DLL to see what plugins are exposed and 2. the plugin itself doesn't need to be a usercontrol. The interface has a single method (in this example) that simply returns the main object I'll be displaying on the screen. Here's the excerpt:
The preloader takes a fully qualified assembly name as well as a URL from which to download the assembly. Under the hood, it uses the downloader object to grab the assembly and reflection to load the assembly into memory, iterate its contents, and return back references to all IPlugin-implementing classes in the assembly. The calling code then calls IPlugin.RootElement to get the instance of the root element (a usercontrol) from the plugin assembly which it then adds to a canvas on the main page. While I confine the plugin to a small part of the main canvas, you could, and likely would, have it completely overtake the canvas area.
Activator.CreatInstance is a reflection method for creating an instance of a type. Reflection is not unique to Silverlight, so I was able to use techniques I have used on other .NET projects here.
As to your exception, take a look in your constructor for your usercontrol. Chances are you have an error there, probably in the xaml.
Hope that helps
Pete
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
bayahi
Member
22 Points
12 Posts
Re: Re: Using Downloader to load a packaged XAML
Nov 15, 2007 05:34 PM | LINK
so, is't possible to instanciate a UserControl that does not implements an interface and if it's, how ?
Psychlist1972
Contributor
6802 Points
1079 Posts
Microsoft
Moderator
Re: Re: Using Downloader to load a packaged XAML
Nov 15, 2007 07:15 PM | LINK
Sure, but you would need to write your own detected and instantiation code. I assume you would look for a specific control name and instantiate it using that type or using the base Control type. You would either need to know the types ahead of type, downcast to Control, or use an interface like I did.
In general, however, whenever you're trying to create anything that smells of a plugin-type architecture, you're better off using an interface.
So, to answer your question: using my code directly, it isn't possible. Using my code as a starting point: sure, you just need to make the modifications. I'd suggest starting up a new project and taking over only the bits that make sense for you.
Pete
10rem.net - Pete Brown's site and blog | twitter: @pete_brown
I work for the Developer Guidance group in Microsoft. Opinions are my own.
bayahi
Member
22 Points
12 Posts
Re: Re: Using Downloader to load a packaged XAML
Nov 16, 2007 09:53 AM | LINK
May be i am wrong but i think that you do not need to Download the assembly with a downloader before loading it with Assembly.Load. Well, when i noticed that the assembly must be in the project folder, i had some doubt that the loader is getting the assembly directly from the project directory and not because it was downloaded. I tried to test that in a new project where i putted my DLL in the project directory then i tried to load it directly with Assembly.Load and i added it to the main Canvas and this work fine.
I'm affraid that what i said is wrong because in all the article and codes (including Pete and Damoxc codes) i found that the loading is done after downloading the DLL, so could you check out please.