初始化 FileStream Windows Phone 7 时出现 System.MethodAccessException

发布于 2024-12-08 17:40:05 字数 2800 浏览 2 评论 0原文

我正在制作一个 WP7,它从图库或相机获取图像,然后按下按钮将其编码为 Base64 字符串,将其发送到 Web 服务。我目前正在使用 VS2010 中包含的 WP7 模拟器。

为此,我尝试使用 FileStream 对象来打开存储在图像路径中的图像。但是,当我尝试初始化 FileStream 时,我在控制台中收到以下消息:

“System.MethodAccessException”类型的第一次机会异常 发生在LiveAndesApp.dll

“taskhost.exe”(托管):已加载“System.ServiceModel.Web.dll”

第一 类型“System.Xml.XmlException”的偶然异常发生在 System.Xml.dll

后面跟着很多很多的System.Xml.XmlException。奇怪的是,我将 FileStream 创建放在捕获 System.MethodAccessException 和 E 的 try-catch 语句中,程序甚至不输入它,它只是继续 sendSighting

我做错了什么以及我该怎么办改善这个?多谢!

这是完整的代码。这就是我调用转换图片的方法的方法。

public void next_Click(object sender, EventArgs e)
        {
            //Dependera de si seguimos flujos offline y online. 
            if (!offline_mode)
            {
                NavigationService.Navigate(new Uri("/Pages/SendInformation.xaml?mode=online", UriKind.Relative));
                Controller c = new Controller();
                c.sendSighting();
            }
            else { NavigationService.Navigate(new Uri("/Pages/SendInformation.xaml?mode=offline", UriKind.Relative)); }

这是控制器类的代码。为了简洁起见,我省略了与网络请求相关的所有内容:

    public class Controller
    {
        public Controller()
        { }
    
        /// <summary>
        /// Manda un avistamiento al servicio.
        /// </summary>
        public void sendSighting()
        {
            //Obtenemos el avistamiento
            AddSightingFlowObject flow_object = AddSightingFlowObject.getInstance();
    
            //Creamos el objeto json y lo incorporamos al body del request.
            JObject json = new JObject();
    
            //Si la imagen no es nula, tenemos que procesarla.
            JArray arr = new JArray(new List<String>());
            if (flow_object.ImageControl != null)
            {
                String image_base_64 = ConvertImageToBase64(flow_object.ImagePath);
                arr.Add(image_base_64);
            }
            else
            {
                arr.Add("");
            }
            json.Add("PhotoURL", arr);
        }
    
    
        public String ConvertImageToBase64(String imagePath)
        {
            String image_base_64 = "";
            FileStream fs;
            Byte[] arr;
    
            try
            {
                fs = new FileStream(imagePath, FileMode.Open);
                arr = new Byte[fs.Length];
                fs.Read(arr, 0, arr.Length);
                image_base_64 = System.Convert.ToBase64String(arr);
            }
            catch (System.MethodAccessException e)
            {
                String error = "Error: " + e.Message + "Stack Trace: " + e.StackTrace;
            }
    
            return image_base_64;
        }

}

感谢您的宝贵时间! :D

I'm making a WP7 that takes an image from either the gallery or the camera and by pressing a button sends it to a webservice by encoding it to a base64 string. I'm currently using the WP7 Emulator included in VS2010.

To do this, I try to use a FileStream object that will open the image stored in the image path. However, when I try to initialize the FileStream, I get in the console the message:

A first chance exception of type 'System.MethodAccessException'
occurred in LiveAndesApp.dll

'taskhost.exe' (Managed): Loaded 'System.ServiceModel.Web.dll'

A first
chance exception of type 'System.Xml.XmlException' occurred in
System.Xml.dll

Followed by lots and lots of the System.Xml.XmlException. The weird thing is that I put the FileStream creation in a try-catch statement that catches System.MethodAccessException and E, and the program doesn't even enter it, it just goes on with the sendSighting

What am I doing wrong and how can I improve this? Thanks a lot!

Here is the complete code. This is how I call the method for converting the picture.

public void next_Click(object sender, EventArgs e)
        {
            //Dependera de si seguimos flujos offline y online. 
            if (!offline_mode)
            {
                NavigationService.Navigate(new Uri("/Pages/SendInformation.xaml?mode=online", UriKind.Relative));
                Controller c = new Controller();
                c.sendSighting();
            }
            else { NavigationService.Navigate(new Uri("/Pages/SendInformation.xaml?mode=offline", UriKind.Relative)); }

This is the code for the Controller class. I omitted everything related to the web request for the sake of brevity:

    public class Controller
    {
        public Controller()
        { }
    
        /// <summary>
        /// Manda un avistamiento al servicio.
        /// </summary>
        public void sendSighting()
        {
            //Obtenemos el avistamiento
            AddSightingFlowObject flow_object = AddSightingFlowObject.getInstance();
    
            //Creamos el objeto json y lo incorporamos al body del request.
            JObject json = new JObject();
    
            //Si la imagen no es nula, tenemos que procesarla.
            JArray arr = new JArray(new List<String>());
            if (flow_object.ImageControl != null)
            {
                String image_base_64 = ConvertImageToBase64(flow_object.ImagePath);
                arr.Add(image_base_64);
            }
            else
            {
                arr.Add("");
            }
            json.Add("PhotoURL", arr);
        }
    
    
        public String ConvertImageToBase64(String imagePath)
        {
            String image_base_64 = "";
            FileStream fs;
            Byte[] arr;
    
            try
            {
                fs = new FileStream(imagePath, FileMode.Open);
                arr = new Byte[fs.Length];
                fs.Read(arr, 0, arr.Length);
                image_base_64 = System.Convert.ToBase64String(arr);
            }
            catch (System.MethodAccessException e)
            {
                String error = "Error: " + e.Message + "Stack Trace: " + e.StackTrace;
            }
    
            return image_base_64;
        }

}

Thank you for your time! :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2024-12-15 17:40:05

使用隔离存储代替 System.IO

FileStream 是 System.IO 命名空间的一部分,可以替换为诸如isolatedStorageFileStream之类的东西

带有帮助的链接

Use Isolated Storage instead of System.IO

FileStream is part of System.IO namespace which can be replaced with smth like IsolatedStorageFileStream

Link with help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文