Click here to Skip to main content
15,888,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was building an app to show a picture then play a sound when tapped in WP7. I get the "If the access level of a method in a class library has changed, recompile any assemblies that reference the library." error when I compile, pointing at Main. Well Actually at "Attempt to access the method failed: System.IO.Directory.GetFiles(System.String). This may be due to porting it to WP7 whole from WinCE, but I am not sure.

This app is dirt simple (see code below), so I am having a hard time figuring out what has the wrong permissions. Have I made some fundamental error (I mean besides not making functions instead of copy-pasting functionality) ? Google is unhelpful, and Microsoft is AMAZINGLY unhelpful.

I apologize now for the length of code, but since I'm not sure what I am looking for I don't know how to reduce it to the "target" code.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace KanaFlash_WP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
       
        public MainPage()
        {
            InitializeComponent();  //Start up

            //read files with .png into files array
            string[] files = System.IO.Directory.GetFiles("*.png"); 

            //select a random file from files array and put it in soundfiles
            Random random = new Random();
            int randomNumber = random.Next(0, files.Length);
            string picturefilename = files[randomNumber];

            //display selected file
            BitmapImage bt = new BitmapImage();
            bt.UriSource = new Uri(picturefilename, UriKind.Absolute);
            image1.Source = bt;

            //Now for the matching sound file

            string soundfilename = System.IO.Path.GetFileNameWithoutExtension(picturefilename);
            soundfilename = soundfilename.ToLower();
            soundfilename = soundfilename.Replace("_", "");
            btnEnglish.Content = soundfilename;
            soundfilename = soundfilename + ".wav";
        }
        bool silencer = false;
       // string[] files = Directory.GetFiles(@"\Storage Card\Program Files\KanaFlash\Kana\", "*.png");

        string[] files = System.IO.Directory.GetFiles("*.png");
        private void image1_ImageFailed(object sender, GestureEventArgs e)
        {
            
        }

        public void PlayNewFile(string media)
        {   
            var stream = TitleContainer.OpenStream(media);
            var effect = SoundEffect.FromStream(stream);
            effect.Play();
        }

public void GestureListener_Tap(object sender, GestureEventArgs e)
{
    //select a random file from files array and put it in soundfiles
    Random random = new Random();
    int randomNumber = random.Next(0, files.Length);
    string picturefilename = files[randomNumber];

    //display selected file
    BitmapImage bt = new BitmapImage();
    bt.UriSource = new Uri(picturefilename, UriKind.Absolute);
    image1.Source = bt;

    //Now for the matching sound file

    string soundfilename = System.IO.Path.GetFileNameWithoutExtension(picturefilename);
    soundfilename = soundfilename.ToLower();
    soundfilename = soundfilename.Replace("_", "");
    btnEnglish.Content = soundfilename;
    soundfilename = soundfilename + ".wav";
    if (silencer == false)
    {
        if (System.IO.File.Exists(soundfilename))
        {
            //old method for WinCE:
            //SoundPlayer sound_player = new SoundPlayer(@"\Storage Card\Program Files\KanaFlash\WAV\" + soundfilename);
            //sound_player.Play();

            //new method for WP7:
            PlayNewFile((soundfilename));    //Needs to be WAV File

        }
    }
}

        public void btnEnglish_Click(object sender, RoutedEventArgs e)
        {
            //Toggle sound/no sound
            if (silencer == true)
            {
                silencer = false;
            }
            else silencer = true;
        }

     //   private void btnExit_Click(object sender, RoutedEventArgs e)
     //   {
     //      Application.Exit();
     //      turns out MS doesn't want you to cleanly exit!  Idiots. 
    //    }
    }
}
Posted
Updated 14-Jun-12 8:40am
v3
Comments
Steve Maier 12-Jun-12 15:18pm    
Formatted code.
[no name] 14-Jun-12 14:55pm    
http://forums.create.msdn.com/forums/p/75122/457217.aspx
Peltier Cooler 14-Jun-12 16:51pm    
I tried substituting GetFileNames as shown here:

public string[] filenames = IsolatedStorageFile.GetFileNames("file:///C:/Users/unhappydeveloper/Documents/Visual Studio 2010/Projects/KanaFlash WP7/KanaFlash WP7/*.png");

I get a "Error 1 An object reference is required for the non-static field, method, or property 'System.IO.IsolatedStorage.IsolatedStorageFile.GetFileNames(string)' C:\Users\lcopley\documents\visual studio 2010\Projects\KanaFlash WP7\KanaFlash WP7\MainPage.xaml.cs 50 37 KanaFlash WP7
"

Sorry about the formatting; I'm new at this.
[no name] 14-Jun-12 23:49pm    
See http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx for an example of how to use the IsolatedStorage class. I think that will solve your error.
Peltier Cooler 15-Jun-12 0:23am    
Wes, apparently I have an excess of stupid, or MS' documentation is more impenetrable than usual. The "example of how to use the IsolatedStorage class" you referenced assumes I am creating the directories, sub-directories and the files. I want to read and display a set of images I provide. How does this help me?

1 solution

Now I have little experience working with WP7 platform, but the
C#
bool silencer = false;

Seems out of place, (but that might not be your issue).
 
Share this answer
 
Comments
Peltier Cooler 13-Jun-12 13:34pm    
Out of place? I'm not sure what you mean. Could you elaborate?
Frank R. Haugen 13-Jun-12 17:13pm    
I think I must have been half asleep because I can't remember what made me say it, it made sense then, but now it don't :-S

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900