简体   繁体   中英

Xamarin Forms: Stacklayout Visible with System.Timers

I am on the most current version of Xamarin Forms. I have a Content Page. The Content Page has a grid that has a StackLayout and ScrollView. StackLayout Visible is false at the start point. When I click my Login Button, which has a Login method (see below) I set the StackLayout visible true. I use System.Timers too which start when login button clicked. If this timer reach 10 sec and the login isn't succesful the timer elapsed method activate. This method you can see below. At this point this work great, but I want to Login again and the StackLayout content doesn't show up. Can Anybody help me?

LoginPage.xml code:

<?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="Spirocco.LoginPage"
         xmlns:renderer="clr-namespace:Spirocco.Droid.Renderers">
<Grid>
    <StackLayout x:Name="stackView" IsVisible="False" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="300" HeightRequest="50" BackgroundColor="LightGray" Orientation="Horizontal">
        <ActivityIndicator IsRunning="True" Color="Black" HorizontalOptions="Center" Margin="20" HeightRequest="30" WidthRequest="30"/>
        <Label Text="Bejelentkezés..." TextColor="Black" VerticalOptions="Center" FontSize="16"/>
    </StackLayout>
    <ScrollView Orientation="Both" x:Name="scrollView">
        <ScrollView.Content>
            <StackLayout BackgroundColor="#302138">
                <Image Source="login_logo" Margin="0,0,0,0"></Image>
                <StackLayout BackgroundColor="White" Margin="20,0,20,30">
                    <Label Text="ÜDVÖZÖLJÜK!" FontSize="30" FontFamily="Comic Sans MS" Margin="0,15,0,0" TextColor="#302138" HorizontalTextAlignment="Center"></Label>
                    <renderer:BaseEntry x:Name="entryEmail" Text="{Binding Email}" Placeholder="E-mail" Margin="40,0,40,0" Keyboard="Email" ReturnType="Next"/>
                    <renderer:BaseEntry x:Name="entryPassword" Text="{Binding Password}" Placeholder="Jelszó" IsPassword="True" Margin="40,0,40,0" ReturnType="Send"/>
                    <Button Text="BEJELENTKEZÉS" Clicked="Login" TextColor="White" BackgroundColor="#302138" Margin="40,10,40,0"/>
                    <Button Text="REGISZTRÁCIÓ" Clicked="Register" TextColor="White" BackgroundColor="#302138" Margin="40,0,40,25"/>
                </StackLayout>
                <Label BackgroundColor="#302138" HeightRequest="160"/>
            </StackLayout>
        </ScrollView.Content>
    </ScrollView>
</Grid>

My login method:

private async void Login(object sender, EventArgs e)
    {
        if (entryEmail.Text != null && entryPassword.Text != null) 
        {
            try
            {
                stackView.IsVisible = true;
                scrollView.Opacity = 0.5;
                timer = new Timer(10000);
                timer.Start();
                timer.Elapsed += SetContentViewVisible;
            }
            catch (Exception)
            {
                stackView.IsVisible = false;
                scrollView.Opacity = 1;
                await DisplayAlert("Hiba történt", "Sikertelen bejelentkezés", "Vissza");
            }
        }
        else
        {
            await DisplayAlert("Bejelentkezés", "Sikertelen bejelentkezés, kérem a sikeres bejelentkezéshez töltse ki az e-mail cím és jelszó mezőt!", "Vissza");
        }
    }

SetContentViewVisible method:

private void SetContentViewVisible(object sender, ElapsedEventArgs e)
    {
        timer.Dispose();
        scrollView.Opacity = 1;
        stackView.IsVisible = false;
        timer.Stop();
    }

I want to refresh UI from different thread. This was problem.

private void SetContentViewVisible(object sender, ElapsedEventArgs e)
    {
        Device.BeginInvokeOnMainThread(
              () =>
              {
                  timer.Dispose();
                  scrollView.Opacity = 1;
                  stackView.IsVisible = false;
                  timer.Stop();
                  DisplayAlert(SaySomething);
              });
    }

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