更改 WPF 项目的启动文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11833796/
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 04:52:22  来源:igfitidea点击:

Changing the startup file of a WPF project

wpf

提问by Darren Hale

I am sure this is a trivial problem, but it's one of which I cannot seem to solve.

我确信这是一个微不足道的问题,但这是我似乎无法解决的问题之一。

I changed the startup file in my App.xaml from StartupUri="MainWindow.xaml"to the code below, hoping that this all I needed to do.

我将 App.xaml 中的启动文件从StartupUri="MainWindow.xaml"下面的代码更改为以下代码,希望这就是我需要做的。

<Application x:Class="MVVMPrototype.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MVVMPrototype.View.MVVMPrototype.xaml">
<Application.Resources>

</Application.Resources>

However, I then got the following error stating that I need to set the build property to resource as show below. Previously it was set to Page, which is the same as what the MainWindow.xaml was set to.

但是,我随后收到以下错误,指出我需要将构建属性设置为资源,如下所示。以前它被设置为 Page,这与 MainWindow.xaml 的设置相同。

Resource

资源

After doing that I then have a compiler error stating that InitializeComponent();does not exists in the current context. The only time I know this to happen is when the class / namespace is out of sync between the .cs file and the .xaml file, but this is not the case.

这样做之后,我有一个编译器错误,指出InitializeComponent();当前上下文中不存在。我知道这种情况发生的唯一时间是 .cs 文件和 .xaml 文件之间的类/命名空间不同步,但事实并非如此。

I have had a look at the StartupUri documentationbut I have not found it to be that helpful. I am sure it is something silly I have overlooked. And yes, I have clean and built the project several times with no luck. Does anyone know how to solve it?

我查看了StartupUri 文档,但我没有发现它有什么帮助。我确定这是我忽略的愚蠢的事情。是的,我已经多次清理并构建了该项目,但都没有运气。有谁知道如何解决它?

UPDATE:Seva Titov has solved the first part. But now I have a TargetInvocationExceptionwith an inner exception of System.Security.SecurityExceptionwith the picture below (open the image in a new tab if it's too hard to read):

更新:Seva Titov 已经解决了第一部分。但是现在我有一个TargetInvocationException内部例外,System.Security.SecurityException如下图(如果很难阅读,请在新选项卡中打开图像):

Security Exception

安全异常

Any ideas for this one?

对这个有什么想法吗?

回答by seva titov

StartupUri specifies relative file location, and you seem to have a file MVVMPrototype.xaml in folder View, so your code should look like this:

StartupUri 指定了相对文件位置,并且您似乎在文件夹 View 中有一个文件 MVVMPrototype.xaml,因此您的代码应如下所示:

<Application x:Class="MVVMPrototype.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     StartupUri="View/MVVMPrototype.xaml">

回答by Darren Hale

Okay, I have now figured out what is happening.

好的,我现在已经弄清楚发生了什么。

As pointed out in the answer by Seva Titov the StartupUri should have the value of "View/MVVMPrototype.xaml".

正如 Seva Titov 在回答中指出的,StartupUri 的值应该为"View/MVVMPrototype.xaml".

The InitializeComponent();error where it was stating it was not defined in the current context was because of the build action on the properties of MVVMPrototype.xaml was set to resource, not page. Originally I thought adding a new Window control solved this, but it turns out to be a red herring since the build action was set as page on the new control - I think that is the default.

InitializeComponent();它指出它未在当前上下文中定义的错误是因为对MVVMPrototype.xaml的属性的构建操作设置为 resource 而不是 page。最初我认为添加一个新的 Window 控件可以解决这个问题,但结果证明这是一个红鲱鱼,因为构建操作被设置为新控件上的页面 - 我认为这是默认设置。

The last error with the TargetInvocationExceptionand the inner exception of System.Security.SecurityExceptionI fixed by toggling the "Enable ClickOnce security settings" under Project Properties -> Security to generate the app.manifest.

通过在项目属性 -> 安全性下切换“启用 ClickOnce 安全设置”来生成app.manifest来修复TargetInvocationException和内部异常的最后一个错误。System.Security.SecurityException

This gave me the final error that it is unable to find the manifest signing certificate, so I just unchecked the "Sign the ClickOnce manifests" under project properties -> Signing.

这给了我无法找到清单签名证书的最终错误,所以我只是取消选中项目属性-> 签名下的“签署 ClickOnce 清单”。

This solved my issue. Hope it helps someone else. Please note I have little, if any, knowledge of deployment / publishing, and I am not sure how the last two will effect this.

这解决了我的问题。希望它可以帮助别人。请注意,我对部署/发布知之甚少(如果有的话),我不确定最后两个将如何影响这一点。