【问题标题】:LongPress is triggers while using PinchGestureRecognizer in xamarin forms在 xamarin 表单中使用 PinchGestureRecognizer 时,LongPress 是触发器
【发布时间】:2022-11-01 14:20:50
【问题描述】:

我正在为 LongPress 使用 xamarin 社区工具包,为 PinchGestureRecognizer 使用 GestureRecognizers。 PinchGestureRecognizer 完成后,xct:TouchEffect.LongPressCommand 也会被触发。有没有办法一次触发这些事件?

这是我的代码示例

 <StackLayout
        xct:TouchEffect.LongPressCommand="{Binding LongPressCommand}"
        xct:TouchEffect.LongPressCommandParameter="LongPress"
        BackgroundColor="Red">
        <Frame
            Padding="24"
            BackgroundColor="#2196F3"
            CornerRadius="0">
            <Label
                FontSize="36"
                HorizontalTextAlignment="Center"
                Text="Welcome to Xamarin.Forms!"
                TextColor="White" />
        </Frame>
        <Label
            Padding="30,10,30,10"
            FontSize="Title"
            Text="Start developing now" />
        <Label
            Padding="30,0,30,0"
            FontSize="16"
            Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" />
        <Label Padding="30,24,30,0" FontSize="16">
            <Label.FormattedText>
                <FormattedString>
                    <FormattedString.Spans>
                        <Span Text="Learn more at " />
                        <Span FontAttributes="Bold" Text="https://aka.ms/xamarin-quickstart" />
                    </FormattedString.Spans>
                </FormattedString>
            </Label.FormattedText>
        </Label>
        <StackLayout.GestureRecognizers>
            <PinchGestureRecognizer PinchUpdated="OnPinchUpdated" />
        </StackLayout.GestureRecognizers>
    </StackLayout>

cs 文件

        public ICommand LongPressCommand { get; set; }
        public MainPage()
        {
           
            InitializeComponent();           
            LongPressCommand = new Command<string>(LongPress);
            BindingContext = this;
        }
        public void LongPress(string flag)
        {
        }
        private void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
        {
            
        }
    }

【问题讨论】:

  • 您可能需要在OnPinchUpdated 期间保存当前时间。然后在LongPress 中,检查距离最近的OnPinchUpdated 已经过去了多少时间。如果经过的时间“太短”(可能是 250 毫秒?尝试不同的值),则忽略 LongPress

标签: xamarin xamarin.forms xamarin.android xamarin-community-toolkit


【解决方案1】:

您可以设置LongPress 命令的duration 来触发该命令。默认为500ms,您可以通过LongPressDuration 属性进行设置。在下面的示例中,我将其设置为3s这意味着LongPress 将在之后触发3s.您可以设置适当的持续时间以避免与PinchGestureRecognizer 发生冲突。

xct:TouchEffect.LongPressDuration="3000"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2020-04-05
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多