Hej,
im trying to load some data from the native iOS project using the Dependency Service.
Its a simple task im trying to desirialize some Data that i had saved before, but regardless what i do it always returns null.
If i check the values with the debugger in iOS project theyre never null. Very strange maybe im missing something ...
My Interface Implementation:
[assembly: Xamarin.Forms.Dependency(typeof(SaveRead))]
namespace LindeXF.iOS {
public class SaveRead : ISaveRead {
public async Task<ApplicationModel> LoadApplicationModel(string userName) {
try {
var userPath = CreatePathJson(userName);
if (File.Exists(userPath)) {
await Task.Run(() => {
using (var file = File.OpenText(userPath)) {
var serializer = new JsonSerializer();
var m = (ApplicationModel) serializer.Deserialize(file, typeof(ApplicationModel));
}
});
}
}
catch (Exception ex) {
throw new Exception(ex, "LoadApplicationModel()");
}
return new ApplicationModel {Username = userName};
}
}
in the Portable Project
DependencyService = Xamarin.Forms.DependencyService.Get<ISaveRead>();
var s = await ApplicationModel.DependencyService.LoadApplication(userName);
thanks for your Time.
One doubt you are calling method LoadApplication
from dependency service.
var s = await ApplicationModel.DependencyService.LoadApplication(userName);
and method declaration is LoadApplicationModel
.
public async Task<ApplicationModel> LoadApplicationModel(string userName) {
}
maybe that's the problem? Try using below code for call:
var data = await DependencyService.Get<ISaveRead>()
.LoadApplicationModel(userName);
First you deserialize the file contents and store the ApplicationModel object in m
.
using (var file = File.OpenText(userPath)) {
var serializer = new JsonSerializer();
var m = (ApplicationModel) serializer.Deserialize(file, typeof(ApplicationModel));
}
That looks all good but instead of returning m
, you create a new ApplicationModel object and return that.
return new ApplicationModel {Username = userName};
I think you should return m
like this (for example):
using (var file = File.OpenText(userPath)) {
var serializer = new JsonSerializer();
var m = (ApplicationModel) serializer.Deserialize(file, typeof(ApplicationModel));
m.Username = userName;
return m;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.