【问题标题】:Random Images in Wp7Wp7中的随机图像
【发布时间】:2014-09-11 19:27:34
【问题描述】:

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

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]);
}

但没有得到结果:-( 请提出建议..提前致谢。

【问题讨论】:

    标签: image windows-phone-7 c#-4.0 random


    【解决方案1】:

    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;
    

    【讨论】:

      【解决方案2】:
          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;
          }
      

      【讨论】:

        猜你喜欢
        • 2022-07-10
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-15
        相关资源
        最近更新 更多