XAML 文件如何与 cs 文件关联? [英] How do XAML files associate with cs files?

查看:69
本文介绍了XAML 文件如何与 cs 文件关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 项目中,XAML 文件似乎应该有相应的 .cs 文件.我知道 Visual Studio 为我们做了所有的事情.我只是好奇他们是如何联系在一起的?我的意思是,它们是在项目文件中指定的,还是仅仅因为它们具有相同的名称?还有,App.xaml 文件指定了启动文件,但是编译器怎么知道呢?是否可以指定 App.xaml 以外的其他文件来执行与 App.xaml 相同的操作?

解决方案

WPF 没有什么神奇之处.所有发生的事情都写在某个地方.VS 生成了一些代码.

xaml 代码与一个类相关联.

VS 为你生成一个 MainWindow.cs 文件,它有一个名为 MainWindowWindow 类型的类.类型在这里很重要.如果您使用另一种类型,即使它是正确的类名,编译器也不会将其链接到 MainWindow.xaml.最终,对于 UserControl,您将拥有 xaml 标记 <UserControl 而不是 Window 标记.

还有一点,编译器还在编译时在 obj 文件夹中生成一个名为 MainWindow.g.cs 的文件,你也可以在其中找到一个 MainWindow.baml xaml 文件的编译版本.此文件将包含一个部分类 MainWindow,其中包含您在 XAML 中使用的声明的所有控件.这是编译器所做的幕后工作,与 XAML 和相关类之间的关联无关.

应用是一样的,只是类类型发生了变化.这是为了将 XAML 与类链接.对于启动窗口,它在 XAML 文件中默认指定为一个类.但是,您可以自定义 .cs 文件并在 ApplicationStartUp 事件中执行自己的逻辑.

关闭事件也是如此.默认情况下,所有窗口都关闭时,但您可以将其更改为 MainWindow 关闭或显式关闭时.

csproj(在 c# 的情况下)告诉编译器哪个类是应用程序.

<Generator>MSBuild:Compile</Generator><SubType>设计器</SubType></应用定义>

不仅仅是像其他人所说的 XAML 标记.标签只定义了类的类型,不会让你的程序从这个特定的类开始.


(来源:microsoft.com)

您可以在此处进一步阅读:MSDN - 构建 WPF申请

MSDN -WPF 中的代码隐藏和 XAML

MSDN -应用管理概述

It seems that XAML files should have corresponding .cs files in a C# project. I know Visual Studio does all the things for us. I'm just curious how they are linked together? I mean, are they specified in the project file, or just because they have the same names? And also, App.xaml file specifies the startup file, but how does the compiler know? Is it possible to appoint another file other than App.xaml to do the same things as App.xaml does?

解决方案

There's no magic happening in WPF. All what happen is written some where. It's VS which generates some of the code.

The xaml code is linked with a class.

<Window x:Class="YourNameSpace.MainWindow" ...

VS generates for you a MainWindow.cs file which has a class named MainWindow of Window type. The type is important here. If you use another type the compiler won't link it to your MainWindow.xaml even if it's the right class name. Eventually, for a UserControl you will have the xaml tag <UserControl instead of a Window tag.

One more thing, the compiler also generates at compile time a file called MainWindow.g.cs in the obj folder where you can also find a MainWindow.baml the compiled version of the xaml file. This file will contain a partial class MainWindow with all the controls declared that you use in the XAML. That's a behind the scene job that the compiler does which is unrelated to the association between the XAML and the relative class.

The application is the same, only that the class type changes. This is to link XAML with the class. For the StartUp Window, it's specified in the XAML file by default to a class. However, you can customize the .cs file and do yo own logic in the ApplicationStartUp event.

Same for the Shutdown event. By default it's when all your windows are closed but you can change it to when the MainWindow is closed or an explicit shutdown.

The csproj (in the case of c#) tells the compiler which class is the application.

<ApplicationDefinition Include="App.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</ApplicationDefinition>

Not only the XAML tag like others have stated. The tag only defines the type of the class and don't make your program start with this specific class.


(source: microsoft.com)

You can read further here : MSDN - Building WPF application

MSDN - Code-Behind and XAML in WPF

MSDN - Application Management Overview

这篇关于XAML 文件如何与 cs 文件关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