Looking to load up XAML/events/everything connected to a user control for a DLL that is loaded at runtime into another WPF project based on a config file.
So you have 2 DLLs, only one will be loaded based on JSON configuration, however I want all my buttons and everything to work correctly. This is the code I have ATM
User control for Grey DLL
<UserControl x:Class="WPFGreyButtonTest.InstrumentUserControl"
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:local="clr-namespace:WPFGreyButtonTest"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Label x:Name="colourName" Content="GREY" HorizontalAlignment="Left" Height="93" Margin="284,88,0,0" VerticalAlignment="Top" Width="243" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="50" Foreground="#FF8B8B8B"/>
</Grid>
</UserControl>
No actual code
User control for Purple DLL
<UserControl x:Class="WPFPurpleButtonTest.InstrumentUserControl"
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:local="clr-namespace:WPFPurpleButtonTest"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Label x:Name="colourName" Content="PURPLE" HorizontalAlignment="Left" Height="93" Margin="284,88,0,0" VerticalAlignment="Top" Width="243" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="50" Foreground="#FFDC00FF"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="365,253,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>
Code for that (both DLLs have the same class name i.e. InstrumentUserControl)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFPurpleButtonTest
{
/// <summary>
/// Interaction logic for InstrumentUserControl.xaml
/// </summary>
public partial class InstrumentUserControl : UserControl
{
public static readonly DependencyProperty InnerButtonProperty = DependencyProperty.Register("InnerButton", typeof(Button), typeof(InstrumentUserControl));
public Button InnerButton
{
get { return (Button)GetValue(InnerButtonProperty); }
set { SetValue(InnerButtonProperty, value); }
}
public InstrumentUserControl()
{
InitializeComponent();
InnerButton = button;
}
}
}
Main WPF application code
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFSandBox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
UserControl userControl = null;
InstrumentEnum instrumentType = InstrumentEnum.Invalid;
string dllToLoad = null;
// When we first initialize our WPF app, in the constructor we can
// allow a config file (such as a json) to be read and load up
// an appropriate user control view
public MainWindow()
{
InitializeComponent();
ReadJson();
LoadRunTimeDLL();
//var ucs = new List<InstrumentUserControl>();
var ucs = new List<UserControl>();
ucs.Add(userControl);
//ucs.Add(new UserControl1());
//ucs.Add(new UserControl1());
//ucs.Add(new UserControl1());
ic.ItemsSource = ucs;
}
private void ReadJson()
{
using (StreamReader r = new StreamReader("../../Config/Config.json"))
{
string json = r.ReadToEnd();
var jsonData = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach (var entry in jsonData)
{
if (entry.Key == "InstrumentType")
{
Enum.TryParse(entry.Value, out instrumentType);
}
else if (entry.Key == "DllToLoad")
{
dllToLoad = entry.Value;
}
}
}
}
private void LoadRunTimeDLL()
{
string assemblyName = string.Format("{0}\\{1}.dll",
new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, dllToLoad);
if (assemblyName != null)
{
Assembly asm = Assembly.LoadFile(assemblyName);
Type[] tlist = asm.GetTypes();
foreach (Type t in tlist)
{
if (t.Name == "InstrumentUserControl")
{
userControl = Activator.CreateInstance(t) as UserControl;
break;
}
}
if (userControl != null)
{
//contentControl.Content = userControl;
}
}
}
}
}
And it's MainWindow xaml
<Window x:Class="WPFSandBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFSandBox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!--<Grid>
<ContentControl Grid.Row="1" x:Name="contentControl" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</Grid>-->
<ItemsControl x:Name="ic" />
</Window>
Any help would be appreciated on how I can make sure whichever DLL gets loaded, the button works correctly.
I'm trying to load WPF DLLs at runtime. Works fine, but the buttons and having the events tied to the main WPF app is what I don't know how to do