如何在 Xamarin Forms 中延迟几秒随机图像并显示为 XAML?

How to Random Images with delay in a secs in Xamarin Forms and Display into XAML?

我想在 xamarin 表单中随机化图像并在几秒钟内实现延迟。

这是我的XAML代码

<Image x:Name="RandomImageList" Source="Album1.jpg">

这是我的 C# 代码

List<string> imageList = new List<string> { "Album1.jpg", "Album2.jpg", "Album3.jpg" };

        var random = new Random();
        var next = random.Next(4);
        var image = imageList[next];

        RandomImageList.Source = ImageSource.FromResource(image);

这是我运行这段代码的结果。

*System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' *

很简单,就这样做吧。

public MainPage()
{
    InitializeComponent();

    List<string> imageList = new List<string> { "add.png", "compare.png", "down.png", "remove.png" };

    Device.BeginInvokeOnMainThread(async () =>
    {
        while (true)
        {
            var random = new Random();
            var next = random.Next(4);
            var image = imageList[next];
            await Task.Delay(2000);

            RandomImage.Source = ImageSource.FromFile(image);
        }
    });
}

输出: