简体   繁体   English

如何使用 Xamarin 更改带有随机短语列表的标签

[英]How to change a label with a random list of phrases with Xamarin

I'm new to Xamarin, I want to make an application who every day, a phrase change.我是 Xamarin 的新手,我想每天做一个应用程序,一个短语变化。 I want to use a timer to change the phrase.我想使用计时器来更改短语。

I tried several tutorials but didn't work to me.我尝试了几个教程,但对我没有用。

Here's a example on my XAML and C# code who I made actualy:这是我实际制作的 XAML 和 C# 代码示例:

XAML XAML

<ScrollView Grid.Row="1">
        <StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10">
            <Label x:Name="Label_Time" Text="[TIME]" FontSize="Title" HorizontalOptions="Center"/>
            <Label Text="before the new enigma" FontSize="Subtitle" HorizontalOptions="Center"/>
            <Label Text="You can resolve now or waiting tomorrow for a new enigma." FontSize="20" Padding="0,0,0,0"/>
            <Entry x:Name="Label_Answer" Placeholder="Enter the answer"/>
            <Button Text="Answer" BackgroundColor="#487eb0" Clicked="OnButtonClicked"/>
        </StackLayout>
    </ScrollView>

C# C#

public void RandomEnigma(object sender, EventArgs e)
    {
        var enigmas = new List<string>()
        {
            "My first is the main color of the earth, my second can be used to move, my third is for surviving. My everything is a liquid. What is that ?",
            "Very childish, I can introduce children to programming. What is my name ?",
        };
    }

    private void OnButtonClicked(object sender, EventArgs e)
    {
        // WIP
    }

You can generate a number between 0 and the range of array enigmas each time with the following code:您可以使用以下代码每次生成一个介于 0 和数组enigmas范围之间的数字:

    int range = enigmas.Count;

    Random rnd = new Random();
    int number = rnd.Next(0, range-1);  // creates a number between 0 and range-1

And after that, assign a value of the arry to a label:之后,将数组的值分配给标签:

    mLabel.Text = enigmas[number];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM