【问题标题】:Long press for non touch devices Windows 8长按非触摸设备 Windows 8
【发布时间】:2013-10-30 13:44:09
【问题描述】:

我一直在为 Win 8 开发一个应用程序。该应用程序使用长按。在触摸设备上一切正常。但现在我意识到了别的事情,长按在非触摸设备上不起作用。当我在本地机器上运行应用程序时,一切都呈现了,但长触摸消失了!

非触控电脑没有长按吗?

我正在检查触摸平板电脑和非触摸电脑上的应用程序,相同的应用程序示例日历的行为不同。两种设备在点击/触摸日期时打开的页面不同?他们如何做到这一点?维护不同的代码??

我需要长按我的应用程序。我如何让它在非触摸电脑上运行?鼠标右键弹出导航栏!!我需要在保持事件和指针事件中编写相同的代码??

现在我该怎么办! (恐慌!)

【问题讨论】:

    标签: windows-8 long-press


    【解决方案1】:

    您可以使用计时器以及 PointerPressed 和 PointerReleased 事件来模拟此过程。

    你可以像这样创建简单的计时器

    private bool _isTimerActive = false;
    private int _timeInMS = 0;
    private const int Delay = 1500; // wait for 1.5 sec
    
    private async void StartTimer()
    {
        _isTimerActive = true;
    
        while (_isTimerActive)
        {
            await Task.Delay(10);
            _timeInMS += 10;
            if (_timeInMS > Delay)
            {
                _holdingAction(null, null); // Do the suitable action after holding for 1.5 secounds
                StopTimer();
            }
        }
    }
    
    private void StopTimer()
    {
        _isTimerActive = false;
        _timeInMS = 0;
    }
    

    那么您必须为 .cs 中的每个按钮添加事件处理程序,因为按钮控件不允许触发 PointerPressed 和 PointerReleased 事件。

    这样:

    MyButton.AddHandler(PointerPressedEvent, new PointerEventHandler(Button_PointerPressed), true);
    MyButton.AddHandler(PointerReleasedEvent, new PointerEventHandler(Button_PointerReleased), true);
    

    最后,您可以使用委托来定义每个按钮的操作

    private delegate void HoldingAction(object sender, HoldingRoutedEventArgs e);
    private HoldingAction _holdingAction;
    
    // your Long-press event 
    private void Button_Holding(object sender, HoldingRoutedEventArgs e)
    {
        //Your awesome code here.
    }
    
    private void Button_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        StartTimer();
        _holdingAction = Button_Holding;
    }
    
    private void Button_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        StopTimer();
    }
    

    【讨论】:

    • 感谢您的回答。但是,如果我有一个Button_Click 和一个Button,我还需要连接一个长按它呢?我尝试了您的回答,但似乎根本没有调用 PointerReleased 处理程序。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2023-03-11
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多