Я пытаюсь использовать Xamarin.AccountStore
для сохранения учетных данных пользователя, чтобы им не приходилось вводить их каждый раз, когда они используют приложение.
Сохранение учетных данных выглядит примерно так:
Account account = new Account(...);
AccountStore store = AccountStore.Create();
store.Save(account, appName);
При эмуляции приложения из VS это работает точно так, как я ожидал. При работе на реальном телефоне через Xamarin Live метод Save
выдает исключение из-за того, что кажется отсутствующим методом в библиотеке Android.
Сообщение об исключении:
Конструктор типа «Java.Security.KeyStore + SecretKeyEntry» не найден.
Трассировка стека:
at System.RuntimeType.CreateInstanceImpl (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark& stackMark) [0x00213] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Activator.CreateInstance (System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x000ba] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at NInterpret.Interpreter.newobj (System.Collections.Generic.Dictionary`2[TKey,TValue] gargs, Mono.Cecil.MethodReference m, System.Object[] createInstanceArgs) [0x00305] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.interpretBlock (System.Object[] args, System.Object[] locals, Mono.Cecil.Cil.Instruction initialInstruction, Microsoft.FSharp.Collections.FSharpSet`1[T] inTries, Microsoft.FSharp.Core.FSharpOption`1[T] lastException) [0x03d5b] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.interpret (System.Object[] args) [0x00099] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.callAMethod (NInterpret.AMethod m, NInterpret.AType[] ptypes, NInterpret.AType[] genericMethodArgs, System.Collections.Generic.Dictionary`2[TKey,TValue] genericArgsIndex, System.Object[] args, System.Collections.Generic.List`1[T] byRefArgs, Microsoft.FSharp.Core.FSharpOption`1[T] byRefTarget, System.Object target, System.Boolean virtualCall) [0x00184] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.callMethodReference (System.Collections.Generic.Dictionary`2[TKey,TValue] gargs, Mono.Cecil.MethodReference ms, System.Boolean virtualCall) [0x00384] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.interpretBlock (System.Object[] args, System.Object[] locals, Mono.Cecil.Cil.Instruction initialInstruction, Microsoft.FSharp.Collections.FSharpSet`1[T] inTries, Microsoft.FSharp.Core.FSharpOption`1[T] lastException) [0x03c6e] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.interpret (System.Object[] args) [0x00099] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.callAMethod (NInterpret.AMethod m, NInterpret.AType[] ptypes, NInterpret.AType[] genericMethodArgs, System.Collections.Generic.Dictionary`2[TKey,TValue] genericArgsIndex, System.Object[] args, System.Collections.Generic.List`1[T] byRefArgs, Microsoft.FSharp.Core.FSharpOption`1[T] byRefTarget, System.Object target, System.Boolean virtualCall) [0x00184] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.callMethodReference (System.Collections.Generic.Dictionary`2[TKey,TValue] gargs, Mono.Cecil.MethodReference ms, System.Boolean virtualCall) [0x00384] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.interpretBlock (System.Object[] args, System.Object[] locals, Mono.Cecil.Cil.Instruction initialInstruction, Microsoft.FSharp.Collections.FSharpSet`1[T] inTries, Microsoft.FSharp.Core.FSharpOption`1[T] lastException) [0x03c6e] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
at NInterpret.Interpreter.interpretBlock (System.Object[] args, System.Object[] locals, Mono.Cecil.Cil.Instruction initialInstruction, Microsoft.FSharp.Collections.FSharpSet`1[T] inTries, Microsoft.FSharp.Core.FSharpOption`1[T] lastException) [0x0013c] in <5a7d38ec7eef0f2aa7450383ec387d5a>:0
Я проверял это на нескольких устройствах Android, все с одинаковыми результатами. Эмулятор работает под управлением Android 7.1, а телефоны работают под управлением версий 6. * и 7.1, поэтому не похоже, что это проблема версии Android.
Я также пытался очистить папки bin
и obj
, переустановить соответствующие пакеты Nuget, перезапустить VS и мой компьютер и т. Д., Но ни один из них не помог.
Что вызывает эти поведенческие различия между эмулируемыми и реальными устройствами?
Потенциально не связано:
В какой-то момент я обернул выражение Save
в try
/ catch
и вернул bool
, чтобы указать, удалось ли сохранить. В эмуляторе это возвращает true
после успешного сохранения. На «реальном» устройстве оператор catch
отключается (и должен возвращать false
), но после возврата к вызывающему коду возвращаемое значение не false
, а (null)
. Это расстраивает по многим причинам (Как ненулевому bool
было присвоено значение, отличное от true / false? Как я могу с уверенностью сделать что-нибудь , если я не могу доверять возвращаемым типам мои функции? и т. д.). Я понятия не имею, связано ли это поведение с рассматриваемым вопросом, но я включаю его сюда для полноты картины.