C#WPF在运行时加载并使用DLL中的自定义控件

时间:2018-01-27 23:12:11

标签: c# wpf dll runtime

我是WPF的新手,也不是C#的专业人员,但是,我正在创建一个应用程序,它必须获得一组真/假状态并为每个创建切换开关然后设置它(isChecked) )财产。

然而,经过大约3个小时的谷歌搜索后,我找到了这个代码,所以我可以在运行时使用WPF中的外部控件并将其编辑为控件,而不是切换开关: -

            Control Switch = null;
            Assembly asm = Assembly.LoadFile(Directory.GetCurrentDirectory() + "/CTS.dll");
            Type[] tlist = asm.GetTypes();
            foreach (Type t in tlist)
            {
                if (t.Name == "HorizontalToggleSwitch")
                {
                    Switch = Activator.CreateInstance(t) as Control;
                    break;
                }
            }

            Switch.Name = "MasterSwich" + i;
            Switch.HorizontalAlignment = HorizontalAlignment.Right;
            Switch.Margin = new Thickness(0, 0, 35, 0);
            Switch.Width = 100;
            Switch.Height = 35;

            SwichesPanel.Children.Add(Switch);

工作正常,但我无法设置与切换开关相关的任何属性(如IsChecked),因为它只将其视为控件。

如何解决这个问题,或者是否有更好的想法在运行时从DLL文件导入控件?

切换开关文件以防任何人需要,可在此处找到:Toggle Switch Demo

更新: -

我可以在添加HorizontalToggleSwitch时声明Using ToggleSwitch;,但我发现了一个很好的错误

  

System.IO.FileNotFoundException:'无法加载文件或程序集'ToggleSwitch,Version = 1.1.0.0,Culture = neutral,PublicKeyToken = 8637099990568f75'或其依赖项之一。系统找不到指定的文件。'

任何想法?

2 个答案:

答案 0 :(得分:0)

我解决了它,我刚刚导入了更新中提到的lib,并使用了进程管理器来找出编译器正在寻找的内容并找出它想要DLL的确切文件名所以只需重新设置一切就可以了。

答案 1 :(得分:0)

以防万一有人遇到同样的问题!这是我的解决方案。 我不知道Windows为什么要为此苦苦挣扎,但是如果仅在xaml中使用Assembly,则Windows将找不到它,或者在错误的文件夹中搜索。就我而言,我在AddIn-Folder中工作,而在

不是
System.AppDomain.CurrentDomain.BaseDirectory;

在C#代码中使用Addin aka Windows时能够找到所有程序集,但是当我仅在xaml代码中使用ToggleSwitch UI元素时,它在错误的文件夹中搜索,而不在addin文件夹中搜索。

我通过以下方法解决了这个问题。

在插件启动功能中,我添加了一个伪代码来强制应用加载程序集,以便以后使用时可以使用

  static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ...
            LoadLoadUiAssemblies();
            ...
        }

        public void LoadUiAssemblies()
        {
            // somehow the loading of the wpf components does not work, 
            //here we load the assembly in the cache so that we can use it
            var ts = new WPFToggleSwitch.ToggleSwitch();
        }
    }

这是WPF中的用法

<UserControl x:Class="ViewManager.DimensionAssistantView"
             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:ViewManager"
             mc:Ignorable="d"
             xmlns:p="clr-namespace:ViewManager.Properties"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             d:DesignHeight="300" d:DesignWidth="400"

             xmlns:wts="clr-namespace:WPFToggleSwitch;assembly=WPFToggleSwitch"

             Name="balalal"
             MinWidth="100" MinHeight="100">


 <StackPanel  Grid.Row="0" 
                     HorizontalAlignment="Left" Orientation="Horizontal" 
                     IsEnabled="{Binding IsFaceSelected}" Background="WhiteSmoke">

            <wts:ToggleSwitch IsChecked="{Binding  SelectedFaceSketchsVisibleStatus}" CheckedText="un-visible" UncheckedText="Visible">
                <wts:ToggleSwitch.Content>
                    <TextBlock VerticalAlignment="Center"
                               TextWrapping="Wrap"
                               Text="{x:Static p:Resources.DAShowAllDimensionsToggleButtonText}" />
                </wts:ToggleSwitch.Content>
            </wts:ToggleSwitch>
        </StackPanel>