简体   繁体   中英

Splashscreen will not move to next page

I have been reading much about Xamarin for sometime and how to make a splash screen. I am quite new to something like this and hence i wanted to ask on here, Why is the splash screen not redirecting to the login page in the Emulator'

i am using Visual Studio 2019, I added a new folder drawable-hdpi and then added the images to it to be used inside the xaml. Now what? it does not redirect!

Here is what my code looks like

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

The corresponsing MainPage.xaml.cs File Looks like this

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();
        }
    }
}

The Login Page which shows the username and password (LoginPage.xaml) Looks like this

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

The splashscreen is just standstill and does not redirect to the Login Page. Could there be something i am missing?

I didn't check but you can try this

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

I have tried shared sample, the problem is that you need to update nuget packages ( especially version of Xamarin.Forms ) of project to the latest version .

Right click Solution of project, choose Manage Nuget Packages For Solution...

在此处输入图像描述

After Updated:

在此处输入图像描述

The effect:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM