2012-08-22 46 views
0

所以我開發的應用程序在倒計時完成後拍攝一張照片。我已經在Windows 7中使用win32定時器,但我不知道如何在Windows metro中應用它。我需要一些幫助或C++中的一些示例代碼,涉及如何在設定的時間過期後觸發事件。 在此先感謝您的幫助使用計時器在Windows metro中用C++觸發事件

回答

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... 
} 
+0

謝謝你,這正是我需要的。 – HollywoodCurls

+0

我遇到了問題'timer-> Tick + = ref new Windows :: UI :: Xaml :: EventHandler(this,&Application1 :: MainPage :: DispatcherTimer_Tick);編譯器不能識別「windows :: ui :: xaml :: eventhandler」作爲類型 – HollywoodCurls

+0

事件的類型是Windows :: Foundation :: EventHandler ^'。 –

0

Windows::UI::Xaml::DispatcherTimer

文檔包括其使用的一個例子。這個例子是用C#編寫的,但它應該直接轉換成C++/CX。

+0

不幸的是翻譯成C#這是困難的,因爲很明顯的DateTimeOffset庫中的C++版本不存在。 – HollywoodCurls

+0

C++有'std :: chrono'。 –

+0

DateTimeOffset是Windows :: Foundation :: DateTime在C++ –

相關問題