WPF:App.xaml 的目的是什么?

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

WPF: What is App.xaml's Purpose?

wpfxaml

提问by Jeff LaFay

I've done .Net development for awhile but I'm new to the WPF technology. What is the supposed purpose of App.xaml? Also, what type of xaml code do youusually put in it? It seems like for simple applications it could be ignored and left untouched. Is this true?

我已经做了一段时间的 .Net 开发,但我是 WPF 技术的新手。App.xaml 的预期目的是什么?另外,通常在其中放入什么类型的 xaml 代码?对于简单的应用程序,它似乎可以被忽略并且保持不变。这是真的?

采纳答案by TomTom

It is true. App.Xaml is some sort of central starting point. You CAN use it, or you CAN start your first window (it is defined in the app.xaml) manually. There are some lifetime events there centralls (like application start).

是真的。App.Xaml 是某种中心起点。您可以使用它,或者您可以手动启动您的第一个窗口(它在 app.xaml 中定义)。那里有一些生命周期事件(比如应用程序启动)。

回答by rasx

App.xamlis the declarativeportion of your code (usually generated by Visual Studio) extending System.Windows.Application. For example, Expression Blend can use App.xamlto share a Resource Dictionary or a design-time data set with your entire application. And, because we are using Microsoft products, whatever Expression Blend can do auto-magically, we can do by hand in Visual Studio.

App.xaml是代码的声明部分(通常由 Visual Studio 生成)扩展System.Windows.Application. 例如,Expression Blend 可用于App.xaml与整个应用程序共享资源字典或设计时数据集。而且,因为我们使用的是 Microsoft 产品,所以无论 Expression Blend 可以自动神奇地做什么,我们都可以在 Visual Studio 中手动完成。

Now the tangent:To me, to ask about the purpose of App.xamlis to ask about the purpose for System.Windows.Application. Feel free to accuse me of changing the original question (let the digital brutally ensue).

现在切线:对我来说,询问 的目的App.xaml就是询问 的目的System.Windows.Application。随意指责我改变了最初的问题(让数字残酷地接踵而至)。

You can't just open a System.Windows.Controls.Windowin any Assembly you like… Chris Sells is likely telling me this in his book. I began to understand the purpose of System.Windows.Applicationwhile using MEF and MVVM Light to display WPF windows in DLLs(not EXEs). I got errors like this:

你不能System.Windows.Controls.Window在任何你喜欢的大会中打开一个...... Chris Sells 很可能在他的书中告诉我这一点。我开始了解System.Windows.Application使用 MEF 和 MVVM Light 在DLLs(而不是EXEs)中显示 WPF 窗口的目的。我有这样的错误:

The type 'System.Windows.Markup.IComponentConnector' is defined in an assembly that is not referenced.

The type 'System.Windows.Markup.IComponentConnector' is defined in an assembly that is not referenced.

or

或者

The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced.

The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced.

The above error is simply saying that I'm trying to open a WPF Window inside of a DLL and notan EXE. Then, there's this error:

上述错误是简单地说,我试图打开一个DLL的WPF窗口内,没有一个EXE。然后,有这个错误:

The component 'Songhay.Wpf.WordWalkingStick.Views.ClientView' does not have a resource identified by the URI '/Songhay.Wpf.WordWalkingStick;component/views/clientview.xaml'.

The component 'Songhay.Wpf.WordWalkingStick.Views.ClientView' does not have a resource identified by the URI '/Songhay.Wpf.WordWalkingStick;component/views/clientview.xaml'.

This boils down to the absence of a facility that associates WPF Window XAML with the WPF “code” (an instance). This facility is associated with WPF EXEsand not WPF DLLs. Visual Studio auto-generates a WPF EXE class called App.g.cs(in your \obj\Debugfolder) with this call in it: System.Windows.Application.LoadComponent(this, resourceLocater)where resourceLocateris a badly named variable containing a System.Uripointing to the XAML like ClientView.xamlmentioned above.

这归结为缺少将 WPF Window XAML 与 WPF“代码”(一个实例)相关联的工具。此工具与 WPFEXEs而不是 WPF相关联DLLs。Visual Studio 自动生成一个名为App.g.cs(在您的\obj\Debug文件夹中)的 WPF EXE 类,System.Windows.Application.LoadComponent(this, resourceLocater)其中包含此调用:where resourceLocateris a badly named variable 包含System.Uri指向 XAMLClientView.xaml如上所述。

