Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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# 在iOS iPhone设备中测试xamarin应用程序时出错_C#_Xamarin.ios_Xamarin_Monodevelop_Keychain - Fatal编程技术网

C# 在iOS iPhone设备中测试xamarin应用程序时出错

C# 在iOS iPhone设备中测试xamarin应用程序时出错,c#,xamarin.ios,xamarin,monodevelop,keychain,C#,Xamarin.ios,Xamarin,Monodevelop,Keychain,我有这段代码 SecRecord rec=新的SecRecord(SecKind.GenericPassword); rec.Generic=NSData.FromString(“通过”); SecStatusCode res; SecRecord match=SecKeyChain.QueryAsRecord(rec,out res); if(res==SecStatusCode.Success) txtLogin.Text=match.ValueData.ToString(); SecRec

我有这段代码

SecRecord rec=新的SecRecord(SecKind.GenericPassword);
rec.Generic=NSData.FromString(“通过”);
SecStatusCode res;
SecRecord match=SecKeyChain.QueryAsRecord(rec,out res);
if(res==SecStatusCode.Success)
txtLogin.Text=match.ValueData.ToString();
SecRecord login=新的SecRecord(SecKind.GenericPassword){
Generic=NSData.FromString(“登录”)
};
SecStatusCode重新登录;
SecRecord matchlogin=SecKeyChain.QueryAsRecord(login,out reslogin);
if(reslogin==SecStatusCode.Success)
txtPassword.Text=matchlogin.ValueData.ToString();
当我在iPhone模拟器上调试时,一切正常,但当我开始在iPhone上调试时,它会给我一个错误

System.InvalidCastException: Cannot cast from source type to destination type.
at MonoTouch.Foundation.NSMutableDictionary.LowlevelFromObjectAndKey (IntPtr obj, IntPtr key) [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/.pmcs-compat.NSMutableDictionary.cs:408
at MonoTouch.Security.SecRecord..ctor (SecKind secKind) [0x0002d] in /Developer/MonoTouch/Source/maccore/src/Security/.pmcs-compat.Items.cs:619
at CompanyIOS.LoginController.ViewDidLoad () [0x00008] in /Users/nickolas/Projects/CompanyWorkSolution/CompanyIOS/LoginController.cs:38
at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38
at CompanyIOS.Application.Main (System.String[] args) [0x00008] in /Users/nickolas/Projects/CompanyWorkSolution/CompanyIOS/Main.cs:17

我不明白我错过了什么演员。请帮助我找出问题所在。

问题可能与使用“Generic”属性有关。这是我之前的回答。 以下是我的示例代码:

SecRecord existingRec=新的SecRecord(SecKind.GenericPassword){
服务=Keychain.USER\u服务,
标签=钥匙链。用户\u标签
};
var record=新的SecRecord(SecKind.GenericPassword){
服务=Keychain.USER\u服务,
Label=Keychain.USER\u标签,
帐户=用户名,
ValueData=NSData.FromString(密码),
可访问=可访问。始终
};
SecStatusCode code=SecKeyChain.Add(记录);
if(code==SecStatusCode.DuplicateItem){
代码=SecKeyChain.Remove(existingRec);
if(code==SecStatusCode.Success)
代码=SecKeyChain.Add(记录);
}

您还可以在LoginController中显示哪一行是第38行,因为这是它引发异常的地方吗?

我在MonoDevelop中也遇到了这个错误,我所做的任何事情都无法解决它。我最终将的源代码复制粘贴到我的解决方案中(您需要在构建设置中启用不安全的代码),瞧,它成功了:

var uri = new Uri("http://www.contoso.com");
MonoDevelop.MacInterop.Keychain.AddInternetPassword(uri, "ohad", "mypasswrd");
var pass = MonoDevelop.MacInterop.Keychain.FindInternetPassword(uri);
Console.WriteLine("Password: " + pass); //prints "mypasswrd"

38行为SecRecord rec=新的SecRecord(SecKind.GenericPassword);这对我没有帮助((