Asked by:
Loading Controls from External Assemblies

Question
-
To Whom It May Concern:
I am building a plug-able application that consists of a dashboard or main UI, and any number of external dlls that represent an APP. Using the below code, I successfully loaded the assemblies and displayed the Tiles on the Dashboard:
DashboardVM
private DashboardViewModel() // SINGLETON
{
this.SelectTile = new DelegateCommand(OnSelectTile);
Tiles = new ObservableCollection<Tile>();
BeginBackgroundTasks();
}
private void BeginBackgroundTasks()
{
ComponentFinder loader = new ComponentFinder(@"Y:\Development\Management\Neurons");
IDictionary<string, Assembly> assemblies = loader.LoadAssembliesByPath();
foreach(Assembly assembly in assemblies.Values)
{
Type[] types = assembly.GetTypes();
Type componentType = types.First(x => typeof(IComponent).IsAssignableFrom(x));
IComponent component = (IComponent)Activator.CreateInstance(componentType);
_componentMap.Add(assembly.FullName, component);
component.DashTile.Command = new DelegateCommand(OnSelectTile);
component.DashTile.CommandParameter = assembly.FullName;
Tiles.Add(component.DashTile);
component.OnViewChanged += Component_OnViewChanged;
}
}Dashboard.xaml
<UserControl x:Class="Management.UI.Views.Dashboard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<telerik:RadTileList ItemsSource="{Binding Tiles}" />
</UserControl>PROBLEM:
* When I load the dashboard for the first time, everything is fine!
* When I navigate away from the dashboard, everything is fine!
* However, when I return to the dashboard the Tiles do not display. I don't fully understand why the ObservableCollection (Tiles) are not displaying or binding even through they are the same as before the view changed. They are the same because the dashboardVM is a singleton. I set the current view in the main window as follows:
CurrentViewModel = DashboardVM.Instance;
I am sure the Dashboard view is displayed or returned, but the tiles loaded from the external assemblies do not render or display even though they did on the initial load.
- Edited by Vince2010 Monday, May 25, 2020 1:49 AM
Sunday, May 24, 2020 12:32 AM
All replies
-
I refactored the BeginBackgroundTasks() method to use the new .netcore 3 AssemblyLoadContext class. This solved some issues when loading external DLLs due to InitializeComponent() not finding XAML file/resource, but not the one described above. The changes were:
private void BeginBackgroundTasks()
{
_finder = new ComponentFinder(@"Y:\Development\Management\Components");
ISet<string> paths = _finder.FindAssemblyPaths();
foreach(string path in paths)
{
ComponentLoadContext context = new ComponentLoadContext(path);
Assembly assembly = context.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(path)));
Type[] types = assembly.GetTypes();
Type componentType = types.FirstOrDefault(x => typeof(IComponent).IsAssignableFrom(x));
if ( componentType != null )
{
IComponent component = (IComponent)Activator.CreateInstance(componentType);
component.DashTile.Command = new DelegateCommand(OnSelectTile);
component.DashTile.CommandParameter = assembly.FullName;
component.OnViewChanged += Component_OnViewChanged;
Tiles.Add(component.DashTile);
_componentMap.Add(assembly.FullName, component);
}
}
}- Edited by Vince2010 Monday, May 25, 2020 1:56 AM
Monday, May 25, 2020 1:56 AM -
Hi,
Thanks for posting here.
This forum is for discussing about User Interface development for Windows Desktop.
For WPF question, you could ask on Q&A forum:
https://docs.microsoft.com/answers/topics/wpf.html
Best Regards,
Drake
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- Proposed as answer by Drake_WuMicrosoft contingent staff Wednesday, June 3, 2020 7:40 AM
Monday, May 25, 2020 7:47 AM