问题
This code it´s write in C# in visual studio 2012 I ´ve using winrt tool kit
Hi I tried create this loadGrupos to catch the file grupo.txt and read and load his content in to the class Grupos but I don´t know what´s happen the list grupos recive the content from the json but when I callend the loadGrupos the variable grupos don´t expose nothing. I really don´t know what´s going on. I tried debug and I didn´t found any error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Windows.Storage;
using Windows.ApplicationModel;
using WinRTXamlToolkit.IO.Extensions;
using Windows.UI.Popups;
namespace xxx;
public class MyJConverter
{
public String _Path;
//private Windows.ApplicationModel.Package package;
// private Windows.Storage.StorageFolder installedLocation;
private StorageFolder _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
//KnownFolders.DocumentsLibrary
public MyJConverter() {
// package = Windows.ApplicationModel.Package.Current;
// installedLocation = package.InstalledLocation;
}
public void save(Object obj)
{
_Path = JsonConvert.SerializeObject(obj);
gravandoUmDocumento("data",_Path);
}
public void saveGrupo(Object obj)
{
_Path = JsonConvert.SerializeObject(obj);
gravandoUmDocumento("grupo", _Path);
}
public async void gravandoUmDocumento(String nomeDoArquivo,String menssagem) {
// var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
// var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
//await menssagem.WriteToFile(nomeDoArquivo + "1.txt", KnownFolders.DocumentsLibrary);
await menssagem.WriteToFile(nomeDoArquivo + ".txt", _Folder);
}
private List<Ano> anos;
public List<Ano> load()
{
leituraDeUmArquivo("data").Wait();
if (anos != null)
{
return anos;
}
return null;
}
private List<Grupos> grupos;
public List<Grupos> Grupos
{
get { return grupos; }
set { grupos = value; }
}
public List<Grupos> loadGrupos()
{
leituraDeUmArquivo("grupo").Wait();
{
if (grupos != null)
{
return grupos;
}
else
return null;
}
}
public async Task leituraDeUmArquivo(String arquivoASerLido)
{
String leitura = "";
//leitura do data
try
{
// settings
var _Path =arquivoASerLido + ".txt";
// acquire file
var _File = await _Folder.GetFileAsync(_Path);
// read content
leitura = (String) await Windows.Storage.FileIO.ReadTextAsync(_File);
// leitura = (String)_ReadThis;
}
catch {
// leituraDeUmArquivo(arquivoASerLido);
}
if (leitura != "")
{
if (arquivoASerLido.Equals("data"))
{
try
{
anos = JsonConvert.DeserializeObject<List<Ano>>(leitura);
}
catch {
}
}
if (arquivoASerLido.Equals("grupo"))
{
try{
grupos = JsonConvert.DeserializeObject<List<Grupos>>(leitura);
}
catch { }
}
}
}
}
HI I did the modifications you recomend but the problem didn´t solved so I post all my code. I really didn´t found why the winrt can´t load the file, some time win 8 load some times no. Now with the modificiations the app block and do go to front.
if I go with debug the visual found the file, if I go only run no.
回答1:
There should be warning about "calling async method without awaiting" (or something like this).
You need to await
for completion of async
method instead of just calling it.
I.e. cheap and dirty (not good idea for WinRT) approach is to call Task.Wait on result of leituraDeUmArquivo
(the method need to be updated to return Task
):
leituraDeUmArquivo("grupo").Wait();
Better approach is to make code properly asynchronous and just await
call to leituraDeUmArquivo
(your loadGrupos
function will likely need to be marked async
too and handled appropriately).
Note: to make await
/ .Wait()
working leituraDeUmArquivo
should return Task
.
public async Task leituraDeUmArquivo(String arquivoASerLido)
// don't need to touch body of the method.
来源:https://stackoverflow.com/questions/16785336/calling-async-method-does-not-set-member-variable