Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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# 使用Xamarin表单应用程序触摸Id_C#_Xamarin_Xamarin.forms_Xamarin.ios_Touch Id - Fatal编程技术网

C# 使用Xamarin表单应用程序触摸Id

C# 使用Xamarin表单应用程序触摸Id,c#,xamarin,xamarin.forms,xamarin.ios,touch-id,C#,Xamarin,Xamarin.forms,Xamarin.ios,Touch Id,我们正在尝试使用Xamarin表单应用程序在iOS上使用Touch Id 在我们的Xamarin表单应用程序中,在App.Xaml.cs构造函数中,我们使用一个接口来引用iOS本机touch id实现: bool _authenticatedWithTouchID = DependencyService.Get<ITouchID>().AuthenticateUserIDWithTouchID(); if (_authenticatedWithTouchID)

我们正在尝试使用Xamarin表单应用程序在iOS上使用Touch Id

在我们的Xamarin表单应用程序中,在App.Xaml.cs构造函数中,我们使用一个接口来引用iOS本机touch id实现:

bool _authenticatedWithTouchID = DependencyService.Get<ITouchID>().AuthenticateUserIDWithTouchID();
            if (_authenticatedWithTouchID)
            {
                MainPage = new NavigationPage(new MainPage());
            }
            else
            {
                MainPage = new NavigationPage(new LoginPage());
            }
这是iOS项目中接口的实现:

[assembly: Dependency(typeof(TouchID))]
namespace GetIn.iOS
{
public class TouchID : ITouchID
{
        public bool AuthenticateUserIDWithTouchID()
        {
            bool outcome = false;

            var context = new LAContext();
            if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out NSError AuthError))
            {
                var replyHandler = new LAContextReplyHandler((success, error) => {

                    Device.BeginInvokeOnMainThread(() => {
                        if (success)
                        {
                            outcome = true;
                        }
                        else
                        {
                            outcome = false;
                        }
                    });

                });
                context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "Logging with touch ID", replyHandler);
            };
            return outcome;
        }
    }
}
我们从结果变量(如果用户经过身份验证,则为true)获得响应,但该响应不会传递回表单应用程序

我们也尝试过使用异步任务,但运气不佳


有没有推荐的方法?我们正努力使这一点尽可能简单。

您需要更改代码以处理异步行为

public class TouchID : ITouchID
{
    public Task<bool> AuthenticateUserIDWithTouchID()
    {
        var taskSource = new TaskCompletionSource<bool>();            

        var context = new LAContext();
        if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out NSError AuthError))
        {
            var replyHandler = new LAContextReplyHandler((success, error) => {

                taskSource.SetResult(success);                    

            });
            context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, "Logging with touch ID", replyHandler);
        };

        return taskSource.Task;
    }
}
并更改接口声明

公共接口ITouchID
{
任务AuthenticateUserIDWithTouchID();
}
最后是你的Xamarin.Forms代码

var touchId=DependencyService.Get();
var_authenticatedWithTouchID=等待touchId.authenticateUserWithTouchId();
如果(_使用TouchID进行身份验证)
{
主页=新导航页(新主页());
}
其他的
{
主页=新导航页面(新登录页面());
}

通过使用上面的异步更改(尽管不使用异步方法也可以做到这一点),然后执行以下操作,实现了这一点:

通过将以下代码从app.xaml.cs移到MainPage.xaml.cs(我们的选项卡式页面)构造函数并进行调整

var touchId = DependencyService.Get<ITouchID>();
var _authenticatedWithTouchID = await 
touchId.AuthenticateUserIDWithTouchID();
 if (_authenticatedWithTouchID)
 {
  //Do Nothing as on this page
 }
 else
 {
  //Go back to login page
    Navigation.InsertPageBefore(new LoginPage(), this);
    await Navigation.PopAsync(); 
 }
var touchId=DependencyService.Get();
var\u authenticatedWithTouchID=wait
touchId.AuthenticateUserIDWithTouchID();
如果(_使用TouchID进行身份验证)
{
//不执行本页上的操作
}
其他的
{
//返回登录页面
InsertPageBefore(new LoginPage(),this);
等待导航。PopAsync();
}

Ajam,您可以查看这篇文章。您好,感谢您的回复,我们正在为我们的代码寻找解决方案,而不是使用插件。您需要taskcompletionsource,因为您的代码是异步的,但您的代码是同步的。您好,由于您提供的函数现在是异步的,我们必须将其移到onStart()函数中。。。。受保护的重写异步void OnStart()。我们仍然在iOS项目的主要中得到未处理的异常:基础。名称:NSInternalInconsistencyException原因:应用程序启动结束时,应用程序窗口应具有根视图控制器
using System.Threading.Tasks;
public interface ITouchID
{
    Task<bool> AuthenticateUserIDWithTouchID();
}
 var touchId = DependencyService.Get<ITouchID>();
 var _authenticatedWithTouchID = await touchId.AuthenticateUserIDWithTouchID();
 if (_authenticatedWithTouchID)
 {
   MainPage = new NavigationPage(new MainPage());
 }
 else
 {
   MainPage = new NavigationPage(new LoginPage());
 }
var touchId = DependencyService.Get<ITouchID>();
var _authenticatedWithTouchID = await 
touchId.AuthenticateUserIDWithTouchID();
 if (_authenticatedWithTouchID)
 {
  //Do Nothing as on this page
 }
 else
 {
  //Go back to login page
    Navigation.InsertPageBefore(new LoginPage(), this);
    await Navigation.PopAsync(); 
 }