如何在 Visual Studio 的 WPF Python 项目中手动创建 App.xaml 文件?

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

How to manually create an App.xaml file in a WPF Python Project in Visual Studio?

c#wpfvisual-studio-2012ironpythonironpython-studio

提问by Robins Gupta

When we create a new WPF C# Project from Visual Studiowe get 4 basic files"App.xaml","App.cs","MainWindow.xaml","MainWindow.cs"

当我们从 Visual Studio创建一个新的 WPF C# 项目时,我们会得到4 个基本文件"App.xaml","App.cs","MainWindow.xaml","MainWindow.cs"

Here App.xamlfile is basically used for storing application related Resourcesin one file to be present all over the application.

这里的App.xaml文件主要用于存储application related Resources在一个文件中以存在于整个应用程序中。

But when creating a WPF Python Project in Visual Studiowe get only 2 files"MainWindow.xaml" and "MainWindow.py"

但是在 Visual Studio 中创建 WPF Python 项目时,我们只有2 个文件"MainWindow.xaml" and "MainWindow.py"

I want to create 2 more file in my Python project"App.xaml"and "App.py"so that in "App.xaml"I can easily define my all application related resources styles and control-templates.

I want to create 2 more file in my Python project“App.xaml”“App.py”,这样"App.xaml"我就可以轻松定义所有与应用程序相关的资源样式和控制模板。

Plz Help!!

请帮忙!!

回答by Sheridan

You can add an Application file to your project from the Solution Explorer. You can press Alt+S to open it. In the Solution Explorer, select your project and then right click and select the Addmenu item and then New Itemmenu item from the popup menu.

您可以从解决方案资源管理器将应用程序文件添加到您的项目。您可以按 Alt+S 打开它。在解决方案资源管理器中,选择您的项目,然后右键单击并从弹出菜单中选择添加菜单项和新建项菜单项。

Next, in the Add New Itemdialog window, you can select WPFfrom the Installed Templateson the left to filter the available templates. Then, you can simply choose to add a Resource Dictionaryand name it App.xaml. As long as you put the relevant code in it, it will be fine:

接下来,在Add New Item对话框窗口中,您可以从左侧的Installed Templates 中选择WPF来过滤可用的模板。然后,您可以简单地选择添加一个资源字典并将其命名为。只要把相关代码放进去就可以了:App.xaml

<Application x:Class="ManuallyCreatingAWpfProject.app"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="AppStartup"
    >
  <Application.Resources>

  </Application.Resources>

</Application>

...

public partial class App : Application
{
    void AppStartup(object sender, StartupEventArgs args)
    {
        Window1 mainWindow = new Window1();
        mainWindow.Show();
    }
}

This code was taken from the Walkthrough: Manually Creating a Windows Presentation Foundation Project Using Visual Studiopage on MSDN, which you may also find interesting.

此代码取自演练:使用MSDN 上的Visual Studio 手动创建 Windows Presentation Foundation 项目页面,您可能还会发现它很有趣。

You might also want to go to the project properties (right click on the project in the Solution Explorer and select Properties) and set the Startup Objectto the App.xamlfile in the Applicationtab. Finally, you'd better set the Build Actionto ApplicationDefinitionin the App.xamlfile properties.

您可能还想转到项目属性(右键单击解决方案资源管理器中的项目并选择Properties)并将Startup Object设置App.xamlApplication选项卡中的文件。最后,您最好在文件属性中将Build Action设置为ApplicationDefinitionApp.xaml