如何将启动 uri 属性设置为 wpf 项目中文件夹中的另一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18180215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:25:21  来源:igfitidea点击:

how to set startup uri property to another window in folder in wpf project

wpfstartup-folder

提问by Sekhar Babu

In my wpf project i have created a folder called practice, in that folder i added a window, now i want to run that window, so in app.xaml file i set the startup uri to foldername.window.xaml but it is saying build action property is not set to resource.

在我的 wpf 项目中,我创建了一个名为 practice 的文件夹,在该文件夹中我添加了一个窗口,现在我想运行该窗口,因此在 app.xaml 文件中,我将启动 uri 设置为 foldername.window.xaml 但它说的是构建操作属性未设置为资源。

for that i setted build action property to resource. Now this time that it is showing error message initialized componenet doesn't exist in the current context.

为此,我将构建操作属性设置为资源。现在这一次它显示错误消息初始化组件在当前上下文中不存在。

Can you tell me what properties we need to set when we create separate folders in wpf project and that folders contains windows or pages. and How to access those pages in other pages or in App.Xaml file startupUri Property.

您能告诉我在 wpf 项目中创建单独的文件夹时需要设置哪些属性,并且该文件夹包含窗口或页面。以及如何在其他页面或 App.Xaml 文件 startupUri 属性中访问这些页面。

回答by Ian Griffiths

When you have folders in your project structure, you should use a "/" not a ".", so it's foldername/window.xaml.

当您的项目结构中有文件夹时,您应该使用“/”而不是“.”,所以它是foldername/window.xaml.

(I hope it's not actually called window.xamlby the way. That's a confusing name for a type in a WPF project, because there's a built in type called Window.)

(我希望它实际上没有window.xaml被顺便调用。对于 WPF 项目中的类型来说,这是一个令人困惑的名称,因为有一个名为 的内置类型Window。)

Setting the build action to Resource will make matters worse: not only were you using the wrong name, you've now changed the build action to the wrong one for XAML. The correct build action for a .xamlfile is usually Page. (App.xamlis an exception to that rule.) The Pagebuild action causes the page to be compiled into a binary representation (known as BAML), and that binary format can then be loaded either by the call to InitializeComponentin the codebehind, or through Application.LoadComponent.

将构建操作设置为 Resource 会使事情变得更糟:您不仅使用了错误的名称,而且您现在将构建操作更改为 XAML 的错误名称。文件的正确构建操作.xaml通常是Page. (App.xaml该规则的一个例外。)Page构建操作会导致页面被编译为二进制表示(称为 BAML),然后可以通过调用代码InitializeComponent隐藏中的 或通过Application.LoadComponent.

Setting the build action to Resource will just embed a copy of the XAML source directly in the project, which won't help you - you can't work with XAML in that form if you want to have a codebehind file. (Not in WPF, anyway. It's different in other XAML-based frameworks such as WinRT.)

将构建操作设置为 Resource 只会在项目中直接嵌入 XAML 源的副本,这对您没有帮助 - 如果您想要一个代码隐藏文件,则无法以该形式使用 XAML。(无论如何,在 WPF 中不是。在其他基于 XAML 的框架(如 WinRT)中有所不同。)

Since Page is the default build action for a newly-added window, you don't actually need to set any properties at all. You just need to use /for folder boundaries.

由于 Page 是新添加窗口的默认构建操作,因此您实际上根本不需要设置任何属性。您只需要/用于文件夹边界。

回答by Debendra Dash

If the XAML is inside any Folder the startup url will be defined as below.

如果 XAML 在任何文件夹内,则启动 url 将定义如下。

enter image description here

在此处输入图片说明

This is how it will defined. enter image description here

这就是它将如何定义。 在此处输入图片说明