简体   繁体   中英

Xamarin.Forms Dependency Service non-static fields/properties

I'm using Dependency Service to get the platform specific implementation of an interface. Let's say I have the following interface:

public interface IMyInterface
{
    bool IsEnabled { get; set; }
}

And the implementing class in my Android project:

[assembly: Dependency(typeof(MyClass))]
namespace App.Droid
{
    class MyClass : IMyInterface
    {
        public bool IsEnabled { get; set; }
    }
} 

At some point in the code, I set IsEnabled to true .

After that, I start a new activity that makes my app go to background:

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

MainActivity.Instance.StartActivity(intent);

When my app returns to foreground, I access the property IsEnabled and I get false instead of true . This actually happens with every single property and private field of the impementing class. Are those properties garbage collected when I leave the app for a new activity?

The only way I found to solve this issue is to make all backing fields static , but this makes a lot of overhead in the code, which might be unnecessary if I knew the reasons under this behavoiur.

Not too understanding the title of your question.

If you use the singleton pattern, you can extract the properties based on the unique instantiation object when needed.Like this:

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;
        }
    }

If not , you can use Properties ( https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.application.properties?view=xamarin-forms)to access the data .Like this:

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();
                    }

You may be involved in the use of lifecycles and notifications .Also if there is a lot of data, consider using the SQLite database to save this data .Can refer to this link here

More :In Xamarin.Android, you also can try lifecycles to show saved data.Like OnResume method to show data.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM