Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串中的值不正确_C#_String_Windows Phone 7 - Fatal编程技术网

C# 字符串中的值不正确

C# 字符串中的值不正确,c#,string,windows-phone-7,C#,String,Windows Phone 7,我对字符串值有问题。字符串是全局声明的。字符串是name\u file\u image 我有以下代码: // iterate image files foreach (XElement node in xml.Element("graphics").Elements("file")) { // pick image if (node.Attribute("ty

我对字符串值有问题。字符串是全局声明的。字符串是
name\u file\u 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中的名称保存图像。但我有个问题。变量字符串始终具有相同的名称。我想代码是不正确的。有什么帮助吗?

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

在您的情况下,这将是: OpenReadAsync(新Uri(url\u图像),名称\u文件\u图像


在webClient_image eventhandler中,可以从OpenReadCompletedEventArgs参数中提取值。参数e有几个属性。e、 UserState现在应该包含您的文件名。

向我们显示错误消息或堆栈跟踪这没有错误消息。该变量仅拾取最后一个值。让我试着解释一下。我从xml中选取值,变量选取值并将文件保存到内存中。但它只保存最后一个。我认为下载速度比下载速度慢。。