简体   繁体   English

C#WPF在发布时崩溃

[英]C# WPF crashes on launch

So I recently started learning winforms... but upon asking a question here I learned that WPF applications are much better, thus started learning the latter. 所以我最近开始学习winforms ......但是在这里问了一个问题后,我了解到WPF应用程序要好得多,因此开始学习后者。 I'm making a very basic login screen and ran into a weird glitch. 我正在制作一个非常基本的登录屏幕并遇到了一个奇怪的故障。 Upon startup the program instantly crashes (Shows a message box saying "Login has stopped working" "A problem caused the program to stop working correctly. Please close the program.")... I found the problem by removing pieces of code (there's not much at the moment, just getting the hang of the basics) - and it's the background image I used. 启动时程序立即崩溃(显示一个消息框说“登录已停止工作”“一个问题导致程序停止正常工作。请关闭程序。”)...我通过删除代码找到了问题(这里有)目前并不多,只是掌握基础知识) - 这是我使用的背景图像。

<Window.Background>
        <ImageBrush ImageSource="Background1.jpg"/>
</Window.Background>

This is the piece of code causing the problem, also tried replacing it with grid.background but the exact same error appears. 这是导致问题的一段代码,也尝试用grid.background替换它,但出现完全相同的错误。 Any ideas as to how to fix this issue are appreciated. 关于如何解决这个问题的任何想法都表示赞赏。 (The image is saved in the same folder as the solution, and shows properly in the preview). (图像保存在与解决方案相同的文件夹中,并在预览中正确显示)。

In order to simply reference an image like that: 为了简单地引用像这样的图像:
- add your image to the project ( right click => Add => new item... ) at the same level as your window - 将您的图像添加到项目中(右键单击=>添加=>新项目...)与窗口处于同一级别
- set its "Build Action" property to "Resource" - 将其“Build Action”属性设置为“Resource”
- set its "Copy to Output Directory" property to "Do not copy". - 将其“复制到输出目录”属性设置为“不复制”。

Try copying the file to either bin\\Debug or bin\\Release folder depending on the build configuration. 尝试将文件复制到bin \\ Debug或bin \\ Release文件夹,具体取决于构建配置。 The current directory for the program might not be the solution's root directory. 程序的当前目录可能不是解决方案的根目录。

If this works, a better approach would be to add it to the resources and not use any absolute path structure. 如果这样做,更好的方法是将其添加到资源而不使用任何绝对路径结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM