【问题标题】:Hiding/Showing text in Xamarin Forms在 Xamarin 表单中隐藏/显示文本
【发布时间】:2021-01-07 20:43:13
【问题描述】:

我正在使用 Xamarin Forms 开发移动应用程序。我想在应用程序中按下按钮时显示某些文本,否则隐藏。但是,我不确定如何使用 XAML 和 C# 来执行此操作。我尝试使用 Console.WriteLine() 命令编写条件,但文本未显示在应用程序中。有没有关于如何解决这个问题的建议,或者我应该查看哪些资源来了解更多信息?

XAML 代码 - 按钮:

 <Button Text="I am new to this app" Clicked="Button_Clicked"> 
 </Button><Button Text="I have used this app before" Clicked="Button_Clicked_1"></Button

这些按钮的 C# 代码:

    /* This only changes the label of the buttons, it doesn't make the text appear separately like I want it to.*/

  void Button_Clicked (System.Object sender, System.EventArgs e)
    {
        ((Button)sender).Text = "Hey there! Welcome to this awesome app!";

    }

    void Button_Clicked_1(System.Object sender, System.EventArgs e)
    {
        ((Button)sender).Text = "Welcome back!";

    }

【问题讨论】:

  • 使用任何 UI 元素的 IsVisible 属性来显示/隐藏它。如果您需要进一步的帮助,请发布相关代码,以便我们了解您在做什么。
  • 嗨!现在添加代码。感谢您的建议和回答。
  • 我仍然不知道你想做什么。有两个按钮——当用户点击它们时会发生什么?您希望在单击另一个按钮时隐藏一个按钮吗?
  • 嗨!单击一个按钮时,我希望文本出现在按钮下方(一个按钮的某些文本,另一个按钮的不同文本)。我希望这能消除您的困惑。

标签: c# android ios xaml xamarin.forms


【解决方案1】:

你需要使用Labels来显示额外的文字

<Button x:Name="Button1" Text="I am new to this app" Clicked="Button_Clicked" /> 
<Label x:Name="Label1" Text="Hey there! Welcome to this awesome app!" IsVisible="False" />
<Button x:Name="Button2" Text="I have used this app before" Clicked="Button_Clicked_1" />
<Label x:Name="Label2" Text="Welcome back!" IsVisible="False" />

然后在后面的代码中你可以更改每个标签的IsVisible 属性

void Button_Clicked (System.Object sender, System.EventArgs e)
{
    Label1.IsVisible = true;
}

void Button_Clicked_1(System.Object sender, System.EventArgs e)
{
    Label2.IsVisible = true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    相关资源
    最近更新 更多