如何以编程方式更改 .NET Maui 应用程序中标签的 Text 属性?

问题描述 投票:0回答:3

我最初在 MainPage.xaml 文件中设置了一个标签,如下所示:

<Label x:Name="Ans"
       Text=""
       FontSize="18"
       FontFamily="Verdana"
       FontAttributes="Bold"
       TextColor="White"
       Grid.Column="1"
       Grid.Row="2" 
       Padding="20,20,0,0"/>

我想要在运行时做的是更改

Text
属性以具有值。

我在我写的课程中尝试过这样的事情:

string retVal;
retVal = "This is a test";
Ans.Text = retVal;

但我收到一条消息:

“当前上下文中不存在名称‘Ans’。”

我该如何解决这个问题?

c# xaml maui
3个回答
2
投票

您只能通过属于同一 XAML 页面/视图的代码隐藏中的

x:Name
来访问标签,在您的情况下,这将是 MainPage.xaml.cs,例如:

public partial class MainPage : ContentPage
{
    public void ChangeLabel()
    {
        Ans.Text = "This is a test";
    }
}

您无法通过其他类的

x:Name
属性访问视图元素。 XAML 文件与其隐藏代码属于同一类,这就是为什么隐藏代码类被标记为
partial

更常见的方法是使用 MVVM 模式和数据绑定,而不是直接设置

Text
属性。

如果您需要根据不属于 View 或 ViewModel 的内容更新 UI,例如一个不同的、不相关的 View 或 ViewModel,那么您还可以使用 WeakReferenceMessenger 订阅消息并相应地更新 UI。


0
投票
using Microsoft.Maui.Controls;

// ...

// Inside a method or event handler
string retVal = "This is a test";
Label ansLabel = (Label)FindByName("Ans");
ansLabel.Text = retVal;

使用 FindByName 方法,您可以在当前 XAML 页面中查找指定的控件。

希望是你的帮助


0
投票

也许你的标签在 CollectionView 中!

我之前也遇到过同样的问题,而且是“CollectionView”问题。 让我解释一下,不仅仅是一个标签,还有 CollectionView 中的任何元素,它的 x:name="whatever" 在代码隐藏中都不会被知道。知道您应该考虑也许其他类型的 UI 集合也对其在 Elements 内部具有相同的效果,idk。

我已经测试了所有这些,创建一个新的 ContentPage 并将其替换为此处的 xaml(不要忘记编辑命名空间)。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="someNamespace.TestPage"
         Title="TestPage">
<VerticalStackLayout>        
    <CollectionView>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Label x:Name="LabelInsideCollectionView"
                       Text="Welcome to .NET MAUI!"
                       VerticalOptions="Center"
                       HorizontalOptions="Center" />
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
    
    <Label x:Name="LabelOutsideCollectionView"
           Text="Welcome to .NET MAUI!"
           VerticalOptions="Center"
           HorizontalOptions="Center" />
    
</VerticalStackLayout>
</ContentPage>

这是隐藏代码。

namespace someNamespace;
public partial class TestPage : ContentPage
    {
        public TestPage()
        {
            InitializeComponent();

            // This work.
            LabelOutsideCollectionView.Text = "Test";

            // this will not work, Error CS0103  The name 
           //'LabelInsideCollectionView' does not exist in the current context
            LabelInsideCollectionView.Text = "Test"; 
        }
    }

您将看到您的代码隐藏可以读取“LabelOutsideCollectionView”,但不能读取“LabelInsideCollectionView”。

我希望它对您或其他人有所帮助,并希望 MAUI 团队能够解决所有这些问题。

这是截至本回答时我的计算机上已安装版本的列表。

Installed Workload Id      Manifest Version       Installation Source
---------------------------------------------------------------------
android                    33.0.68/7.0.100        VS 17.7.33906.173
maui-android               7.0.92/7.0.100         VS 17.7.33906.173
maui-windows               7.0.92/7.0.100         VS 17.7.33906.173
maui-maccatalyst           7.0.92/7.0.100         VS 17.7.33906.173
maccatalyst                16.4.7089/7.0.100      VS 17.7.33906.173
maui-ios                   7.0.92/7.0.100         VS 17.7.33906.173
ios                        16.4.7089/7.0.100      VS 17.7.33906.173

我刚刚向 Github 上的 .NET MAUI 存储库报告了一个问题。 https://github.com/dotnet/maui/issues/16182

© www.soinside.com 2019 - 2024. All rights reserved.