【发布时间】:2018-09-10 13:22:25
【问题描述】:
我正在尝试创建一个使用 Auth0 服务进行登录的 Xamarin Forms 应用程序。我一直按照他们关于如何使其工作的说明进行操作,但似乎每次都会崩溃。
URI 已创建且有效,因此客户端的实现看起来不错,但是当调用 StartActivity 行时出现此错误:
Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
我认为这与我使用 DependencyService 能够调用 Android 和 iOS 项目中的函数并且它以异步方式运行但我找不到其他任何事情可以使它工作这一事实有关。
我尝试使用 RunOnUiThread 和 BeginInvokeOnMainThread 但结果还是一样。
我已遵循这些说明和示例:
所以我创建了一个简单的视图,其中包含一个按钮,可以点击以使用他们的服务登录。此按钮调用委托服务,以便能够在 Android 和 iOS 中启动指令
登录按钮的 XAML 代码
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.LoginPage">
<ContentPage.Content>
<StackLayout VerticalOptions="StartAndExpand">
<Button x:Name="LoginButton" Text="Login / Signup" Clicked="OnLoginSignupButtonClicked" />
</StackLayout>
</ContentPage.Content>
调用依赖服务的按钮代码
void OnLoginSignupButtonClicked(object sender, EventArgs e)
{
DependencyService.Get<ILoginButton>().LoginRegisterButtonTap();
}
然后我执行他们在方法实现中提供的代码
public async void LoginRegisterButtonTap()
{
// Prepare for the login
try
{
authorizeState = await client.PrepareLoginAsync();
// Send the user off to the authorization endpoint
var uri = Android.Net.Uri.Parse(authorizeState.StartUrl);
var intent = new Intent(Intent.ActionView, uri);
intent.AddFlags(ActivityFlags.NoHistory);
StartActivity(intent);
} catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
【问题讨论】:
标签: android xamarin.forms auth0