开发环境
本文涉及到的C#工程是基于 Visual Studio 2019 Ver16.10(建议更新到Visual Studio 2019最新的版本)
准备工作
首先需要下载 WebRuntime 二进制包:TheUniverse下载地址 ,这一步是工作的基础。
我们考虑如下两种情况:
| | 1. 您有一个已经存在的C# WinForm工程; 2. 您创建了一个全新的C# WinForm工程; | | - | ----------------------------------------------------------- |
无论是哪一种情形,您需要参照如下视频呈现的步骤调整您的C#工程:(调整:1、编译模式;2、manifest配置;3、组件引用)
.NET工程配置调整视频教程
现在开始构造exe
(1)创建WinForm工程
您首先需要一个最简单的C# WinForm 工程,我们将这个C#工程命名为“FirstApp”
(注意这里需要的是一个基于.NET Framework的C# WinForm工程)
工程创建完毕之后,我们看到如下图片:
编译这个工程,生成可执行文件“FirstApp.exe”,找到可执行文件所在的文件夹(FirstApp工程的编译输出目录),将我们准备工作阶段解压TheUniverse.zip得到的文件夹TheUniverse里面的所有文件复制到FirstApp.exe所在的文件夹(C#工程的输出目录):
(复制必要的运行时组件到输出目录)
(2)建立对cosmos.dll的引用
复制结束之后,我们在输出目录之中找到cosmos.dll,在FirstApp工程之中引用“cosmos.dll” 。
(3)添加工程的mainfest清单文件
建立引用之后,给工程添加“mainfest”文件(即所谓的清单文件) ,这是一个标准流程:
(app.mainfest非常重要)
打开app.manifest文件,找到如下部分:
(注意清单文件如图标记的部分)
将标记部分修改为
完整的修改之后的mainfest文件如下:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
-->
</assembly>
注意清单文件之中如下的段落:(非常重要)
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
(4)修改Main函数
现在打开program.cs,我们看到如下代码窗口:
将如图标记的代码修改为:
(5)修改工程的编译配置
现在,我们需要修改工程配置,如下图:
(我们只支持64位应用)
现在,我们完成了全部基础工作,可以重新编译工程了。
重新审核代码
在program,cs包含的代码之中,将
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
修改为:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Universe.Cosmos.Run();
}
我们非常明显的看到一个“问题”,即这个代码没有启动任何一个Form。换句话说,虽然FirstApp是一个WinForm程序,但目前为止,代码里面没有任何一个环节加载了GUI元素!