Programming with .NET - Generalhttp://forums.silverlight.net//17.aspx/1?Programming+with+NET+GeneralGeneral discussions around authoring Silverlight .NET applications.Mon, 01 Jan 0001 00:00:00 -05001713699http://forums.silverlight.net//p/4604/13699.aspx/1?Using+Downloader+to+load+a+packaged+XAMLUsing Downloader to load a packaged XAML <p>I've been reading about loading packaged XAML files to load a&nbsp;UI Element (Canvas object) from a .zip file, but all of the examples have been in javascript.&nbsp; How would I approach this&nbsp;in C#?</p> <p>I am trying to do something like this:</p> <font size="2">downloader.Open(</font><font color="#a31515" size="2">&quot;GET&quot;</font><font size="2">, </font><font color="#0000ff" size="2">new</font><font size="2"> System.</font><font color="#2b91af" size="2">Uri</font><font size="2">(</font><font color="#a31515" size="2">&quot;clock.zip&quot;</font><font size="2">, </font><font color="#2b91af" size="2">UriKind</font><font size="2">.Relative));<br> downloader.Completed &#43;= </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">EventHandler</font><font size="2">(downloader_Completed);<br> downloader.Send();</font><font size="2"> <p></font>Followed by:</p> <p>protected void downloader_Completed(object sender, EventArgs e)<br> {<br> &nbsp;&nbsp;&nbsp; /*some object*/.createFromXAMLDownloader(&quot;Page.xaml&quot;);<br> }</p> <p>&nbsp;</p> <p>Problems:</p> <p>1) I don't know what object to call createFromXAMLDownloader() on.&nbsp; Looking at the javascript version is not helpful here.&nbsp;As a possible workaround for now I am using XamlReader.Load(downloader.GetResponseText(&quot;Page.xaml&quot;));&nbsp; </p> <p>2) Before it gets to my downloader_Completed, I get an error: AG_E_RUNTIME_MANAGED_ASSEMBLY_DOWNLOAD</p> <p>3) How should I package the DLL that goes with the .xaml page?&nbsp; I have it inside the .zip file in&nbsp;a ClientBin directory.&nbsp; Once I get this working, will the XAML know to pick up the DLL from the .zip in the appropriate location?</p> <p>Any thoughts, or relevant articles/posts I can read?</p> <p>Thanks!</p> <p>-Jason-</p> 2007-09-10T22:14:15-04:0013708http://forums.silverlight.net//p/4604/13708.aspx/1?Re+Using+Downloader+to+load+a+packaged+XAMLRe: Using Downloader to load a packaged XAML <ol> <li> <div>createFromXAMLDownloader() does not exist in 1.1.&nbsp; If your control inherits from Control, you can use this.InitializeFromXaml and pass it the string that you get from the Downloader.</div> </li><li> <div>You need to reference your dll in your application so that it is already in the /ClientBin directory when you try to initialize the control from the XAML.</div> </li><li> <div>I think #2 answers #3.</div> </li></ol> <p>I hope this helps.</p> 2007-09-10T23:19:10-04:0013709http://forums.silverlight.net//p/4604/13709.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>So there is no way to dynamically load and create a silverlight control from within another?</p> <p>That can't be right...</p> 2007-09-10T23:25:41-04:0013710http://forums.silverlight.net//p/4604/13710.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>Wo!&nbsp; I think there's a misunderstanding here.</p> <p>You can, thankfully, create and load a silverlight control within another.&nbsp; How it's done is by having your SL app reference the control's dll.&nbsp; Then, when the application dll is downloaded, it brings the control's dll with it and knows, when it's compiled, the control's dll.&nbsp; I suggest looking at the SilverlightUIControls sample that comes with the SDK for the best way to create controls.</p> <p>If this answer is off base from what you're needing, I apologize.</p> 2007-09-10T23:38:45-04:0013712http://forums.silverlight.net//p/4604/13712.aspx/1?Re+Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Re: Using Downloader to load a packaged XAML <p>Hmmm...the keyword for me is 'dynamically'.&nbsp; As in, not knowing ahead of time what control might be loaded, or which DLLs would need to be referenced.</p> <p>I'm thinking of something like the Flash paradigm where one MovieClip can load another.</p> <p>In my case, it would not be possible for my app to have a priori knowledge of what kind of control might be instantiated.</p> <p>This is why I thought of the .zip as a complete, self-referential package.&nbsp; If not, is there a better way to achieve what I am trying?</p> 2007-09-10T23:49:15-04:0013715http://forums.silverlight.net//p/4604/13715.aspx/1?Re+Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Re: Using Downloader to load a packaged XAML <p>Well, I think this question has gone beyond my level knowledge, so I hope someone else can be of more help.</p> 2007-09-11T00:00:17-04:0020476http://forums.silverlight.net//p/4604/20476.aspx/1?Re+Using+Downloader+to+load+a+packaged+XAMLRe: Using Downloader to load a packaged XAML <p>i have the same problem: i want to lunch the loaded Xaml by using C#, i couldn't find the InitializeFromXaml function, can you give me more details please,</p> <p>thanks.</p> 2007-11-12T10:17:44-05:0020479http://forums.silverlight.net//p/4604/20479.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p><font size="2"><font color="#0000ff" size="2"></font><font size="2"></font></font>&nbsp;</p> <p><font size="2">//the zip file contains&nbsp;all the xaml files needed&nbsp;</font></p> <font size="2">&nbsp;<font color="#0000ff" size="2">void</font><font size="2"> DownloadXaml(</font><font color="#0000ff" size="2">object</font><font size="2"> sender, </font><font color="#2b91af" size="2">EventArgs</font><font size="2"> args) //called when xaml zip is downloaded</font></font><font size="2"> </font><font size="2"> <p>{</p> <p></font><font color="#2b91af" size="2">Downloader</font><font size="2"> downloader = </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">Downloader</font><font size="2">();</font></p> <font size="2">downloader.Completed &#43;= </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">EventHandler</font><font size="2">(DownloadComplete);</font><font size="2">downloader.Open(</font><font color="#a31515" size="2">&quot;GET&quot;</font><font size="2">, </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#2b91af" size="2">Uri</font><font size="2">(</font><font color="#a31515" size="2">&quot;xyz.zip&quot;</font><font size="2">, </font><font color="#2b91af" size="2">UriKind</font><font size="2">.Relative));</font><font size="2"> <p>downloader.Send();</p> <p>}</p> <p>Downloader allXaml;</p> </font><font size="2"><font color="#0000ff" size="2">void</font><font size="2"> DownloadComplete(</font><font color="#0000ff" size="2">object</font><font size="2"> sender, </font><font color="#2b91af" size="2">EventArgs</font><font size="2"> e)</font></font><font size="2"> <p>{</p> <p>allXaml = sender </font><font color="#0000ff" size="2">as</font><font size="2"> </font><font color="#2b91af" size="2">Downloader</font><font size="2">;</font></p> <font size="2">setXaml();</font><font size="2"> // you can actually call it from whenever you want to add any xaml in your application <p></font><font size="2">}</font></p> <p><font size="2">void setXaml()</font></p> <p><font size="2">{</font></p> <font size="2"></font><font color="#0000ff" size="2">string</font><font size="2"> xamlName =&nbsp;name of the xaml file&nbsp;</font><font size="2">;</font><font size="2"> <p></font><font color="#2b91af" size="2">String</font><font size="2"> xamlFragment = allAnimation.AllXaml.GetResponseText(xamlName);</p> </font><font color="#2b91af" size="2">Canvas</font><font size="2"> root = canvas.FindName(</font><font color="#a31515" size="2">&quot;Page&quot;</font><font size="2">) </font><font color="#0000ff" size="2">as</font><font size="2"> </font><font color="#2b91af" size="2">Canvas</font><font size="2">;</font><font size="2"> root.Children.Insert(1, (</font><font color="#2b91af" size="2">Visual</font><font size="2">)</font><font color="#2b91af" size="2">XamlReader</font><font size="2">.Load(xamlFragment));</font><font size="2"> <p></font><font size="2">}</font></p> <p><font size="2"></font>&nbsp;</p> <p><font size="2">I have done like this.</p> </font> 2007-11-12T10:42:17-05:0020496http://forums.silverlight.net//p/4604/20496.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>thanks,</p> <p>while waiting for the answer, i tried to find a solution and i did it like this:</p> <font color="#0000ff" size="2"> <p>this</font><font size="2">.Children.Add((</font><font color="#2b91af" size="2">Visual</font><font size="2">)</font><font color="#2b91af" size="2">XamlReader</font><font size="2">.Load(xamlText));</p> </font> <p>with <font color="#0000ff" size="2">this </font>references the Page.xaml.cs,</p> <p>can you please tell me the difference between using Add and using Insert, and how to choose the index value ?</p> <p>thanks,</p> <p>Assem.</p> 2007-11-12T12:53:26-05:0020501http://forums.silverlight.net//p/4604/20501.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>another thing, what if i want to load an entire application with DLL, like Jason said, how can i do that ?<br> </p> 2007-11-12T13:52:20-05:0020504http://forums.silverlight.net//p/4604/20504.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p></p> <blockquote><span class="icon-blockquote"></span> <h4>bayahi</h4> <p>another thing, what if i want to load an entire application with DLL, like Jason said, how can i do that ?<br> </p> </blockquote> &nbsp; <p>I wrote up an <a class="" href="http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2007/11/09/Silverlight-Assembly-Preloader-in-Managed-Code.aspx" target="_blank"> example of how to do this</a> 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.</p> <p>Hope that helps.</p> <p>Pete</p> 2007-11-12T14:35:46-05:0020519http://forums.silverlight.net//p/4604/20519.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>&nbsp;thanks <strong>Pete,</strong></p> <p>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 <u>really new</u> with Silverlight so take me easy[:&#36;].</p> <p>Well, what i have understood from your application is that the main steps to run an external application with DLL are:</p> <p>1) download the DLL file with downloader.</p> <p>2) load the DLL file with Assembly.Load</p> <p>3) Create an element with the loaded DLL.</p> <p>4) Add the element to the container.</p> <p>&nbsp;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.</p> <p>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 ? </p> <p>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 ?</p> <p>thanks again,</p> <p>Assem.<br> &nbsp;</p> 2007-11-12T17:37:17-05:0020520http://forums.silverlight.net//p/4604/20520.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>Yes, the steps you have are correct.</p> <p>The DLL &quot;LibOne&quot; 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 :)</p> <p>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 <a class="" href="http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2007/10/16/UserControls-as-Screens-in-Silverlight-1.1-_2D00_-Part-2-of-2-.aspx" target="_blank"> UserControls as Screens sample</a>, you can see more on creating usercontrols. There are also quickstarts in the &quot;getting started&quot; section here.</p> <p>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.</p> <p>For the more concrete example:</p> <p>I have a loader application that occupies the user while I download all the other application bits (screens,&nbsp;images, xaml, etc.). The main application itself is all packaged up behind the usercontrol in the downloaded assembly.</p> <p>Another one:</p> <p>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.</p> <p>Hope that helps. If not, let me know what else I can do to help out.&nbsp;</p> <p>Pete</p> 2007-11-12T17:51:32-05:0020562http://forums.silverlight.net//p/4604/20562.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>hi Pete,</p> <p>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].</p> <p>thanks,</p> <p>Assem.</p> 2007-11-13T07:30:38-05:0020563http://forums.silverlight.net//p/4604/20563.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>hi Pete,</p> <p>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].</p> <p>thanks,</p> 2007-11-13T07:31:08-05:0020890http://forums.silverlight.net//p/4604/20890.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>&nbsp;Hi, it's me again.</p> <p>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.</p> <p>I noticed also, that your UserControlPlugin is implementing an interace IPlugin, then in the instanciation of the Assembly you used this interface:</p> <p>IPlugin instance = (IPlugin)Activator.CreateInstance(type);</p> <p>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:</p> <p>Exception has been thrown by the target of an invocation.</p> <p>May be this not the write message but that is what i get.</p> <p>thanks,&nbsp;</p> <p><br> &nbsp;</p> 2007-11-15T16:38:57-05:0020892http://forums.silverlight.net//p/4604/20892.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>I cover some or most of that in the <a class="" href="http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2007/11/09/Silverlight-Assembly-Preloader-in-Managed-Code.aspx" target="_blank"> article I wrote on the topic</a>.</p> <p>Yes, the &nbsp;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.</p> <p>I use an interface so that&nbsp;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:</p> <p><em>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.</em></p> <p>Activator.CreatInstance is a <a class="" href="http://msdn2.microsoft.com/en-us/library/system.reflection.aspx" target="_blank"> reflection</a> method for creating&nbsp;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.</p> <p>As to your exception, take a look in your constructor for your usercontrol. Chances are you have an error there, probably in the xaml.</p> <p>Hope that helps</p> <p>Pete</p> 2007-11-15T16:58:20-05:0020896http://forums.silverlight.net//p/4604/20896.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>&nbsp;so, is't possible to instanciate a UserControl that does not implements an interface and if it's, how ?<br> </p> 2007-11-15T17:34:04-05:0020913http://forums.silverlight.net//p/4604/20913.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>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.</p> <p>In general, however, whenever you're trying to create anything that smells of a plugin-type architecture, you're better off using an interface.</p> <p>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.</p> <p>Pete</p> 2007-11-15T19:15:38-05:0020982http://forums.silverlight.net//p/4604/20982.aspx/1?Re+Re+Using+Downloader+to+load+a+packaged+XAMLRe: Re: Using Downloader to load a packaged XAML <p>&nbsp;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.</p> <p>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.<br> </p> 2007-11-16T09:53:14-05:00