当我杀死正在运行的实例并使用Db ecncryption应用程序重新启动应用程序时崩溃

时间:2017-11-06 10:31:53

标签: xamarin

我在场景下实现了代码

namespace Inspect.Droid
{

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{             
    private DataBaseEncryption dbEncryption;             
    protected override async void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        MainAppActivity = this;
        Forms.Init(this, bundle);
        //to get the current device language code
        var code = Locale.Default.GetDisplayLanguage(Locale.Default);
        CommonInfo.Instance.DeviceCurrentLanguageCode = code.Substring(0, 2).ToUpper();
        Console.WriteLine("Device lang code: " + CommonInfo.Instance.DeviceCurrentLanguageCode);
        try
        {
            ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);
        }
        catch (Exception ex)
        {
            //when application coming back from background throws exception, bcz PCLAppConfig is already initialized, so as there is no change no need to change here
            Console.WriteLine(ex.Message);
        }
        GLOBAL_CONTEXT = Android.App.Application.Context;
        ACTIVITY_CONTEXT = this;
        FragmentMgr = FragmentManager;
        dbEncryption = new DataBaseEncryption();
        //await PrepareDb();
        await  dbEncryption.InitiateDecryption();
        //AzureMobileServices Initialization for Android
        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
        SessionInfo.Instance.DeviceId = Utility.GenerateGuid().ToString();

        //This will set the value for _skipCount variable in all respected helper classes.
        SetSkipCount();
        Inspect.ScreenSize = new Xamarin.Forms.Size(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density,
        Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);

        Inspect.AndroidDisplayMetricsDensity = Resources.DisplayMetrics.Density;
        int statusBarResId = Resources.GetIdentifier("status_bar_height", "dimen", "android");
        if (statusBarResId > 0)
        {
            Inspect.AndroidStatusBarHeight = Resources.GetDimensionPixelSize(statusBarResId);
        }

        Context context = this.ApplicationContext;
        Acr.UserDialogs.UserDialogs.Init(this);

        //allowing the device to change the screen orientation based on the rotation
        MessagingCenter.Subscribe<InspectionAddNotesPage>(this, CommonConstants.AllowLandScape, sender =>
        {
            RequestedOrientation = ScreenOrientation.Unspecified;
        });
        //during page close setting back to portrait
        MessagingCenter.Subscribe<InspectionAddNotesPage>(this, CommonConstants.PreventLandScape, sender =>
        {
            RequestedOrientation = ScreenOrientation.Portrait;
        });
        LoadApplication(new Inspect());
        //Intializing the context for Media access
        CrossCurrentActivity.Current.Activity = this;
        PasscodeAuthDependency.Activity = this;
    }



    protected override void OnStop()
    {
        base.OnStop();

        dbEncryption.InitiateEncryption(true);

    }
    protected override void OnRestart()
    {
        base.OnRestart();
        dbEncryption.InitiateDecryption();


    }
    protected override void OnDestroy()
    {
        base.OnDestroy();

    }

请提供解决方案。

1 个答案:

答案 0 :(得分:1)

你的例外究竟来自哪里?你有没有成功获得起源? 如果您正在讨论ConfigurationManager.Initialize()方法中的异常,请尝试此操作。

if(Configurationmanager.AppSettings == null)
{
    ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);
}

如果ConfigurationManager已经找到了初始化的NameValueCollection,那么它只会抛出异常。