我已经尝试了很多在线解决方案,但到目前为止都没有成功。
我有一个带有LogInPage
和LoggedInPage
的Xamarin应用程序。启动时,我导航到LogInPage
,托管在TabBar
中,因此FlyoutMenu
不可见,当用户正确登录时,我尝试将Shell移动到LoggedInPage
,它是FlyoutMenu
的一部分。
然而,该应用程序崩溃,并返回以下响应:
Android.Content.Resources+NotFoundException消息=无法查找资源ID#0xfffffffff
我的代码如下:
应用
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FinalProject.App">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="TextColor" Value="White"></Setter>
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="DodgerBlue" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Transparent" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
AppShell
<Shell x:Name="MainShell" xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FinalProject.AppShell"
Title="FinalProject"
xmlns:core="clr-namespace:FinalProject"
xmlns:coreviews="clr-namespace:FinalProject.Views">
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="DodgerBlue" />
<Setter Property="Shell.ForegroundColor" Value="White" />
<Setter Property="Shell.TitleColor" Value="White" />
<Setter Property="Shell.DisabledColor" Value="Transparent" />
<Setter Property="Shell.UnselectedColor" Value="Transparent" />
<Setter Property="Shell.TabBarBackgroundColor" Value="DodgerBlue" />
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="Transparent"/>
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
<Style Class="FlyoutItemLabelStyle" TargetType="Label">
<Setter Property="TextColor" Value="White"></Setter>
</Style>
<Style Class="FlyoutItemLayoutStyle" TargetType="Layout" ApplyToDerivedTypes="True">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{x:OnPlatform UWP=Transparent, iOS=White}" />
<Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="DodgerBlue" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="DodgerBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
<Style Class="MenuItemLayoutStyle" TargetType="Layout" ApplyToDerivedTypes="True">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="DodgerBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
</Shell.Resources>
<FlyoutItem Title="Main">
<ShellContent Route="LoggedPage" ContentTemplate="{DataTemplate coreviews:LoggedPage}" />
</FlyoutItem>
<FlyoutItem Title="Trial">
<ShellContent Route="TrialPage" ContentTemplate="{DataTemplate coreviews:TrialPage}" />
</FlyoutItem>
<MenuItem Text="Logout" StyleClass="MenuItemLayoutStyle" Clicked="Logout">
</MenuItem>
<TabBar>
<ShellContent Route="LoginPage" ContentTemplate="{DataTemplate coreviews:LogInPage}" />
</TabBar>
登录页面.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FinalProject.Views.LogInPage"
BackgroundColor="White">
<ContentPage.Content>
<StackLayout
VerticalOptions="Center"
Margin="20">
<Label
Text="Caminante"
HorizontalOptions="Center"
TextColor="Black"
FontSize="35"
Margin="0, 20"
/>
<Entry
Placeholder="E-mail"
PlaceholderColor="Black"
TextColor="Black"
Keyboard="Email"
x:Name="EmailInput"
/>
<Entry
Placeholder="Password"
PlaceholderColor="Black"
TextColor="Black"
IsPassword="true"
x:Name="PasswordInput"
/>
<Button
Text="Enter"
Clicked="LoginClicked"
Margin="60, 40"
BackgroundColor="DodgerBlue"
TextColor="White"
/>
<Button
Text="Register"
Clicked="RegisterClicked"
Margin="60, 40"
BackgroundColor="DodgerBlue"
TextColor="White"
/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
LogInPage.xaml.cs
using FinalProject.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace FinalProject.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LogInPage : ContentPage
{
IAuth auth;
public LogInPage()
{
InitializeComponent();
auth = DependencyService.Get<IAuth>();
//Routing
}
async void LoginClicked(object sender, EventArgs e)
{
string Token = await auth.LoginWithEmailPassword(EmailInput.Text, PasswordInput.Text);
if (Token != "")
{
Storage.uid = Token;
Storage.userEmail = EmailInput.Text;
await Shell.Current.GoToAsync("///LoggedPage");
//Application.Current.MainPage = new AppShell();
}
else
{
ShowError();
}
}
async void RegisterClicked(object sender, EventArgs e)
{
await auth.RegisterWithEmailPassword(EmailInput.Text, PasswordInput.Text);
string Token = await auth.LoginWithEmailPassword(EmailInput.Text, PasswordInput.Text);
await FireBaseActions.AddUser(Token, EmailInput.Text);
LoginClicked(sender, e);
}
async private void ShowError()
{
await DisplayAlert("Authentication Failed", "E-mail or password are incorrect. Try again!", "OK");
}
}
}
LoggedPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FinalProject.Views.LoggedPage">
<ContentPage.Content>
<StackLayout x:Name="slLoggedIn1">
<Label Text="Working?"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
LoggedPage.xaml.cs
namespace FinalProject.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoggedPage : ContentPage
{
public string noChars { get; set; }
public List<Character> lista { get; set; }
public bool Visible { get; set; }
public bool notVisible { get { return !Visible; } }
public LoggedPage()
{
BindingContext = this;
noChars = "Hello, " + Services.Storage.userEmail + "!" + Environment.NewLine + "It seems you don't have any characters yet, press the button below to fix that.";
if (FireBaseActions.GetUserCharacters().Result.Count.Equals(0) || FireBaseActions.GetUserCharacters().Result.Equals(null))
{
lista = new List<Character>();
Visible = false;
}
else
{
lista = FireBaseActions.GetUserCharacters().Result;
Visible = true;
}
InitializeComponent();
}
编辑:根据一位评论者的建议添加了IAuth。
IAuth实现类:
[assembly: Dependency(typeof(AuthDroid))]
namespace CaminanteFinal.Droid
{
class AuthDroid : IAuth
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
try
{
var user = await FirebaseAuth.Instance.SignInWithEmailAndPasswordAsync(email, password);
var token = await (FirebaseAuth.Instance.CurrentUser.GetIdToken(false).AsAsync<GetTokenResult>());
//return FirebaseAuth.Instance.CurrentUser.Uid;
return token.Token;
}
catch (FirebaseAuthInvalidUserException e)
{
Acr.UserDialogs.UserDialogs.Instance.Alert(e.ErrorCode, "Error", "Ok");
e.PrintStackTrace();
return "";
}
catch (FirebaseAuthInvalidCredentialsException e)
{
Acr.UserDialogs.UserDialogs.Instance.Alert(e.ErrorCode, "Error", "Ok");
e.PrintStackTrace();
return "";
}
}
public async Task<string> RegisterWithEmailPassword(string email, string password)
{
try
{
var user = await FirebaseAuth.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
Acr.UserDialogs.UserDialogs.Instance.Alert("User " + email + " with password " + password + " has been created.", "Success!", "Ok");
return "";
}
catch (FirebaseAuthUserCollisionException e)
{
Acr.UserDialogs.UserDialogs.Instance.Alert(e.ErrorCode, "Error", "Ok");
e.PrintStackTrace();
return "";
}
catch (FirebaseAuthWeakPasswordException e)
{
Acr.UserDialogs.UserDialogs.Instance.Alert(e.ErrorCode, "Error", "Ok");
e.PrintStackTrace();
return "";
}
catch (FirebaseAuthInvalidCredentialsException e)
{
Acr.UserDialogs.UserDialogs.Instance.Alert(e.ErrorCode, "Error", "Ok");
e.PrintStackTrace();
return "";
}
}
}
}
这是Xamarin.Forms的问题,它已经在github上被跟踪了。
当此PR合并或回滚到XF 4.8 时,您需要等待XF5的下一个SR