只在代码中创建UI而不使用XAML的正确方法



我正在尝试使用VS代码在linux(MX-linux(中开发Uno应用程序。我希望不使用XAML。使用模板创建存储库,然后删除MainPage.xaml文件,并用我的UI代码替换InitializeComponent();

虽然这在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文件的情况下运行,前提是*.xaml.cs文件中的每个InitializeComponent()方法都有合适的代码。

我删除了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。

在保留带有空<Page>元素的xaml文件时,Android平台应用程序确实成功工作,而UWP遇到运行时错误,Wasm应用程序运行,但页面为空(即没有组件出现(。

如果用于Gtk平台的代码能够忽略xaml文件,那么Uno中是否有其他平台的等效代码?或者这种方法根本不可能?

在WASM项目的binobj文件夹中可能有一些MainPage.xaml的遗留痕迹。打开VS Code中的解决方案文件夹,并到处搜索MainPage.xaml。请确保删除引用文件的区域。之后,构建应该会再次工作。

我找到了所需的更改。我最初认为问题出在各个平台上,但现在我明白了Skia.Gtk端口运行的唯一原因,与其他平台不同,是因为构建错误在其中默默地失败了

为了解决这个问题,对任何xaml文件的所有依赖都将在*.Shared.projitems中删除。根据我的测试,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)AssetsSharedAssets.md" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="$(MSBuildThisFileDirectory)StringsenResources.resw" />
</ItemGroup>
</Project>

哦,我得到";空白";Wasm应用程序中的页面只是因为背景是白色的,以及上面的所有组件。所以我只需要将页面的背景设置为黑色。

相关内容

最新更新