繁体   English   中英

Xamarin形式:Stacklayout与System.Timers可见

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

我使用的是Xamarin Forms的最新版本。 我有一个内容页面。 内容页面具有一个包含StackLayout和ScrollView的网格。 在开始时,StackLayout Visible为false。 当我单击具有Login方法的Login按钮时(请参见下文),我将StackLayout设置为true。 我也使用System.Timers,它在单击登录按钮时启动。 如果此计时器达到10秒,并且登录失败,则激活计时器已用方法。 您可以在下面看到此方法。 在这一点上,这个工作很好,但是我想再次登录,并且StackLayout内容没有显示。 有谁能够帮助我?

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>

我的登录方法:

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

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

我想从其他线程刷新UI。 这是问题。

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