首页 > 解决方案 > WPF InitializeComponent() 在使用 SplashScreen Buid Action 添加图像时给出 CS0103 错误

问题描述

我面临着一些我无法向自己解释的奇怪问题(因此修复):在我的 C# 应用程序中,我有 3 个 WPF Windows 会在调用时弹出。当然,所有这些 WPF 窗口都有该InitializeComponent()方法。

我还创建了一个SplashWindow自定义类,我在其中定义了一个 SplashScreen 通用窗口。需要一个Uri对象作为输入,它将弹出一个启动屏幕,其中包含引用该 Uri 的图像,然后在调用关闭方法时关闭。我用于启动画面的图像加载到我的解决方案中名为Resources的文件夹中,并且Build Action设置为SplashScreen。这是代码:

    class SplashWindow
{
    private Form form = null;

    public SplashWindow(Uri uri)
    {
        form = new Form();

        StreamResourceInfo info = System.Windows.Application.GetResourceStream(uri);

        Image splashImage = Image.FromStream(info.Stream);

        // Get image dimensions
        form.Width = splashImage.Width;
        form.Height = splashImage.Height;            

        form.BackgroundImage = splashImage;

        form.BackgroundImageLayout = ImageLayout.Stretch;

        form.StartPosition = FormStartPosition.CenterScreen;
        form.FormBorderStyle = FormBorderStyle.None;
        form.TopMost = true;

        form.Show();
    }

    internal void CloseSplash()
    {
        form.Close();
    }
}

我在我的代码中调用了这个闪屏对象,如下所示:

// Start async to get user
Task<User> getUser = GitHubConnection.LoginAsync(tok);

// Pop up splash screen in the meantime
SplashWindow splash = new SplashWindow(validationUri);

// Await for user
User user = await getUser;

// Close splash screen
splash.CloseSplash();

这非常有效!

我面临的问题是,如果我尝试将另一个图像添加到我的解决方案并将构建操作设置为 SplashScreen 到新更新的图像,那么InitializeComponent()我所有 WPF 窗口中的所有方法都会开始给出 CS0103 错误:The name InitializeComponent does not exist in current context。所以这就像我不能使用多个启动画面,但这对我来说没有任何意义。

知道为什么会这样吗?我错过了什么?

标签: c#wpfsplash-screen

解决方案


推荐阅读