【问题标题】:WPF Application: Missing Resources when starting from WinForms AppWPF 应用程序:从 WinForms 应用程序启动时缺少资源
【发布时间】:2016-03-22 13:30:43
【问题描述】:

我需要使我的 WPF 桌面应用程序可以从 WinForms 应用程序启动。

但是,我正在使用第三方控件(例如来自 Telerik),并且只有将所有相同的引用 DLL 复制到正在启动 WPF 应用程序的 WinForms 应用程序才能启动该应用程序。

例外情况始终是找不到与 application.xaml 关联的文件或依赖文件。大概是因为正在执行的程序集。

我不允许添加对启动 WPF 应用程序的 WinForms 应用程序的引用,那么如何强制我的 WPF 应用程序查找正确的位置?

问题出现在此处以及我的 MainWindow 的资源部分中:

<Application x:Class="Application" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/System.Windows.xaml" />
            <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.xaml" />
            <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Input.xaml" />
            <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Data.xaml" />
            <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />
            <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Docking.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

非常感谢。

【问题讨论】:

    标签: .net wpf winforms xaml telerik


    【解决方案1】:

    我猜该应用程序正在它启动的应用程序目录中寻找依赖程序集 - 在 WinForms 应用程序目录中,如果找不到,它会引发错误。如果是这种情况,您可以查看AssemblyResolve 事件以指向程序集的正确位置。

    【讨论】:

    • 感谢您的及时回复。明确知道这一点很有用,但在这种情况下,WPF 应用程序可能是通过 COM 类启动的,所以我无法使用它。我想在 WPF 应用程序的构造函数中尝试强制执行此操作,或者如果可能的话直接在 xaml 中指定。
    【解决方案2】:

    我知道现在帮助主题启动器为时已晚,但是我会在此处发布答案,因为有人可能会遇到同样的问题。我花了一些时间来理解这个问题,而且修复实际上非常简单——只需在调用 Run() 之前调用 WPF 应用程序类的 InitializeComponent()。您还需要显式设置 ResourceAssembly 属性(到 App.xaml 所在的程序集):

    var wpfApp = (WpfApp)Application.Current ?? new WpfApp();
    Application.ResourceAssembly = AppDomain.CurrentDomain
        .GetAssemblies()
        .Single(a => a.FullName.Contains("MyApp.WpfClient"));
    wpfApp.InitializeComponent();
    wpfApp.Run();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2020-10-02
      • 1970-01-01
      相关资源
      最近更新 更多