仅在代码中制作 UI 而没有 XAML 的正确方法?
Correct approach to make UI only in code, without XAML?
我正在尝试使用 VS 代码在 linux (MX-linux) 中开发 Uno 应用程序。我希望不使用 XAML。使用模板创建存储库,我继续删除 MainPage.xaml
文件,并将 InitializeComponent();
替换为我的 UI.
代码
虽然这在 Skia.Gtk
平台上运行良好,但 Wasm
构建失败,因为它无法找到 MainPage.xaml
文件。
这是构建时大日志的开始部分
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
MSBUILD : error : Generation failed: System.AggregateException: One or more errors occurred. (Generation failed for Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator. System.AggregateException: One or more errors occurred. (Failed to parse file /home/Coding/UnoPrac/UnoPrac.Shared/MainPage.xaml)
所以我的问题是,我的处理方式是否正确?当然,我可以让 xaml 文件只包含 <Page..>
元素,它适用于某些平台,但是是否可以强制 Uno 忽略 xaml 文件(如果不存在)?
编辑: 我也在 windows 上测试过它。 Skia.Gtk
平台似乎是独一无二的,因为它可以 运行 没有任何 xaml 文件,前提是 InitializeComponent()
中的每个 InitializeComponent()
方法都存在合适的代码隐藏=23=] 个文件。
我删除了 App.xaml
文件并在 App.xaml.cs
文件
中添加了这个片段来代替 InitializeComponent()
方法
this.Resources = new Microsoft.UI.Xaml.Controls.XamlControlsResources();
同样,我删除了 MainPage.xaml
文件并在 App.xaml.cs
文件
中添加了这个来代替 InitializeComponent()
方法
public sealed partial class MainPage : Page
{
Button nextPage = new Button() { Content = "Next" };
TextBlock block = new TextBlock() { Text = "Welcome to Uno" };
public MainPage()
{
//this.InitializeComponent();
Content = new Grid() { Children = { block, nextPage } };
}
}
虽然此设置在 Skia.Gtk
平台上完美运行,但它不适用于我测试过的任何其他平台:UWP、Android 和 Wasm。
在 xaml 文件中保留空 <Page>
元素时,Android 平台应用程序可以成功运行,而 UWP 会遇到 运行time 错误,并且Wasm 应用程序 运行s 但页面是空白的(即没有组件出现)。
如果用于 Gtk 平台的代码能够忽略 xaml 文件,那么 Uno 中是否有其他平台的等效代码?或者这种方法根本不可能?
WASM 项目的 bin
或 obj
文件夹中可能有一些 MainPage.xaml
的遗留痕迹。在VS Code中打开解决方案文件夹,到处搜索MainPage.xaml
。确保删除引用文件的区域。之后构建应该再次运行。
我想出了需要改变的地方。我最初认为问题出在各个平台上,但现在我明白了 Skia.Gtk
端口 运行ning 的唯一原因,与其他平台不同,是因为构建错误在其中悄无声息地失败了。
要解决此问题,将删除 *.Shared.projitems
中任何 xaml 文件的所有依赖项。根据我的测试, App.xaml
本身可以根据问题进行删除和替换,并且 运行 在 Android, Linux(Skia.Gtk) 上完全没问题和 WebAssembly。但是,UWP 对 App.xaml
文件有一些内部依赖性,所以最后我无法删除它。但是,在删除所有其他 xaml
文件的引用时,我测试的所有端口(UWP、Android、Skia.Gtk 和 WebAssembly)都 运行 没有问题。
为了完整起见,我将在下面包含我编辑过的 *.Shared.projitems
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>6279c845-92f8-4333-ab99-3d213163593c</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>UnoPrac</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<ApplicationDefinition Include="$(MSBuildThisFileDirectory)App.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)*.cs"/>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="$(MSBuildThisFileDirectory)Strings\en\Resources.resw" />
</ItemGroup>
</Project>
哦,我在 Wasm 应用程序中看到“空白”页面的原因仅仅是因为背景以及上面的所有组件都是白色的。所以我只需要将页面的背景设置为黑色即可。
我正在尝试使用 VS 代码在 linux (MX-linux) 中开发 Uno 应用程序。我希望不使用 XAML。使用模板创建存储库,我继续删除 MainPage.xaml
文件,并将 InitializeComponent();
替换为我的 UI.
虽然这在 Skia.Gtk
平台上运行良好,但 Wasm
构建失败,因为它无法找到 MainPage.xaml
文件。
这是构建时大日志的开始部分
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
MSBUILD : error : Generation failed: System.AggregateException: One or more errors occurred. (Generation failed for Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator. System.AggregateException: One or more errors occurred. (Failed to parse file /home/Coding/UnoPrac/UnoPrac.Shared/MainPage.xaml)
所以我的问题是,我的处理方式是否正确?当然,我可以让 xaml 文件只包含 <Page..>
元素,它适用于某些平台,但是是否可以强制 Uno 忽略 xaml 文件(如果不存在)?
编辑: 我也在 windows 上测试过它。 Skia.Gtk
平台似乎是独一无二的,因为它可以 运行 没有任何 xaml 文件,前提是 InitializeComponent()
中的每个 InitializeComponent()
方法都存在合适的代码隐藏=23=] 个文件。
我删除了 App.xaml
文件并在 App.xaml.cs
文件
InitializeComponent()
方法
this.Resources = new Microsoft.UI.Xaml.Controls.XamlControlsResources();
同样,我删除了 MainPage.xaml
文件并在 App.xaml.cs
文件
InitializeComponent()
方法
public sealed partial class MainPage : Page
{
Button nextPage = new Button() { Content = "Next" };
TextBlock block = new TextBlock() { Text = "Welcome to Uno" };
public MainPage()
{
//this.InitializeComponent();
Content = new Grid() { Children = { block, nextPage } };
}
}
虽然此设置在 Skia.Gtk
平台上完美运行,但它不适用于我测试过的任何其他平台:UWP、Android 和 Wasm。
在 xaml 文件中保留空 <Page>
元素时,Android 平台应用程序可以成功运行,而 UWP 会遇到 运行time 错误,并且Wasm 应用程序 运行s 但页面是空白的(即没有组件出现)。
如果用于 Gtk 平台的代码能够忽略 xaml 文件,那么 Uno 中是否有其他平台的等效代码?或者这种方法根本不可能?
WASM 项目的 bin
或 obj
文件夹中可能有一些 MainPage.xaml
的遗留痕迹。在VS Code中打开解决方案文件夹,到处搜索MainPage.xaml
。确保删除引用文件的区域。之后构建应该再次运行。
我想出了需要改变的地方。我最初认为问题出在各个平台上,但现在我明白了 Skia.Gtk
端口 运行ning 的唯一原因,与其他平台不同,是因为构建错误在其中悄无声息地失败了。
要解决此问题,将删除 *.Shared.projitems
中任何 xaml 文件的所有依赖项。根据我的测试, App.xaml
本身可以根据问题进行删除和替换,并且 运行 在 Android, Linux(Skia.Gtk) 上完全没问题和 WebAssembly。但是,UWP 对 App.xaml
文件有一些内部依赖性,所以最后我无法删除它。但是,在删除所有其他 xaml
文件的引用时,我测试的所有端口(UWP、Android、Skia.Gtk 和 WebAssembly)都 运行 没有问题。
为了完整起见,我将在下面包含我编辑过的 *.Shared.projitems
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>6279c845-92f8-4333-ab99-3d213163593c</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>UnoPrac</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<ApplicationDefinition Include="$(MSBuildThisFileDirectory)App.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)*.cs"/>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="$(MSBuildThisFileDirectory)Strings\en\Resources.resw" />
</ItemGroup>
</Project>
哦,我在 Wasm 应用程序中看到“空白”页面的原因仅仅是因为背景以及上面的所有组件都是白色的。所以我只需要将页面的背景设置为黑色即可。