[WinRT] IEnumerable<Assembly> SelectAssemblies() CaliburnApplication

Topics: Bootstrappers & IoC, Getting Started
Oct 15, 2012 at 5:10 AM

This

 return new

[] { GetType().GetTypeInfo().Assembly };

Only returns the current assembly not all DLL with View/ViewModel in them, is it possible in WinRT

 

Oct 15, 2012 at 6:15 PM
Edited Oct 15, 2012 at 6:16 PM

You can get multiple assemblies in WinRT with the code shown below. You can add in your own pattern matching as well to only load specific assemblies if you need to.

private async Task<IEnumerable<Assembly>> GetAssemblyListAsync()
{
	var assemblies = new List<Assembly>();
        try
        {
	        var files = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();
                foreach (var file in files)
                {
                    if ((file.FileType == ".dll") || (file.FileType == ".exe"))
                    {
			AssemblyName name = new AssemblyName() { Name = Path.GetFileNameWithoutExtension(file.Name) };
                        Assembly asm = Assembly.Load(name);
                        assemblies.Add(asm);
                    }
                }
	}
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
    return assemblies;
}
Oct 15, 2012 at 7:32 PM

Right, I tried that but i get either of those

1. it get stock on Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();
   for ever

or

2.  It says file not found on that line

 Assembly asm = Assembly.Load(name);

any thoughts?

Oct 15, 2012 at 8:05 PM

You need to call it in or after App.OnLaunched() so it doesn't hang. You can override that method in app.xaml.cs.

 

Oct 15, 2012 at 8:23 PM

In the WinRT Caliburn sample, you can make the call in the Configure() in app.xaml.cs.

I have the following in a test app I was doing.

 

protected override async void Configure()
{
    base.Configure();
    container = new WinRTContainer(RootFrame);
    container.RegisterWinRTServices();
    await ConfigureParts();
}

private async Task ConfigureParts()
{
    await ConfigurePartsWorker().AsAsyncAction();
}

private async Task ConfigurePartsWorker()
{
    var assemblies = await GetAssemblyListAsync();
}

private async Task<IEnumerable<Assembly>> GetAssemblyListAsync()
{
    List<Assembly> assemblies = new List<Assembly>();
    try
    {
        var files = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();

        foreach (var file in files)
        {
            if ((file.FileType == ".dll") || (file.FileType == ".exe"))
            {
                AssemblyName name = new AssemblyName() { Name = Path.GetFileNameWithoutExtension(file.Name) };
                Assembly asm = Assembly.Load(name);
                assemblies.Add(asm);
             }
         }
     }
     catch (Exception e)
     {
         System.Diagnostics.Debug.WriteLine(e.Message);
     }

     return assemblies;
}