在我拔下手机并将其重新插入之前,从网络加载并保存到磁盘中的图像不会出现问题

时间:2015-10-12 09:00:55

标签: c# image windows-phone-8.1

我以为我的工作正常,因为我把手机拔掉了,当我把它插回来时,我把所有的图片都用了,但是,当我试着和它们一起工作时,似乎,它没有按预期工作,因为image.Properties.Width = 0。

你们可以帮助我让它工作吗?

这是代码:

主页

public sealed partial class MainPage : Page
{
    private GetFromJson get = new GetFromJson();
    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
        get.GetFullResponse(GlobalVariables.apiUrl + "/" + "api/"+ GlobalVariables.shopping + "/general");


    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }

    private void btnIngresarConEmail_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(IngresarConEmail));
    }

    private void btnCrearCuenta_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(CrearCuenta));
    }


}

全局变量

public class GlobalVariables
{
    public static string UserName;
    public static string EmailAddress;
    public static string Password;
    public static string Name;
    public static IList<Medio> Medios;
    public string Stores;

    public static string apiUrl = "http://ec2-52-8-2-140.us-west-1.compute.amazonaws.com";
    public static string shopping = "1";

}

GetFromJson

public class GetFromJson
{

    private Medio medio = new Medio();
    public async void GetFullResponse(string address)
    {
        try
        {
            var client = new HttpClient();

            var uri = new Uri(address);
            Debug.WriteLine(uri);
            var Response = await client.GetAsync(uri);
            var statusCode = Response.StatusCode;

            Response.EnsureSuccessStatusCode();

            var ResponseText = await Response.Content.ReadAsStringAsync();

            string myJson = ResponseText;

            Debug.WriteLine(myJson);

            medio.deserializeAndConvert(myJson);

        }

        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }
}

梅迪奥

public class Medio
{
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string img { get; set; }
    public string medios_id { get; set; } 

    public void deserializeAndConvert(string aaa)
    {
        JObject myGeneral = JObject.Parse(aaa);
        IList<JToken> results = myGeneral["resp"]["medios"].Children().ToList();

        IList<Medio> searchResults = new List<Medio>();
        foreach (JToken result in results)
        {
            Medio searchResult = JsonConvert.DeserializeObject<Medio>(result.ToString());
            searchResults.Add(searchResult);
            Debug.WriteLine(searchResults);

        }


        Debug.WriteLine(searchResults.Count);
        GlobalVariables.Medios = searchResults;

        var selected = GlobalVariables.Medios.Where(item => item.img!=null);
        Debug.WriteLine(GlobalVariables.Medios[0].ToString());

    }
}

将照片保存到磁盘(Windows Phone内存)

public sealed partial class CrearCuenta : Page
{

    public CrearCuenta()
    {

        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        QueryPicturesToShow();
    }

    public async void QueryPicturesToShow()
    {
        var pics = from medio in GlobalVariables.Medios
                   where medio.img != null
                   select new { Name = medio.name, Id = medio.id, Picture = medio.img };

        foreach (var item in pics)
        {
            Debug.WriteLine(item.Name);
            Debug.WriteLine(item.Id);
            Debug.WriteLine(item.Picture);



            await savePicToDisk(item.Picture, item.Name, item.Id);
        }
    }

    private async Task savePicToDisk(string picAddress, string picName, string picId)
    {
        StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("carpetaFunciona", CreationCollisionOption.OpenIfExists);
        StorageFile file = await folder.CreateFileAsync((picName + picId + ".png"), CreationCollisionOption.ReplaceExisting);

        string url = GlobalVariables.apiUrl + picAddress;

        Debug.WriteLine(url);
        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

        byte[] responseBytes = await client.GetByteArrayAsync(url);

        var stream = await file.OpenAsync(FileAccessMode.ReadWrite);

        using (var outputStream = stream.GetOutputStreamAt(0))
        {
            DataWriter writer = new DataWriter(outputStream);
            writer.WriteBytes(responseBytes);
            await writer.StoreAsync();
            await outputStream.FlushAsync();

        }
        Debug.WriteLine(file.Name);
    }

非常感谢!!!

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您的服务,所有图片都为空:

Null images