0

我正在为 Windows 手机制作一个应用程序,每当加载新页面时我都必须显示随机图像......我正在使用以下代码:-

private Random rand = new Random();
        private Image GetRandomImage(string PastScannerResults)
        {
            string[] files = Directory.GetFiles("/PastScanner/Results");
            int i = rand.Next(files.Length);
            return Image.FromFile(files[i]);
}

但没有得到结果:-(请提出一些建议..在此先感谢。

4

2 回答 2

0

Directory.GetFiles 是 XBOX 的语法。

为了在 WP7 上处理文件,您需要使用隔离存储。

这样的东西应该工作:

IsolatedStorageFile userFile = IsolatedStorageFile.GetUserStoreForApplication();  

var files = userFile.GetFileNames();

Random r = new Random();

using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(files[r.next(0,files.count], FileMode.Open, FileAccess.Read))
            {
                bi.SetSource(fileStream);
                this.img.Height = bi.PixelHeight;
                this.img.Width = bi.PixelWidth;
            }

this.img.Source = bi;
于 2012-08-01T15:23:55.587 回答
0
    public void image_random()

    {

        List<string> pics = new List<string>()
                   {
                           "Assets/1.jpg",
                           "Assets/2.jpg",
                           "Assets/3.jpg",
                           "Assets/4.jpg",
                           "Assets/5.jpg",
                           "Assets/6.jpg",
                           "Assets/7.jpg",
                           "Assets/8.jpg",
                           "Assets/9.jpg",
                           "Assets/10.jpg",
                           "Assets/11.jpg",
                           "Assets/12.jpg",
                           "Assets/13.jpg",
                           "Assets/14.jpg",
                           "Assets/15.jpg"
                           };

        Random rnd = new Random();
        ImageBrush brush1 = new ImageBrush();
        brush1.ImageSource = new BitmapImage(new Uri(pics[rnd.Next(0, 7)],
        UriKind.Relative));

        ImageBrush brush2 = new ImageBrush();
        brush2.ImageSource = new BitmapImage(new Uri(pics[rnd.Next(0, 7)],
        UriKind.Relative));


        img.Source = brush1.ImageSource;
        img1.Source = brush2.ImageSource;
    }
于 2014-09-11T11:27:34.480 回答