Xamarin.Forms 依赖服务非静态 fields/properties
Xamarin.Forms Dependency Service non-static fields/properties
我正在使用依赖服务来获取接口的平台特定实现。
假设我有以下界面:
public interface IMyInterface
{
bool IsEnabled { get; set; }
}
以及在我的 Android 项目中实施 class:
[assembly: Dependency(typeof(MyClass))]
namespace App.Droid
{
class MyClass : IMyInterface
{
public bool IsEnabled { get; set; }
}
}
在代码中的某个位置,我将 IsEnabled
设置为 true
。
之后,我启动了一个新的 activity 让我的应用程序进入后台:
Intent intent = new Intent();
intent.SetAction(action);
intent.SetFlags(ActivityFlags.NewTask);
MainActivity.Instance.StartActivity(intent);
当我的应用 returns 进入前台时,我访问 属性 IsEnabled
并且我得到 false
而不是 true
。这实际上发生在每一个 属性 和实现 class 的私有字段上。当我离开应用程序以获取新的 activity 时,是否会收集这些属性垃圾?
我发现解决这个问题的唯一方法是制作所有支持字段 static
,但这会在代码中产生很多开销,如果我知道这种行为下的原因,这可能是不必要的。
不太理解你的问题。
如果你使用的是单例模式,那么在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;
}
}
如果没有,可以使用Properties
(https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.application.properties?view=xamarin-forms)to访问数据。像这样:
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();
}
你可能会涉及到lifecycles
和notifications
的使用。另外如果数据量很大,可以考虑使用SQLite
数据库保存这些数据。可以参考对此 link here
更多:在Xamarin.Android中,您也可以尝试lifecycles
显示保存的data.LikeOnResume
方法显示数据.
我正在使用依赖服务来获取接口的平台特定实现。 假设我有以下界面:
public interface IMyInterface
{
bool IsEnabled { get; set; }
}
以及在我的 Android 项目中实施 class:
[assembly: Dependency(typeof(MyClass))]
namespace App.Droid
{
class MyClass : IMyInterface
{
public bool IsEnabled { get; set; }
}
}
在代码中的某个位置,我将 IsEnabled
设置为 true
。
之后,我启动了一个新的 activity 让我的应用程序进入后台:
Intent intent = new Intent();
intent.SetAction(action);
intent.SetFlags(ActivityFlags.NewTask);
MainActivity.Instance.StartActivity(intent);
当我的应用 returns 进入前台时,我访问 属性 IsEnabled
并且我得到 false
而不是 true
。这实际上发生在每一个 属性 和实现 class 的私有字段上。当我离开应用程序以获取新的 activity 时,是否会收集这些属性垃圾?
我发现解决这个问题的唯一方法是制作所有支持字段 static
,但这会在代码中产生很多开销,如果我知道这种行为下的原因,这可能是不必要的。
不太理解你的问题。
如果你使用的是单例模式,那么在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;
}
}
如果没有,可以使用Properties
(https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.application.properties?view=xamarin-forms)to访问数据。像这样:
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();
}
你可能会涉及到lifecycles
和notifications
的使用。另外如果数据量很大,可以考虑使用SQLite
数据库保存这些数据。可以参考对此 link here
更多:在Xamarin.Android中,您也可以尝试lifecycles
显示保存的data.LikeOnResume
方法显示数据.