【问题标题】:WPF - Cannot find resource defined in app xaml fileWPF - 找不到应用程序 xaml 文件中定义的资源
【发布时间】:2019-05-29 13:32:47
【问题描述】:

嘿嘿……

我的 WPF 应用程序有一个自定义启动以显示自定义启动屏幕并进行一些准备工作(例如解析参数、准备文件系统等)。为此,我重写了 OnStartup 方法。

App.xaml.cs

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);

      var splash = new SplashWindow();
      splash.Show();

      // do some black magic

      splash.Close();
      var mw = new MainWindow();
      mw.Show();
    }
  }
}

App.xaml

<Application x:Class="Example.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             ShutdownMode="OnExplicitShutdown">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="Foo">#FFFFAA00</Color>
            <SolidColorBrush x:Key="Bar" Color="{StaticResource Foo}" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

但是以这种方式处理启动我无法访问 app.xaml 中定义的资源,即 MainWindow.xaml 或 Splash.xaml。

其他一些人已经有同样的问题,即https://stackoverflow.com/a/13425695

按照解决方法链接将我带到这个 stackoverflow 帖子:https://stackoverflow.com/a/3896209/5663348
建议的解决方法是定义 Application 元素的 x:Name。但它如何帮助我访问定义的资源?不幸的是,“更多信息”的链接已损坏(太旧)...
应用这个问题的公认答案甚至不能解决问题。

我正在使用 .Net Framework 7.4.2

谁能给我一些提示,我该如何解决这个问题?

干杯和感谢:)

==================

问题解决了... 现实世界的应用程序在实例化 App 类时初始化了 MainWindow 和 SplashWindow...愚蠢的错误...

【问题讨论】:

  • 您在哪里尝试访问资源以及如何访问?覆盖Startup 方法不应影响资源。你的App.xaml 怎么样?
  • @mm8 就像我说的我尝试访问资源,即在 MainWindow 和 SplashWindow 中(添加最后一个)。我还添加了一个示例 App.xaml。在这两个 Windows 中,我得到一个 System.Windows.StaticResourceExntesion 异常,其中键 FooBar 是未知的
  • InitializeComponent() 抛出异常。异常消息仅引用使用 {StaticResource Foo}{StaticResource Bar} 的 xaml 文件中的行和行
  • 当我试图重现您的问题时,这对我来说效果很好。哪个窗口首先失败的 xaml 到底是什么?
  • @D.Weber 我对你的建议是用你如何解决它来回答你自己的问题。它可能会在未来帮助其他人。

标签: c# wpf startup


【解决方案1】:

问题
SplashWindow 和 MainWindow 尝试访问 App.xaml 中定义的资源。 在 App.xaml.cs 中完成了一些准备工作等。加载一些文件。但是当 App 被实例化时,它已经尝试创建 SplashWindow 和 MainWindow 的实例

public partial class App : Application
{
    private readonly SplashWindow splash;

    public App() {
        splash = new SplashWindow();  // this cannot work, if SplashWindow has a reference to a resource defined in App
        this.MainWindow = new MainWindow();  // this cannot work, if MainWindow has a reference to a resource defined in App
    }

    protected override void OnStartup(StartupEventArgs e) {
        base.OnStartup(e);

        var splash = new SplashWindow(); // yes, splash was instancied multiple times...
        splash.Show();

        // do some magic

        splash.Close();
        MainWindow.Show();
    }
}

SplashScreen 和 MainWindow 的实例化导致异常,因为他们试图访问 App 中定义的资源,但 App 没有完成实例化,因此无法访问 Object。
在尝试重建错误时,我创建了一个更简单的应用程序,其中 MainWindow 和 SplashWindow 在 App.OnStartup(e) 中实例化,但是当调用此事件时,应用程序对象存在,因此它可以工作。

解决方案
删除字段splash 并且不要使用构造函数创建SplashWindow 和MainWindow 的实例

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e) {
        base.OnStartup(e);

        var splash = new SplashWindow(); // yes, splash was instancied multiple times...
        splash.Show();

        // do some magic

        this.MainWindow = new MainWindow();
        splash.Close();
        MainWindow.Show();
    }
}

感谢所有帮助我找出这个错误的人...
特别感谢@Default,他提醒我回答这个问题:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多