Skip to main content

Microsoft Silverlight

Answered Question How to reference on demand assemblies.RSS Feed

(0)

silidev
silidev

Member

Member

141 points

92 Posts

How to reference on demand assemblies.

 Hi,

I have 2 Silverlight applications built as 2 different xap's in a single Silverlight web application.

Say SLAPP1 and SLAPP2 and my web project is SLAPP1.web.

In Page.xaml.cs of SLAPP1 i have written code to make a webclient call and download SLAPP2 .zap and assemblies in that zap.

Now in a different class say Page2.xaml.cs in SLAPP1 if i have to just reference assembly downloaded from SLAPP2.zap earlier do i have to make a webclient call again?

Why below simply does not work in Page2.xaml.cs?

                    StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("SLAPP2Project.dll", UriKind.Relative));
                    AssemblyPart ap = new AssemblyPart();
                    Assembly a = ap.Load(streamInfo.Stream);

                    Stream s = a.GetManifestResourceStream("Myproject.Test.xaml");

Where i have already downloaded SLAPP2Project.dll.

Please suggest or ask me for more clarifications.

thanks.

 

.netdan
.netdan

Contributor

Contributor

3390 points

512 Posts

Re: How to reference on demand assemblies.

Hi,

Theres a great post here showing how to dynamically download and use assemblies from other zap files.

Remember to click "Answer" if this has helped you!

Dan Birch
MCSD, MCAD, MCP
Free Silverlight Controls | Free .NET Silverlight CMS

silidev
silidev

Member

Member

141 points

92 Posts

Re: Re: How to reference on demand assemblies.

Yes, i dont have a problem in downloading assemblies ondemand. I am asking how to re refence them once downloaded. Is there a shorter way or should i re do the entire process of making a webclient call. I assume there is some way to refer already downloaded ones.

.netdan
.netdan

Contributor

Contributor

3390 points

512 Posts

Re: Re: How to reference on demand assemblies.

I think I understand you now, you want to reference the assembly downloaded in Page.xaml.cs in Page2.xaml.cs?

If so, when you downloaded the assembly in Page.xaml.cs did you load the assembly into an Assemby object like this:

Assembly a = assemblyPart.Load(assemblySri.Stream);

Could you post the code (from Page.xaml.cs) you used if not?

 

Remember to click "Answer" if this has helped you!

Dan Birch
MCSD, MCAD, MCP
Free Silverlight Controls | Free .NET Silverlight CMS

silidev
silidev

Member

Member

141 points

92 Posts

Re: Re: Re: How to reference on demand assemblies.

 <UserControl x:Class="SLAPP1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        
        <Button Grid.Row="1" Content="re load" Click="Button_Click"/>
    </Grid>
</UserControl>

namespace SLAPP1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            WebClient c = new WebClient();
            c.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            c.OpenReadAsync(new Uri("SLAPP2.xap", UriKind.Relative));
            
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {

            Uri uri = new Uri("SLAPP2.dll", UriKind.Relative);
            StreamResourceInfo xapPackageSri = new StreamResourceInfo(e.Result, null);
            StreamResourceInfo assemblySri = Application.GetResourceStream(xapPackageSri, uri);

            AssemblyPart aPart = new AssemblyPart();
            Assembly assembly = aPart.Load(assemblySri.Stream);

            Stream s = assembly.GetManifestResourceStream("SLAPP2.MyControl.xaml");

            s.Position = 0;
            String xaml_string;
            using (StreamReader Reader = new StreamReader(s))
                xaml_string = Reader.ReadToEnd();

            UserControl sl1 = XamlReader.Load(xaml_string) as UserControl;

            LayoutRoot.Children.Add(sl1);
            LayoutRoot.InvalidateArrange();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Page2 c1 = new Page2();
           
        }
    }
}


namespace SLAPP1
{
    public class Page2
    {
        public Page2()
        {    
        StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("SLAPP2.dll", UriKind.Relative));
        // streamInfo is null here?
        AssemblyPart ap = new AssemblyPart();
        Assembly a = ap.Load(streamInfo.Stream);
        Stream s = a.GetManifestResourceStream("SLAPP2.MyControl.xaml");
        }
    }
}


SLAPP2.MyControl.xaml

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="My Control from App2"/>
    </Grid>
</UserControl>

 

Hope this is good.

.netdan
.netdan

Contributor

Contributor

3390 points

512 Posts

Re: Re: Re: How to reference on demand assemblies.

You could declare a public property of type Assembly and set it in both Page1 and Page 2 like this:

namespace SLAPP1
{
    public partial class Page : UserControl
    {
        public Assembly MyControls { get; set; }

        public Page()
        {
            InitializeComponent();

            WebClient c = new WebClient();
            c.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            c.OpenReadAsync(new Uri("SLAPP2.xap", UriKind.Relative));

        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {

            Uri uri = new Uri("SLAPP2.dll", UriKind.Relative);
            StreamResourceInfo xapPackageSri = new StreamResourceInfo(e.Result, null);
            StreamResourceInfo assemblySri = Application.GetResourceStream(xapPackageSri, uri);

            AssemblyPart aPart = new AssemblyPart();
            MyControls = aPart.Load(assemblySri.Stream);

            Stream s = MyControls.GetManifestResourceStream("SLAPP2.MyControl.xaml");

            s.Position = 0;
            String xaml_string;
            using (StreamReader Reader = new StreamReader(s))
                xaml_string = Reader.ReadToEnd();

            UserControl sl1 = XamlReader.Load(xaml_string) as UserControl;

            LayoutRoot.Children.Add(sl1);
            LayoutRoot.InvalidateArrange();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Page2 c1 = new Page2(MyControls);

        }
    }
}

namespace SLAPP1
{
    public class Page2
    {
        public Assembly MyControls { get; set; }

        public Page2()
        {
        }

        public Page2(Assembly myControls)
            : this()
        {
            MyControls = myControls;
            Stream s = MyControls.GetManifestResourceStream("SLAPP2.MyControl.xaml");
        }
    }
}

It maybe better to use a static property if you're going to use this assemby throughout your application.

 

Remember to click "Answer" if this has helped you!

Dan Birch
MCSD, MCAD, MCP
Free Silverlight Controls | Free .NET Silverlight CMS

silidev
silidev

Member

Member

141 points

92 Posts

Re: Re: Re: Re: How to reference on demand assemblies.

Ha! thats what i dont want to do, i dont want to save it or store in isolated storage. I wanted to know if by loading once there is some way to access from cache or any other api's?

.netdan
.netdan

Contributor

Contributor

3390 points

512 Posts

Answered Question

Re: Re: Re: Re: How to reference on demand assemblies.

The example I posted does not store it in isolated storage, it simply stores a reference to the assembly already in memory and is no different to any other .net property.

Remember to click "Answer" if this has helped you!

Dan Birch
MCSD, MCAD, MCP
Free Silverlight Controls | Free .NET Silverlight CMS
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities