简体   繁体   English

移动 MainWindow.xaml

[英]Moving MainWindow.xaml

I've built a WPF app, which completely works now.我已经构建了一个 WPF 应用程序,现在完全可以使用了。 However, to clean it up a bit I wish to move my MainWindow.xaml to the view folder I've created.但是,为了稍微清理一下,我希望将MainWindow.xaml移动到我创建的视图文件夹中。 After I've done this the application won't run and it gives me an "Unknown build error" which doesn't give any info on how to fix it...完成此操作后,应用程序将无法运行,它给我一个“未知的构建错误” ,但没有提供有关如何修复它的任何信息......
What should I change in my MainWindow.xaml to let the app work properly again?我应该在MainWindow.xaml更改什么才能让应用程序再次正常工作?

I've already changed我已经变了

<Window x:Class="projectname.MainWindow">

to

<Window x:Class="projectname.view.MainWindow">

Should I change other stuff as well?我也应该改变其他东西吗?

You don't need to update class name in xaml file unless you have changed the namespace of your class.除非您更改了类的命名空间,否则不需要更新 xaml 文件中的类名。 Most likely you haven't updated StartupUri for App.xaml .您很可能还没有为App.xaml更新StartupUri Change it to:将其更改为:

StartupUri="view/MainWindow.xaml"

from

StartupUri="MainWindow.xaml"

The answer of @Rohit Vats is quite good! @Rohit Vats回答相当不错!

However that's a good point to remember: you have to change all the absolute paths (in XAML) to your resources, prepending them by a / in order to indicate that these paths are relative to the root directory .但是,记住这一点很重要:您必须更改资源的所有绝对路径(在 XAML 中),在它们前面加上/以指示这些路径是对于root directory


Example:例子:

From <Image Source="Assets/Loading.gif">来自<Image Source="Assets/Loading.gif">

To <Image Source="/Assets/Loading.gif"> and so on.<Image Source="/Assets/Loading.gif">等等。

I just ran into this myself.我自己刚刚遇到了这个。 Here's what I did:这是我所做的:

  1. Move your MainWindow.xaml to your new folder.MainWindow.xaml移动到新文件夹。 In my case it was /Views.就我而言,它是 /Views。
  2. I like to name all my classes with their namespaces reflecting their folder.我喜欢用它们的命名空间来命名我所有的类,它们反映了它们的文件夹。 So my MainWindow.xaml.cs namespace went from ProjectNamespace to ProjectNamespace.Views所以我的MainWindow.xaml.cs命名空间从ProjectNamespace变成了ProjectNamespace.Views
  3. In my MainWindow.xaml , I needed to change x:Class from ProjectName.MainWindow to ProjectName.Views.MainWindow .在我的MainWindow.xaml ,我需要将x:ClassProjectName.MainWindow更改为ProjectName.Views.MainWindow
  4. In your App.xaml, change the StartupUri to reflect the folder.在 App.xaml 中,更改StartupUri以反映文件夹。 I went from StartupUri="MainWindow.xaml" to "StartupUri="Views/MainWindow.xaml"`我从StartupUri="MainWindow.xaml"到 "StartupUri="Views/MainWindow.xaml"`

Doing this allowed me to compile and run my app.这样做使我能够编译和运行我的应用程序。

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

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