如何用Xamarin的随机列表更改标签

发布于 2025-02-10 20:26:03 字数 1359 浏览 2 评论 0原文

我是Xamarin的新手,我想制作一个应用程序,每天都会更改短语。我想使用计时器更改短语。

我尝试了几个教程,但对我不起作用。

这是我XAML和C#代码上的一个示例:

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
    }

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2025-02-17 20:26:03

您可以在每次使用以下代码的情况下每次生成0和数组范围的数字:

    int range = enigmas.Count;

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

此后,将Arry的值分配给标签:

    mLabel.Text = enigmas[number];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文