简体   繁体   English

Xamarin形式:Stacklayout与System.Timers可见

[英]Xamarin Forms: Stacklayout Visible with System.Timers

I am on the most current version of Xamarin Forms. 我使用的是Xamarin Forms的最新版本。 I have a Content Page. 我有一个内容页面。 The Content Page has a grid that has a StackLayout and ScrollView. 内容页面具有一个包含StackLayout和ScrollView的网格。 StackLayout Visible is false at the start point. 在开始时,StackLayout Visible为false。 When I click my Login Button, which has a Login method (see below) I set the StackLayout visible true. 当我单击具有Login方法的Login按钮时(请参见下文),我将StackLayout设置为true。 I use System.Timers too which start when login button clicked. 我也使用System.Timers,它在单击登录按钮时启动。 If this timer reach 10 sec and the login isn't succesful the timer elapsed method activate. 如果此计时器达到10秒,并且登录失败,则激活计时器已用方法。 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. 在这一点上,这个工作很好,但是我想再次登录,并且StackLayout内容没有显示。 Can Anybody help me? 有谁能够帮助我?

LoginPage.xml code: LoginPage.xml代码:

<?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: SetContentViewVisible方法:

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. 我想从其他线程刷新UI。 This was problem. 这是问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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