【发布时间】:2023-03-20 09:09:01
【问题描述】:
嘿嘿,
我正在尝试使用依赖服务从原生 iOS 项目加载一些数据。
这是一项简单的任务,我试图反序列化我之前保存的一些数据,但无论我做什么总是返回 null。
如果我在 iOS 项目中使用调试器检查值,它们永远不会为空。 很奇怪,也许我错过了什么......
我的接口实现:
[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};
}
}
在便携式项目中
DependencyService = Xamarin.Forms.DependencyService.Get<ISaveRead>();
var s = await ApplicationModel.DependencyService.LoadApplication(userName);
感谢您的宝贵时间。
【问题讨论】:
标签: c# xamarin.ios xamarin.forms