首页 > 解决方案 > 连续按钮点击传递到下一个按钮,如何防止

问题描述

我的项目在 Xamarin.Forms 中,支持的平台是 iOS、android 和 Mac。我在单个 Xamrin.Forms.View 中有两个 Stacklayout,每个 Stacklayout 在同一位置都有一个按钮。这意味着第一个 Stacklayout 中的按钮 1 位置是 x=100 和 y=100,第二个 Stacklayout 中的按钮 2 位置与第一个 Stacklayout 相同。默认情况下,第一个 Stacklayout Visible 和第二个是隐藏的,现在当我点击按钮 1 时,第一个 Stacklayout 隐藏和 API 响应到达后可见的第二个布局。

当用户单击按钮 1 时,我立即将文本更改为“请稍候”,此处按钮文本颜色设置为白色,以便在深色按钮背景中获得更好的视图。让我们考虑 API 需要 10 秒的时间来完成处理,如果用户在按钮 1 上单击多次,然后在 Mac 平台中,Mac 将在某处存储/保留这些单击的事件,当 10 秒后第二个 Stacklayout 可见时,mac 立即触发那些存储/保留按钮 2 的单击事件,即使用户未单击按钮 2。我该如何防止它?

一旦用户单击它,我尝试设置按钮 1 的 IsEnabled=false,它在上述情况下工作正常,但是当我在这种情况下禁用按钮时,按钮文本颜色在 mac 平台中自动从白色变为黑色。我不想改变文本的颜色。如果有人有在禁用模式下更改文本颜色的解决方案,那么它对我有用,或者如果有任何其他解决方案可以防止多次点击,请告诉我。

标签: xamarin.formsxamarin.mac

解决方案


所以,这就是我解决这个问题的方法:

我会在视图中添加一个标签,所以我们有button1labelbutton2:一个在一个之上。

开始时,您的按钮 1可见,而其他两个不可见。

当 button1 被点击时,它是隐藏的:button1.IsVisible = false。立即将标签设置为可见,然后,您可以坐下来等到操作完成(将它放在按钮的事件处理程序中至关重要await!! ),当工作完成后,您将标签可见性设置为 false,并且button2如可见。

您可以按照相同的逻辑再次返回。

最终结果是,当您点击button1时,会出现标签,因此如果用户继续点击,则不会存储/保留这些点击!


完全可以执行此操作的Xamarin.Forms代码如下所示。

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"
             x:Class="MyApp.Page1">
    <ContentPage.Content>
        <Grid HorizontalOptions="CenterAndExpand" 
              VerticalOptions="CenterAndExpand">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <Button x:Name="button1" 
                    Text="Go to the Moon!"
                    Clicked="Button_Clicked"/>

            <Label x:Name="label" 
                   IsVisible="False"/>

            <Button x:Name="button2"
                    Text="Go back home!"
                    IsVisible="False"
                    Clicked="Button_Clicked_1"/>

        </Grid>
    </ContentPage.Content>
</ContentPage>

背后的代码

using System;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace MyApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1 ()
        {
            InitializeComponent ();
        }

        String text1 = "Going to the moon...";

        String text2 = "Going back home...";


        private async void Button_Clicked(object sender, EventArgs e)
        {

            ((Button)sender).IsVisible = false;

            label.Text = text1;
            label.IsVisible = true;

            // Simulates running process!
            await Task.Delay(3000);

            label.IsVisible = false;
            button2.IsVisible = true;

        }

        private async void Button_Clicked_1(object sender, EventArgs e)
        {
            ((Button)sender).IsVisible = false;

            label.Text = text2;
            label.IsVisible = true;

            // Simulates running process!
            await Task.Delay(3000);

            label.IsVisible = false;
            button1.IsVisible = true;

        }
    }
} 

希望这个对你有帮助 :)


推荐阅读