【问题标题】:C# WPF load and use custom controls from DLL during runtimeC# WPF 在运行时从 DLL 加载和使用自定义控件
【发布时间】:2018-01-27 23:12:11
【问题描述】:

我是 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


更新:-

当我添加Using ToggleSwitch; 时,我可以声明HorizontalToggleSwitch,但我收到一个很好的错误提示

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

有什么想法吗?

【问题讨论】:

  • 投到HorizontalToggleSwitch。
  • 请举例?
  • 在您的代码中只声明HorizontalToggleSwitch Switch = null; 并在实例化时执行Switch = Activator.CreateInstance(t) as HorizontalToggleSwitch;
  • 我之前无法做到这一点,但我发现我需要在使用时导入它,我做到了并且它有效,但是当我尝试执行该过程时,我得到了那个错误:- System.IO.FileNotFoundException: 'Could not load file or assembly 'ToggleSwitch, Version=1.1.0.0, Culture=neutral, PublicKeyToken=8637099990568f75' or one of its dependencies. The system cannot find the file specified.'

标签: c# wpf dll runtime


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    以防万一有人遇到同样的问题!这是我的解决方案。 我不知道为什么 Windows 会为此苦苦挣扎,但如果您仅在 xaml 中使用程序集,Windows 将无法找到它,或者在错误的文件夹中搜索。在我的情况下,我在 AddIn-Folder 中工作,而 not 在

    System.AppDomain.CurrentDomain.BaseDirectory;
    

    Addin aka Windows 在 C# 代码中使用时能够找到所有程序集,但是当我仅在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多