首页 文章

Xamarin表单:使用System.Timers可以看到Stacklayout

提问于
浏览
0

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

1 回答

  • 0

    我想从不同的线程刷新UI . 这是个问题 .

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

相关问题