Intersoft ClientUI 8 > ClientUI Fundamentals > Application Framework Overview > Application Framework How-to Topics > How-to: Create and Load an Instance of ApplicationPackage Asynchronously |
This example shows how to create an object instance of the main type associated to an ApplicationPackage using asynchronous pipeline, and then add the object to the visual tree in the callback method using code.
Once an ApplicationPackage has been downloaded, you can create an instance of object which type is specified in the MainType property. The main type and possibly other properties are automatically parsed from the XML application metadata (SAFMetadata.xml) of the particular application, with note that EnableMetadataDiscovery is set to True while performing the download. For more information how to download an ApplicationPackage using API, see How-to: Download an External ApplicationPackage.
The type used as the main type of an application package should be a type that can be injected as a visual child in the Silverlight and WPF visual tree, which commonly derives from FrameworkElement class. |
The following example shows how to get the application using GetApplication method, and then load the application into the current domain context using the Load method, and finally create an instance asynchronously using CreateInstanceAsync method then assign the object instance to the visual tree of the Border.
C# |
Copy Code
|
---|---|
ApplicationPackage app = UXShell.Current.GetApplication("ExternalClientUIApp1"); if (app != null) { // load the application to the current domain context app.Load(); if (app.IsLoaded) { app.CreateInstanceAsync( (resultCallBack) => { InstanceLoaderAsyncResult asyncResult = (InstanceLoaderAsyncResult)resultCallBack; if (asyncResult.IsCompleted) { if (asyncResult.Instance == null) throw new Exception("Unsupported instance type"); else if (asyncResult.Error != null) throw asyncResult.Error; Container.Child = asyncResult.Instance as FrameworkElement; } } , null ); } } |
The above example presumes that a ExternalClientUIApp1.xap file existed in the ClientBin of the Web project, which should be the same folder where the root application existed.
To create a new ClientUI Application, you use the ClientUI project templates installed in Visual Studio 2010. For more information, see Introduction to ClientUI Project Templates.