字符串中的值不正确
本文关键字:不正确 字符串 | 更新日期: 2023-09-27 18:14:33
我有一个字符串值的问题。该字符串是全局声明的。字符串为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中的名称保存图像。但我有个问题。变量字符串总是具有相同的名称。我想代码是不正确的。任何帮助吗?
您正在调用WebClient。OpenReadAsync方法,它正在做他的工作(顾名思义)异步。要解决这个问题,你必须通过OpenReadAsync()方法传递文件名。
在您的情况下,将是:webClient。OpenReadAsync(new Uri(url_image), name_file_image);
在webClient_image事件处理程序中,你可以从OpenReadCompletedEventArgs参数中提取值。参数e有几个属性。userstate现在应该包含你的文件名