I'm sure Chris Sells has a whole chapter written on how WPF depends on System.Windows.Applicationfor its very life. It is my loss (quite literally of time) for not having read about it.

我确信 Chris Sells 有一整章都写了 WPF 如何依赖System.Windows.Application它的生命。没有阅读它是我的损失(实际上是时间)。

I have shown myself a little something with this unit test:

我通过这个单元测试向自己展示了一些东西:

[STAThread]
[TestMethod]
public void ShouldOpenWindow()
{
    Application app = new Application();
    app.Run(new Window());
}

Failing to wrap a new Window in the System.Windows.Application.Run()method will throw an error from the land of COM talking about, “Why did you pull the rug from underneath me?”

如果没有在System.Windows.Application.Run()方法中包装一个新的 Window会抛出一个来自 COM 领域的错误,“你为什么从我下面拉地毯?”

回答by Jason

For simple applications, it is true, it can be ignored. The major purpose for App.xaml is for holding resources (style, pens, brushes, etc.) that would would like to be available through out all of the windows in your application.

对于简单的应用,确实​​如此,可以忽略。App.xaml 的主要用途是保存希望在应用程序的所有窗口中都可用的资源(样式、钢笔、画笔等)。

回答by Nicolas Repiquet

Storing resources that are used across the whole application.

存储在整个应用程序中使用的资源。

Application is the root of the logical tree.

应用程序是逻辑树的根。

回答by Rahul Soni

It is like Global.asax if you are coming from an ASP.NET background. You can also use it to share resources throughout your application. Comes in pretty handy for resource sharing.

如果您来自 ASP.NET 背景,它就像 Global.asax。您还可以使用它在整个应用程序中共享资源。对于资源共享非常方便。

回答by AishwaryaKasi

App.xaml is a major part of wpf application.
It contains major four attributes
1.X:Class->used to connect you xaml and code-behind file(xaml.cs).
2.xmlns->To resolve wpf elements like canvas,stack panel(default one).
3.xmlns:x->To resolve XAML language definition.
4. StartupUri->To give start window when application is launching.

回答by Ron16

++++++++

++++++++

App.xaml is the declarative starting point of your application. Visual Studio will automatically create it for you when you start a new WPF application, including a Code-behind file called App.xaml.cs. They work much like for a Window, where the two files are partial classes, working together to allow you to work in both markup (XAML) and Code-behind.

App.xaml.cs extends the Application class, which is a central class in a WPF Windows application. .NET will go to this class for starting instructions and then start the desired Window or Page from there. This is also the place to subscribe to important application events, like application start, unhandled exceptions and so on.

One of the most commonly used features of the App.xaml file is to define global resources that may be used and accessed from all over an application, for instance global styles.

App.xaml 是应用程序的声明性起点。当你启动一个新的 WPF 应用程序时,Visual Studio 会自动为你创建它,包括一个名为 App.xaml.cs 的代码隐藏文件。它们的工作方式与 Window 非常相似,其中两个文件是部分类,协同工作以允许您在标记 (XAML) 和代码隐藏中工作。

App.xaml.cs 扩展了 Application 类,它是 WPF Windows 应用程序中的一个中心类。.NET 将转到此类以获取启动说明,然后从那里启动所需的 Window 或 Page。这也是订阅重要应用程序事件的地方,如应用程序启动、未处理的异常等。

App.xaml 文件最常用的功能之一是定义可以从整个应用程序使用和访问的全局资源,例如全局样式。

+++++++++ Source : http://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

+++++++++ 来源:http: //www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

回答by Major Tom

Here is an updated answer in case people are still looking.

这是一个更新的答案,以防人们仍在寻找。

There is this excellent article on WPF, and the link specifically puts you at the App.Xaml point to begin teaching you the things you can do with it.

有一篇关于 WPF 的优秀文章,该链接专门让您进入 App.Xaml 点,开始教您使用它可以做的事情。

WPF is easy for the first very simple app or two. However, due to the increased flexibility of the framework, you need these types of tutorials to help you understand what can be done from where (in the various application files).

对于第一个或两个非常简单的应用程序,WPF 很容易。但是,由于框架增加了灵活性,您需要这些类型的教程来帮助您了解可以从何处(在各种应用程序文件中)完成的操作。

https://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

https://www.wpf-tutorial.com/wpf-application/working-with-app-xaml/

Good luck.

祝你好运。