【问题标题】:Using a timer to trigger an event in windows metro with C++使用计时器在带有 C++ 的 Windows Metro 中触发事件
【发布时间】:2012-08-22 01:35:53
【问题描述】:

所以我正在开发一个在倒计时完成后拍照的应用程序。我在 Windows 7 中使用过 win32 计时器,但我不知道如何在 Windows Metro 中应用它。我需要一些帮助或 c++ 中的一些示例代码,这些代码与如何在设定的时间到期后触发事件有关。 提前感谢您的帮助

【问题讨论】:

    标签: c++ windows timer microsoft-metro


    【解决方案1】:

    在 C++ 中,声明 DispatcherTimer,并在其上注册一个事件处理程序。

    DispatcherTimer 类 - http://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.dispatchertimer

    http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

    示例代码:

    using namespace Windows::UI::Xaml;
    using namespace Windows::Foundation;
    
    
    void Application1::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        DispatcherTimer^ timer = ref new DispatcherTimer;
        timer->Tick += ref new Windows::UI::Xaml::EventHandler(this, &Application1::MainPage::DispatcherTimer_Tick);
        TimeSpan t;
        t.Duration=1000;
        timer->Interval = t;
        timer->Start();
    }
    
    void Application1::MainPage::DispatcherTimer_Tick(Platform::Object^ sender, Platform::Object^ e)
    {
       // Put TO DO stuff here...
    }
    

    【讨论】:

    • 谢谢,这正是我所需要的。
    • 我在 'timer->Tick += ref new Windows::UI::Xaml::EventHandler(this, &Application1::MainPage::DispatcherTimer_Tick);编译器无法将“windows::ui::xaml::eventhandler”识别为类型
    • 事件类型为Windows::Foundation::EventHandler<Platform::Object^>^
    • 现在我收到了usage requires Windows::UI::Xaml::DispatcherTimer::Tick' to be a data member
    • * 已将 += 更改为 =。感谢大家的帮助!!
    【解决方案2】:

    Windows::UI::Xaml::DispatcherTimer

    该文档包含一个使用示例。该示例使用 C# 编写,但应该可以直接将其转换为 C++/CX。

    【讨论】:

    • 不幸的是,将其翻译成 C# 很困难,因为 DateTimeOffset 显然不存在于 c++ 版本的库中。
    • DateTimeOffset 是 C++ 中的 Windows::Foundation::DateTime
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多