Karthik Balasubramanian
Karthik Balasubramanian

Reputation: 1157

Different XAP files in Silverlight - Sharing context

I have a silverlight application which I recently split them into separate xap files.

The intention is this. There is xap file 1 which takes a few inputs from the user persists it to the database. And when the user clicks a button on this UI (from xap file 1), it loads xap file 2, which asks for more inputs and saves it to the database.

I was able to invoke xap file 2 from xap file 1, but the UI doesn't seem to be able to get the information from the database (the UI shows what was persisted by the previous UI), nor it is able to persist the user's input. I realize that the xap file 2 somehow needs to told somehow where and how to persist.

This is how I load the xap file 2

        WebClient wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCompleted);
        Uri uri = new Uri("xapfile2.xap", UriKind.Relative);
        wc.OpenReadAsync(uri);


 private void OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        Uri uri = new Uri("xapfile2.dll", UriKind.Relative);

        StreamResourceInfo resource_stream = App.GetResourceStream(new StreamResourceInfo(e.Result, null), uri);

        AssemblyPart part = new AssemblyPart();
        Assembly assembly = part.Load(resource_stream.Stream);
        UIElement control = assembly.CreateInstance("xapfile2.Window") as UIElement;
        if (control != null)
        {
            RadWindow window = (RadWindow)control;
            window.ShowDialog();

        }
    }

(All of this UI was working before I split them into separate xap's. We had good reasons to splitting them)

Any help would be greatly appreciated

UPDATE

I tried following the example link and was able to launch the second UI from the separate xap file with all the information from the database.

Now I seem to have hit a new problem. When I do a cancel on the second UI, do I have to unload the second xap calling removeXap from the catalog? When I do so I get Composition remains unchanged error.

1) Change in exports prevented by non-recomposable import 'xapfile2.Views.ViewModel (ContractName="MyViewModel")' on part 'xapfile2.Views.MyView'. Any ideas?.

Thanks K

Upvotes: 0

Views: 1852

Answers (3)

Karthik Balasubramanian
Karthik Balasubramanian

Reputation: 1157

After a lot of struggle I found out that I had redundant assemblies in the xap files. Apparently there should not be a conflict in assemblies. Setting CopyLocal = False on the assemblies required for the second xap seemed to have solved the issue.

Thanks guys for your tips

K

Upvotes: 0

foson
foson

Reputation: 10227

Having seperate XAP files doesn't create any boundries -- when the 2nd XAP is loaded, its types are loaded into the same AppDomain. Seems like you need some kind of common messaging bus between classes in your Xap1 and Xap2. You can use an Event Aggregator. Many frameworks have one or you can use your own -- Build it (and custom event types) in a dll referenced by both xap's. I think I've used http://www.keith-woods.com/Blog/post/Rx-Event-Aggregator.aspx

Upvotes: 0

Jeremiah
Jeremiah

Reputation: 5514

You should use a framework to bring in your XAP file. You're reinventing the wheel.

My suggestion is to use MEF, a framework created by Microsoft to do exactly what you are creating.

MEF has a concept of a deployment catalog, which downloads xap files and loads them. Then, using patterns like interfaces and contracts, you can create objects, show them on the screen and take more input.

See this example: http://codebetter.com/glennblock/2010/03/08/building-hello-mef-part-iv-deploymentcatalog/

Good Luck!

Upvotes: 1

Related Questions