字符串中的值不正确

字符串中的值不正确

本文介绍了字符串中的值不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串值的问题.该字符串是全局声明的.字符串是 name_file_image.

I have a problem with a string value. The string is globally declared. The string is name_file_image.

我有这个代码:

            // iterate image files
            foreach (XElement node in xml.Element("graphics").Elements("file"))
            {
                // pick image
                if (node.Attribute("type").Value == "image")
                {

                    // demoing that we found something
                    //MessageBox.Show(node.Element("fileurl").Value);
                    string url_image = node.Element("fileurl").Value;
                    string[] array = url_image.Split('/');
                    **name_file_image** = array[array.Length - 1];
                    //MessageBox.Show(**name_file_image**);
                    WebClient webClient = new WebClient();
                    webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_image);
                    webClient.OpenReadAsync(new Uri(url_image));
                }
            }

    void webClient_image(object sender, OpenReadCompletedEventArgs e)
    {
        try
        {
            if (e.Result != null)
            {
                // Save image to Isolated Storage


                // Create virtual store and file stream. Check for duplicate JPEG files.
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(**name_file_image**))
                    {
                        myIsolatedStorage.DeleteFile(**name_file_image**);
                    }

                    IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(name_file_image, FileMode.Create, myIsolatedStorage);


                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(e.Result);

                    WriteableBitmap wb = new WriteableBitmap(bitmap);
                    wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                    fileStream.Close();

                    //MessageBox.Show(name_file_image);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我从内存中的文件中读取了 XML,我想使用 XML 中存在的名称保存图像.但我有一个问题.变量字符串始终具有相同的名称.我想代码不正确.有什么帮助吗?

I read the XML from a file in memory and I want to save the images with the name present in the XML. But I have a problem. The variable string always has the same name. I suppose the code is not correct. Any help?

推荐答案

您正在调用 WebClient.OpenReadAsync 方法,该方法正在(顾名思义)异步执行他的工作.要解决此问题,您必须使用 OpenReadAsync() 方法传递文件名.

You're calling the WebClient.OpenReadAsync method, which is doing his work (as the name suggests) asynchronous. To fix this issue, you have to pass the filename with the OpenReadAsync() method.

在你的情况下,这将是:webClient.OpenReadAsync(new Uri(url_image), name_file_image);

In your case that will be:webClient.OpenReadAsync(new Uri(url_image), name_file_image);

在您的 webClient_image 事件处理程序中,您可以从 OpenReadCompletedEventArgs 参数中提取值.参数 e 有几个属性.e.UserState 现在应该包含您的文件名.

In your webClient_image eventhandler you can than extract the value from the OpenReadCompletedEventArgs parameter. The parameter e has a few properties. e.UserState should now contain your filename.

这篇关于字符串中的值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:38