【问题标题】:xamarin form stuck on blank screenxamarin 表单卡在空白屏幕上
【发布时间】:2019-12-16 11:51:30
【问题描述】:

我对 xamarin 形式的移动应用程序进行了一些更改,当我在手机或模拟器上调试它时,什么都没有显示,只是我希望看到登录屏幕的 emtpt 页面。 没有错误抛出,但应用程序无法正常工作。 在 xamarin 表单上使用 Prism。在 4.1 和所有其他 prism 组件上更新

[编辑]

这是 Prism 应用程序类

public partial class App : PrismApplication
    {
        public App() : this(null) { }

    public App(IPlatformInitializer initializer) : base(initializer) { }

    protected override async void OnInitialized()
    {
        //LiveReload.Init();
        HotReloader.Current.Run(this);
        InitializeComponent();
        await NavigationService.NavigateAsync("NavigationPage/Login");
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        new PageRegister(containerRegistry);
        new ServiceRegister(containerRegistry);
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
        var huC = ApplicationSettings.HubConnection;
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

当调试应用程序进入 OnInitialized 方法但无法导航到登录页面时(等待 NavigationService.NavigateAsync("NavigationPage/Login");)

这是登录页面视图模型常量。代码。连cons都进不去。

public LoginViewModel(INavigationService navigationService, ILoginMethod _loginMethod,
            IPageDialogService _dialogService, IPagingParams _pagingParams) : base(navigationService, _dialogService)
        {
            //commands
            NavigateCommand = new DelegateCommand<string>(Navigate);
            Login = new DelegateCommand(LoginCommand);

            DialogService = _dialogService;
            LoginMethod = _loginMethod;
            PagingParams = _pagingParams;

            LoginModel = new LoginModel();
            UserLoginModel = new ApplicationUserLoginModel();

            ActivityIsRunning = false;
            ActivityIsVisible = false;
            LoginButtonEnabled = true;

            LoginMethod.HttpConnection.Parameters.BaseUrl = ApplicationSettings.BaseUrl;
            LoginMethod.HttpConnection.Parameters.Port = ApplicationSettings.Port;
            LoginMethod.HttpConnection.Parameters.Connection = ApplicationSettings.Connection;
            LoginMethod.HttpConnection.Parameters.Version = ApplicationSettings.Version;
            LoginMethod.HttpConnection.Parameters.MediaTypeHeader = ApplicationSettings.MediaTypeHeader;

            PageFooterMessage = "KURUMSAL FİRMA UYGULAMASI";
            HeaderImageUrl = ApplicationSettings.LoginScreeenLogo;

            PagingParams.PageSize = ApplicationSettings.PagingPageDefaultSize;
            PagingParams.PageIndex = ApplicationSettings.PagingPageDefaultIndex;
        }

我看不到任何有用的输出屏幕。我花了整整一个下午,但没有锁定

[编辑] 应用程序.xaml

    <?xml version="1.0" encoding="utf-8" ?>
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
             x:Class="Q.Mobile.Corporate.App">
    <Application.Resources>
        <ResourceDictionary>
            <!--<Color x:Key="ColorPrimary">#795548</Color>
            <Color x:Key="ColorPrimaryDark">#4b2c20</Color>
            <Color x:Key="ColorPrimaryLight">#a98274</Color>-->

            <Color x:Key="ColorPrimary">#1976d2</Color>
            <Color x:Key="ColorPrimaryDark">#26629e</Color>
            <Color x:Key="ColorPrimaryLight">#5199e0</Color>
            <Color x:Key="ColorPrimaryBackGround">#f5f5f5</Color>

            <Color x:Key="ColorListItemBackGround">#f0eded</Color>

            <Color x:Key="CyanColorPrimary">#00838f</Color>
            <Color x:Key="CyanColorPrimaryDark">#005662</Color>
            <Color x:Key="CyanColorPrimaryLight">#4fb3bf</Color>

            <Color x:Key="BlueColorPrimary">#0d47a1</Color>
            <Color x:Key="BlueColorPrimaryDark">#002171</Color>
            <Color x:Key="BlueColorPrimaryLight">#5472d3</Color>

            <Color x:Key="GreyBackground">#e0e0e0</Color>
            <Color x:Key="AmberActivityIndicator">#ffab00</Color>
        </ResourceDictionary>
    </Application.Resources>
</prism:PrismApplication>

Login.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Q.Mobile.Corporate.Views.Login.Login"
             NavigationPage.HasNavigationBar="False"
             Visual ="Material">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness">
            <On Platform="iOS" Value="0, 20, 0, 0"/>
        </OnPlatform>
    </ContentPage.Padding>
    <ContentPage.Content>
        <Grid BackgroundColor="{StaticResource Key=ColorPrimaryBackGround}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackLayout Grid.Row="1" Padding="0,40,0,0" >
                <Image Source="mobile_app_login_screen.png" VerticalOptions="Center"/>
            </StackLayout>
            <StackLayout Grid.Row="2" Padding="30,20,30,0">
                <Entry Placeholder="Kullanıcı Adı" Text="{Binding Username}"/>
                <Entry Placeholder="Şifre" Text="{Binding Password}"/>
                <Button Text="GİRİŞ" IsEnabled="{Binding LoginButtonEnabled}"
                        Command="{Binding Login}"                        
                        Margin="0,20,0,0" CornerRadius="5"
                        TextColor="White" FontAttributes="Bold"
                        HeightRequest="50">
                </Button>
                <ActivityIndicator VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"
                                   Color="{StaticResource Key=AmberActivityIndicator}"
                                   IsVisible="{Binding ActivityIsVisible}"
                                   IsRunning="{Binding ActivityIsRunning}" />
            </StackLayout>

            <StackLayout Grid.Row="4" HorizontalOptions="Center" VerticalOptions="End">
                <Label Text="{Binding PageFooterMessage}"/>
            </StackLayout>

        </Grid>
    </ContentPage.Content>
</ContentPage>

【问题讨论】:

  • 我认为您忘记在 prism 中注册您的登录页面,即将containerRegistry.RegisterForNavigation&lt;Login&gt;(); 放入 RegisterTypes 方法中。
  • 在prism中我们需要注册我们想要导航到的应用页面,包括“NavigationPage”:containerRegistry.RegisterForNavigation&lt;NavigationPage&gt;();
  • //导航页面 containerRegistry.RegisterForNavigation(); containerRegistry.RegisterForNavigation(); //登录和主页 containerRegistry.RegisterForNavigation(); containerRegistry.RegisterForNavigation(); containerRegistry.RegisterForNavigation();我的所有页面都已注册。

标签: mvvm xamarin.forms prism


【解决方案1】:

为了诊断您出现空白屏幕的原因,最好捕获 NavigationResult。 NavigationService 按设计尝试捕获任何异常,然后在 NavigationResult 中将其传回给您。这可以帮助您编写更简洁的代码,而无需在任何地方都使用 try catch。

var result = await NavigationService.NavigateAsync("SomePage");
if(!result.Success)
{
    Console.WriteLine(result.Exception);
    Debugger.Break();
}

您可以尝试添加类似这样的内容以更好地了解问题所在。

【讨论】:

    【解决方案2】:

    错误在于您使用StaticResource 语句的方式。当你得到一个带有棱镜的空白页时,这通常是 view 和/或 ResourceDictionary

    Color="{StaticResource Key=AmberActivityIndicator}"
    BackgroundColor="{StaticResource Key=ColorPrimaryBackGround}"
    

    必须是这样的:

    Color="{StaticResource AmberActivityIndicator}"
    BackgroundColor="{StaticResource ColorPrimaryBackGround}"
    

    编辑:根据以下评论,任何遇到此问题的人,请尝试禁用热重载。

    【讨论】:

    • 还是一样。删除了所有静态资源绑定,但仍然有空白的登录页面屏幕。
    • 嗯,很奇怪。您是否已停用 hotreload ?加上删除bin obj文件夹,从设备中删除应用程序并重建?
    • 我照你说的做了 :) 又做了一次。但没有锁。这让我很生气。任何想法 ?我做了很长时间的更改,我不想删除分支:)
    • 您是否可以在示例项目中复制该问题?自项目开始以来,我已经遇到了 5 次这样的错误,并且总是通过寻找 xaml 问题来解决它
    • 谢谢@Umar3x,无论出于何种原因,热重载开始导致我们的应用程序失败。从“工具”>“选项”中禁用,它可以工作
    猜你喜欢
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多