在“Xamarin.Forms”中同步运行“INavigation.PushModalAsync”

发布于 2025-01-11 03:57:04 字数 257 浏览 2 评论 0原文

如何运行 INavigation.PushModalAsync 同步?

同步,我的意思是我想在打开模式页面时阻止 UI 交互(例如,快速点击两次时不允许打开两个页面)。

How to run INavigation.PushModalAsync synchronously?

By synchronously, I mean I want to block UI interactions when opening a modal page (e.g. disallowing opening two pages when tapping fast twice).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2025-01-18 03:57:04

有两种方法供您参考:

  1. Button 中有一个 IsEnabled 属性,您可以在进入方法时先将 IsEnabled 设置为 false。这将使按钮不可点击。这可以防止多次点击。

  2. 您可以锁定您的方法内部。这样,你的逻辑代码就只会被执行一次。防止多次点击导致代码被多次执行。

    有关如何使用Lock的信息,请参阅:锁定语句

There are two ways for your reference:

  1. There is an IsEnabled property in Button, you can first set IsEnabled to false when entering the method. This will make the Button unclickable. This prevents multiple clicks.

  2. You can lock inside your method. In this way, your logic code will only be executed once. Prevent the code from being executed multiple times due to multiple clicks.

    For information on how to use Lock, please refer to: lock statement.

浅黛梨妆こ 2025-01-18 03:57:04

正如 Wen xu Li 在他的回答中提到的,在操作运行时禁用 UI 是最佳实践。下面是实际情况:

MainViewModel.cs

using MvvmHelpers;
using MvvmHelpers.Commands;

public class MainViewModel : ObservableObject {

    private readonly object longWorkLock = new object();

    private bool _isBusy;
    public bool IsBusy{
        get => _isBusy;
        set => SetProperty(ref _isBusy, value);
    }

    public ICommand DoLongWorkCommand { get; }
    public ICommand DoLongWorkAlternativeWithLock { get; }

    public MainViewModel(){
        DoLongWorkCommand = new AsyncCommand(DoLongWorkAsync);
        DoLongWorkAlternativeWithLock = new AsyncCommand(DoLongWorkAlternativeWithLockAsync);
    }

    private async Task DoLongWorkAsync(){
        if(IsBusy){
            return;
        }
        IsBusy = true;

        //simulate long running work
        await Task.Delay(1234);

        IsBusy = false;
    }

    private async Task DoLongWorkAlternativeWithLockAsync(){
        lock(longWorkLock){
            IsBusy = true;

            //simulate long running work
            await Task.Delay(1234);

            IsBusy = false;
        }
    }
}

MyPage.xaml

<Button
    Text="Do long work"
    Command="{Binding DoLongWorkCommand}"
    IsEnabled="{Binding IsBusy, Converter={StaticResource InvertBooleanConverter}}" />

<Button
    Text="Do long work (alternative with lock)"
    Command="{Binding DoLongWorkAlternativeWithLockCommand}"
    IsEnabled="{Binding IsBusy, Converter={StaticResource InvertBooleanConverter}}" />

请注意,我正在使用 MvvmHelpers< /a> 本例中的库。

As Wen xu Li mentioned in his answer, disabling the UI when an operation is running is the best practice. Here's what that might look like in practice:

MainViewModel.cs

using MvvmHelpers;
using MvvmHelpers.Commands;

public class MainViewModel : ObservableObject {

    private readonly object longWorkLock = new object();

    private bool _isBusy;
    public bool IsBusy{
        get => _isBusy;
        set => SetProperty(ref _isBusy, value);
    }

    public ICommand DoLongWorkCommand { get; }
    public ICommand DoLongWorkAlternativeWithLock { get; }

    public MainViewModel(){
        DoLongWorkCommand = new AsyncCommand(DoLongWorkAsync);
        DoLongWorkAlternativeWithLock = new AsyncCommand(DoLongWorkAlternativeWithLockAsync);
    }

    private async Task DoLongWorkAsync(){
        if(IsBusy){
            return;
        }
        IsBusy = true;

        //simulate long running work
        await Task.Delay(1234);

        IsBusy = false;
    }

    private async Task DoLongWorkAlternativeWithLockAsync(){
        lock(longWorkLock){
            IsBusy = true;

            //simulate long running work
            await Task.Delay(1234);

            IsBusy = false;
        }
    }
}

MyPage.xaml

<Button
    Text="Do long work"
    Command="{Binding DoLongWorkCommand}"
    IsEnabled="{Binding IsBusy, Converter={StaticResource InvertBooleanConverter}}" />

<Button
    Text="Do long work (alternative with lock)"
    Command="{Binding DoLongWorkAlternativeWithLockCommand}"
    IsEnabled="{Binding IsBusy, Converter={StaticResource InvertBooleanConverter}}" />

Note that I'm using the MvvmHelpers library in this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文