我正在制作一个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]);
}
但没有得到结果:-( 请提出建议......谢谢。
答案 0 :(得分: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;
答案 1 :(得分: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;
}