如何将内容从 C# 添加到 XAML

How to add content from C# to XAML

我之前使用过一些 XamarinForms 然后我做了一个 命名以便能够将 c# 代码指向它。

   <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
   x:Class="MauiTest.MainPage"
   BackgroundColor="{DynamicResource SecondaryColor}">

    <StackLayout x:name="_stacklayoutname">     
    <Label Text="" x:Name="_Lable"/>
    </StackLayout>
</ContentPage>

然后我在 MainPage.xaml.cs

中这样做了
   public MainPage()
    {
        InitializeComponent();
        _stacklayoutname.Children.Add(new Label { Text = "TEST" });
        _Lable.Text = "TEST";
    }

现在它明白了,但我可以将 _Lable 更改为“文本”。 我怎样才能指向堆栈布局。

Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name '_stacklayoutname' does not exist in the current context   MauiTest (net6.0-android), MauiTest (net6.0-ios), MauiTest (net6.0-maccatalyst), MauiTest (net6.0-windows10.0.19041)    *** 11  Active

这是错误的

<StackLayout x:name="_stacklayoutname">  

应该是

<StackLayout x:Name="_stacklayoutname">