How to change a label with a random list of phrases with Xamarin

Viewed 47

I'm new to Xamarin, I want to make an application who every day, a phrase change. 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

<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#

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
    }
1 Answers

You can generate a number between 0 and the range of array enigmas each time with the following code:

    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];
Related