简体   繁体   English

Xamarin.Forms DependencyService始终返回null

[英]Xamarin.Forms DependencyService always returns null

Hej,

im trying to load some data from the native iOS project using the Dependency Service. 我试图使用Dependency Service从本地iOS项目加载一些数据。

Its a simple task im trying to desirialize some Data that i had saved before, but regardless what i do it always returns null. 它是一个简单的任务,试图对我之前保存的某些数据进行反序列化,但是无论我做什么,它总是返回null。

If i check the values with the debugger in iOS project theyre never null. 如果我在iOS项目中使用调试器检查值,则它们永远不会为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. 一个疑问是您正在从依赖项服务中调用方法LoadApplication

var s = await ApplicationModel.DependencyService.LoadApplication(userName);

and method declaration is LoadApplicationModel . 方法声明为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 . 首先,您要反序列化文件内容,并将ApplicationModel对象存储在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. 看起来一切都很好,但是您无需创建m ,而是创建了一个新的ApplicationModel对象并将其返回。

return new ApplicationModel {Username = userName};

I think you should return m like this (for example): 我认为您应该这样返回m (例如):

using (var file = File.OpenText(userPath)) {
    var serializer = new JsonSerializer();
    var m = (ApplicationModel) serializer.Deserialize(file, typeof(ApplicationModel));
    m.Username = userName;
    return m;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.Forms DependencyService Get()方法在PCL中返回Null - Xamarin.Forms DependencyService Get() Method Returns Null in PCL Xamarin.Forms FindByName()始终返回null - Xamarin.Forms FindByName() always returns null Xamarin.Forms:使用DependencyService启动活动 - Xamarin.Forms: Start an activity using DependencyService 如何在Xamarin.Forms DependencyService中使用MOQ - How do I use MOQ with Xamarin.Forms DependencyService Xamarin.Forms可移植类中的C#DependencyService - C# DependencyService in Xamarin.Forms portable class Xamarins.Essentials 和 Xamarin.Forms DependencyService 有什么区别 - What is the difference between Xamarins.Essentials and Xamarin.Forms DependencyService Xamarin Forms FindByName 总是返回 null - Xamarin Forms FindByName always returns null json.deserialize返回null + xamarin.forms - json.deserialize returns null + xamarin.forms 如何使用 DependencyService 和接口委托将 xamarin.android 特定功能的方法传递给 xamarin.forms? - How to pass a method of xamarin.android specific functionnality to a xamarin.forms using DependencyService and a delegate of Interface? 如何使用 Xamarin.Forms 中的 DependencyService 在共享代码中获取平台特定代码的 static 成员? - How to get static member of platform specific code in shared code using DependencyService in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM