【问题标题】:Continuous Button click passed to next button, how to prevent连续按钮点击传递到下一个按钮,如何防止
【发布时间】:2019-09-28 10:13:45
【问题描述】:

我的项目在 Xamarin.Forms 中,支持的平台是 iOS、android 和 Mac。我在单个 Xamrin.Forms.View 中有两个 Stacklayout,每个 Stacklayout 在同一位置都有一个按钮。这意味着按钮 1 的位置在第一个 Stacklayout 中为 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 平台中自动从白色变为黑色。我不想改变文本的颜色。如果有人有在禁用模式下更改文本颜色的解决方案,那么它对我有用,或者如果有任何其他解决方案可以防止多次点击,请告诉我。

【问题讨论】:

  • 嗨!如果最后您终于找到了解决问题的方法,请分享,或者如果下面发布的答案有助于实现您的目标,请将其标记为答案。通过这样做,您可以使这篇文章对其他有类似问题的人有用。感谢您为 SO 社区做出贡献!

标签: xamarin.forms xamarin.mac


【解决方案1】:

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

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

一开始,button1 可见,而其他两个不可见。

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

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

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


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;

        }
    }
} 

我希望你觉得这很有用:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 2020-09-09
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多