本文介绍了尝试通过 AppShell 时无法找到资源 ID #0xffffffff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试并尝试过我在网上找到的解决方案,但到目前为止都没有奏效.

I've tried and tried solutions I find online and none have worked so far.

我有一个带有 LogInPageLoggedInPage 的 Xamarin 应用.开始时,我导航到 TabBar 中托管的 LogInPage,因此 FlyoutMenu 不可见,当用户正确登录时,我尝试将 Shell 移至 LoggedInPage,它是 FlyoutMenu 的一部分.

I have a Xamarin app with a LogInPage and a LoggedInPage. On start I navigate to the LogInPage, hosted within a TabBar, so the FlyoutMenu is not visible, and when the user logs in correctly I try to move the Shell to the LoggedInPage, which is part of a FlyoutMenu.

但是,应用程序崩溃并返回以下响应:

However, the app crashes and it returns the following response:

Android.Content.Res.Resources+NotFoundException Message=无法查找资源 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 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

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

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

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.

Added IAuth by a commenter's suggestion.

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 上跟踪.

This is an issue with Xamarin.Forms, which is already tracked on github.

此 PR 合并后,您需要等待 XF5 的下一个 SR 或回滚到 XF 4.8

You need to wait for next SR for XF5 when this PR is merged or rollback to XF 4.8

这篇关于尝试通过 AppShell 时无法找到资源 ID #0xffffffff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:50