【问题标题】:Xamarin Forms, quickly pressing the button opens 2x the same windowXamarin Forms,快速按下按钮打开 2x 相同的窗口
【发布时间】:2017-11-22 10:59:43
【问题描述】:

当我单击重定向到新窗口的图标时,它会打开相同的视图两次。而且我不知道如何处理它只打开一个视图。

示例 xaml 按钮

<local:AnimatedImage Grid.Column="2" Source="googlemap_view_search.png"
                    HorizontalOptions="Center" VerticalOptions="Center" Margin="16,0">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding SearchCommand}" />
                </Image.GestureRecognizers>
            </local:AnimatedImage>

自定义动画图像类

 public class AnimatedImage : Image
{
    private const int ANIMATED_TIME = 100;
    private const double INITIAL_VALUE = 1;
    private const double MAGNIFICATION_VALUE = 1.3;

    public static readonly BindableProperty CommandProperty = BindableProperty.Create<AnimatedImage, ICommand>(p => p.Command, null);
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create<AnimatedImage, object>(p => p.CommandParameter, null);
    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    private ICommand TransitionCommand
    {
        get
        {
            return new Command(async () =>
            {
                this.AnchorX = 0.48;
                this.AnchorY = 0.48;
                await this.ScaleTo(MAGNIFICATION_VALUE, 50, Easing.Linear);
                await Task.Delay(ANIMATED_TIME);
                await this.ScaleTo(INITIAL_VALUE, 50, Easing.Linear);
                if (Command != null)
                {
                    Command.Execute(CommandParameter);
                }
            });
        }
    }

    public AnimatedImage()
    {
        Initialize();
    }

    public void Initialize()
    {
        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = TransitionCommand
        });
    }
}

}

在 ViewModel 中 public DelegateCommand SearchCommand { get;私人套装; } 和

    await NavigateToPage(new SearchPage());

【问题讨论】:

  • 每次调用getter都返回new Command

标签: c# xamarin.forms


【解决方案1】:

问题在于,当您在执行搜索命令之前await 动画时,没有什么可以阻止用户再次启动该命令。您可以在输入命令时添加一个设置为true 的布尔变量,如果该命令已在执行,则忽略第二个用户点击。

private ICommand TransitionCommand
{
    get
    {
        bool isExecuting = false;
        return new Command(async () =>
        {
            if(isExecuting) return;
            isExecuting = true
            try
            {
                this.AnchorX = 0.48;
                this.AnchorY = 0.48;
                await this.ScaleTo(MAGNIFICATION_VALUE, 50, Easing.Linear);
                await Task.Delay(ANIMATED_TIME);
                await this.ScaleTo(INITIAL_VALUE, 50, Easing.Linear);
                if (Command != null)
                {
                    Command.Execute(CommandParameter);
                }
             }
             finally 
             {
                  isExecuting = false;
             }
        });
    }
}

【讨论】:

    【解决方案2】:

    Xamarin Command 具有 CanExecute 方法,可用于根据您的业务逻辑阻止执行命令。

    public ICommand SearchCommand {get; private set; }
    
    bool _canSearch = true;
    public bool CanSearch 
    { 
        get { return _canSearch; } 
        private set { 
            _canSearch = value;
            ((Command)SearchCommand).ChangeCanExecute(); 
        }
    }
    
    public SearchPageViewModel()
    {
        SearchCommand = new Command(() => DoSearchStuff(), () => CanSearch)
    }
    
    void DoSearchStuff()
    {
        CanSearch = false;
        // long searching stuff
        CanSearch = true;
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 2017-06-14
      • 1970-01-01
      相关资源
      最近更新 更多