Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单依赖项服务非静态字段/属性_C#_Android_Xamarin.forms_Xamarin.android_Cross Platform - Fatal编程技术网

C# 表单依赖项服务非静态字段/属性

C# 表单依赖项服务非静态字段/属性,c#,android,xamarin.forms,xamarin.android,cross-platform,C#,Android,Xamarin.forms,Xamarin.android,Cross Platform,我正在使用依赖服务来获得特定于平台的接口实现。 假设我有以下界面: public interface IMyInterface { bool IsEnabled { get; set; } } 以及我的Android项目中的实现类: [assembly: Dependency(typeof(MyClass))] namespace App.Droid { class MyClass : IMyInterface { public bool IsEnabl

我正在使用依赖服务来获得特定于平台的接口实现。 假设我有以下界面:

public interface IMyInterface
{
    bool IsEnabled { get; set; }
}
以及我的Android项目中的实现类:

[assembly: Dependency(typeof(MyClass))]
namespace App.Droid
{
    class MyClass : IMyInterface
    {
        public bool IsEnabled { get; set; }
    }
} 
在代码中的某个点,我将
IsEnabled
设置为
true

之后,我开始一项新活动,使我的应用程序进入后台:

Intent intent = new Intent();
intent.SetAction(action);
intent.SetFlags(ActivityFlags.NewTask);

MainActivity.Instance.StartActivity(intent);
当我的应用程序返回前台时,我访问属性
IsEnabled
,得到的是
false
,而不是
true
。这实际上发生在Impmenting类的每个属性和私有字段上。当我离开应用程序进行新活动时,这些属性是否被垃圾收集


我发现解决此问题的唯一方法是将所有支持字段设置为静态,但这会在代码中产生大量开销,如果我知道此行为下的原因,这可能是不必要的。

不太理解问题的标题

如果使用单例模式,则可以在需要时基于唯一实例化对象提取属性。如下所示:

public class Singleton
    {
        // Define a static variable to hold an instance of the class
        private static Singleton uniqueInstance;

        // Define a private constructor so that the outside world cannot create instances of the class
        private Singleton()
        {
        }

        /// <summary>
        /// Define public methods to provide a global access point, and you can also define public properties to provide global access points
        /// </summary>
        /// <returns></returns>
        public static Singleton GetInstance()
        {
            // Create if the instance of the class does not exist, otherwise return directly
            if (uniqueInstance == null)
            {
                uniqueInstance = new Singleton();
            }
            return uniqueInstance;
        }
    }
private void SaveConnectionData(JSON.Connection C)
                    {
                        App.Current.Properties[Cryptography.Encryption("AccessToken")] = Cryptography.Encryption(C.Access_token);
                        App.Current.Properties[Cryptography.Encryption("ExpiresIn")] = Cryptography.Encryption(C.Expires_in.ToString());
                        App.Current.Properties[Cryptography.Encryption("TokenType")] = Cryptography.Encryption(C.Token_type);
                        App.Current.Properties[Cryptography.Encryption("Scope")] = Cryptography.Encryption(JsonConvert.SerializeObject(C.Scope));
                        App.Current.Properties[Cryptography.Encryption("RefreshToken")] = Cryptography.Encryption(C.Refresh_token);
                        App.Current.SavePropertiesAsync();
                    }
您可能参与使用<代码>生命周期> /代码>和<代码>通知< /代码>。如果有大量数据,请考虑使用<代码> SQLite 数据库保存此数据。可以参考此链接


更多:在Xamarin.Android中,您还可以尝试
生命周期
来显示保存的数据。如
OnResume
方法来显示数据。

您确定在Android项目中访问的实例与Xamarin.Forms项目中的实例完全相同吗?例如,如果您在Xamarin.Forms和new MyClass中使用DependencyService.Get(),请确保实例会有所不同。。。我希望在MyClass中应该有对另一个类实例的引用,例如singleton,它存储一些状态,并且其生存期与MyClass实例分离。谢谢您的评论。嗯,在我的Android项目中,我从不实例化这个类。我只在portable项目中实例化它。在Xamarin.Forms中,我有一个实例,在开始活动之前,我的实例属性设置了一些值/引用,但在新活动之后,仍然在Xamarin.Forms中,所有这些实例属性现在都具有默认值或null。但是android项目中的类仍然“工作”于它的方法,所以我想我会按照你的建议使用单例模式,我会让你知道的。再次感谢。请查看对答案的评论以了解最新更新。非常感谢您的准确答案。我很快就会试试,我会让你知道的。另外,在对问题的评论中,另一位用户建议我使用单例模式,这就是我下一步要做的。好的,如果有帮助的话,请告诉我,然后你可以投票。好的,我尝试过实现它,但这里有一些话要说。首先,我要使singleton的类(
MyClass
)派生自另一个具有公共构造函数的类。这阻止了我有一个单身汉。其次,我使用
var service=DependencyService.Get()检索实现类
默认情况下返回一个全局实例,因此它已经是一种
单例。我会想办法按照我在问题中说的做,也许会考虑你答案中关于数据库等的其他部分。如果是这样,我会将你的答案标记为正确。@Maxim Saplin@Junior Jiang-MSFT,我刚刚发现创建了两个
MyClass
实例。这应该是我的问题的原因。我刚刚发现,这是因为
MyClass
实现了另一个类,该类在特定事件发生时自动实例化(我问题中的代码只是一个示例,并不完整)。知道这一点,我可以妥善处理一切。谢谢你们,你们帮我找到了这个。我会接受答案,因为在正常情况下答案是正确的,但在我的情况下,我没有在我的问题中发布更多的东西,这起了作用。太好了,很高兴你找到了原因并解决了问题!