启动画面不会移至下一页

时间:2019-11-14 13:28:50

标签: c# xaml xamarin

一段时间以来,我一直在阅读很多有关Xamarin的内容,以及如何制作启动画面。我对这样的东西还很陌生,因此我想在这里问一下,为什么启动画面未重定向到模拟器的登录页面中?

我正在使用Visual Studio 2019,我添加了一个新文件夹drawable-hdpi,然后将图像添加到其中以在xaml中使用。怎么办?它不会重定向!

这是我的代码的样子

启动画面Xaml(MainPage.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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             x:Class="VisitorMan.MainPage"
             BackgroundColor="#fff">

    <StackLayout HorizontalOptions="Center" VerticalOptions="Center" Padding="10">
        <Image Source="splash_logo.png" x:Name="imgLogo" HeightRequest="150" WidthRequest="150"/>
    </StackLayout>

</ContentPage>

对应的MainPage.xaml.cs文件如下所示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace VisitorMan
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Animate();
        }

        public async Task Animate() 
        {
            imgLogo.Opacity = 0;
            await imgLogo.FadeTo(1, 4000);
            Application.Current.MainPage = new LoginPage();
        }
    }
}

显示用户名和密码的登录页面(LoginPage.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"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             x:Class="VisitorMan.LoginPage"
             BackgroundColor="#fff">

    <ContentPage.Content>
        <StackLayout Spacing="20" Padding="50" VerticalOptions="Center">
            <Image Source="splash_logo.png" x:Name="imgLogo" HeightRequest="150" WidthRequest="150"/>
            <Entry Placeholder="Username"></Entry>
            <Entry Placeholder="Password" IsPassword="True"></Entry>
            <Button Text="Log In" TextColor="White" BackgroundColor="#ff77D065"></Button>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

初始屏幕只是静止不动,不会重定向到登录页面。我会缺少什么吗?

2 个答案:

答案 0 :(得分:0)

我没有检查,但是您可以尝试

public async Task Animate() 
{
    imgLogo.Opacity = 0;
    await imgLogo.FadeTo(1, 4000);
    Device.BeginInvokeOnMainThread(() =>
    {
       Application.Current.MainPage = new LoginPage();
    });
}

答案 1 :(得分:0)

我尝试了共享示例,但问题是您需要将项目的nuget程序包(特别是Xamarin.Forms的版本)更新到最新版本

右键单击项目解决方案,选择管理解决方案的Nuget程序包...

enter image description here

更新后:

enter image description here

效果:

enter image description here