xamarin 尝试在AppShell中移动时找不到资源ID #0xffffffff

qnzebej0  于 2022-12-07  发布在  Shell
关注(0)|答案(1)|浏览(125)

我试了又试我在网上找到的解决方案,到目前为止没有一个有效。
我有一个Xamarin应用程序,有一个LogInPage和一个LoggedInPage。在开始时,我导航到LogInPage,托管在TabBar中,所以FlyoutMenu是不可见的,当用户正确登录时,我尝试将Shell移动到LoggedInPage,它是FlyoutMenu的一部分。
但是,应用程序崩溃,并返回以下响应:
Android.Content.Res.Resources+NotFoundException消息=无法找到资源ID #0xffffffff
我的代码如下:
应用程序

<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>

应用程序 shell

<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>

LogInPage.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 "";
            }
        }
    }
}
ffvjumwh

ffvjumwh1#

这是Xamarin.Forms的一个问题,它已经是tracked on github了。
this PR is merged或回滚到XF 4.8时,您需要等待XF5的下一个SR

相关问题