2016-10-04 48 views
0

實際上,我正在爲通用Windows平臺編寫一個C++/CX應用程序。我試圖從一個頁面元素傳遞一個不同於Windows運行時(如cv :: detail :: WaveCorrectKind和std :: string)一部分的變量的類到另一個。直接將它作爲對象傳遞給Navigate方法是不可能的。將它寫入外部類並使其可訪問其他頁面元素也是不可能的,因爲每當我嘗試在公共部分中使用某些非WinRT變量或類來從其他頁面元素訪問時,它會給我一些錯誤。這是我想要的一個示例代碼。UWP C++ App頁面之間傳遞OpenCV相關類

第一頁的標頭:

//Page1.h 
// Deklaration der Page1-Klasse 

#pragma once 

#include "Page1.g.h" 
#include "Bibliothek.h" 

namespace eigeneApp 
{ 
    [Windows::Foundation::Metadata::WebHostHidden] 
    public ref class Page1 sealed 
    { 
    public: 
     Page1(); 

    protected: 
     //Navigations Events 
     virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override; 
     virtual void OnNavigatingFrom(Windows::UI::Xaml::Navigation::NavigatingCancelEventArgs^ e) override;  

    private: 
     //Buttons Begin 
     void toPage2Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); 
     //some other Buttonevents for capturing Photos and back to mainpage 
     //Buttons Ende 
     //A OpenCV related Element out of the Bibliothek.h 
     bibitem bibitemElement; 

     //Tasks that handle some stuff out of Bibliothek.h (photo processing) and capturing the photos asynchronous 
     // String Helper for Converting Platform::String to std::string and back  
    }; 
} 

第1頁的CPP-文件:

// 
// Page1.xaml.cpp 
// Implementing Page1-Klasse 
// 

#include "pch.h" 

#include "Page1.xaml.h" 
#include "Page2.xaml.h" 
//other includes for asynch programming, Strings and so on... 
#include "Bibliothek.h" 
#include "opencv2\opencv.hpp" 
#include "opencv2\imgproc.hpp" 

#include "App.xaml.h" 

using namespace eigeneApp;//and other using namespaces 

Page1::Page1() 
{ 
    InitializeComponent(); 
} 

//Buttons Start 
void eigeneApp::Page1::toPage2Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
{ 
    this->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this]() { 
     this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(Page2::typeid),bibitemElement); 
    })); 
} 
// other Button-functions are also here, but not relevant 
//Tasks and the NavigationEvents are handled here and the String Helpers 

據我知道Bibliothek.h含有分類(bibitem)與OpenCV的相關功能。不可能發佈圖書館規範。但我也嘗試在頁面之間傳遞一個cv :: Mat,但它不起作用。

我也嘗試編寫一個包含靜態bibitem的類,並嘗試在Page1和Page2中使用它們以從Page1到Page2中獲取內容。這裏是代碼:

#pragma once 

#include "Bibliothek.h" 

//NavigationData.h 
class NavigationData 
    { 
    public: 
     static bibitem passingbibitem; 
    }; 

和:

//NavigationData.cpp 
#include "NavigationData.h" 
#include "pch.h" 

bibitem NavigationData::passingbibitem; 

後來我加了一個NavigationData元到每個頁面的私人部分,並試圖使用它們。可以在Page1上使用傳遞函數 - 函數,但是當我到達Page2時,傳遞函數的數據是空的。所以我的靜態保持不變thougt是某種錯誤...

有沒有任何方法在頁面之間傳遞非WinRT類和變量?有人可以給我一個如何做到這一點的例子嗎?

我忘記了在我的OnNavigatingFrom-Handler中將bibitem-data設置爲默認值 的函數。現在它似乎可以在單獨的課程中使用靜態項目 。但是,如果有人知道的 任何其他方式傳遞數據,隨意張貼在這裏

被傳遞到導航功能需要被運行時類型
+1

C++/CX是C++的限制超集。就像傳遞其他C++對象一樣,傳遞你的對象。如果你想變得很花哨,把它們包裝在'ref class'中,並免費獲得資源管理。但沒有代碼,我們無法幫助。 – IInspectable

回答

0

類型(派生從平臺::對象^),這意味着他們應該是'ref class'類型。你的想法是創建一個包裝類型是正確的,但問題是公共字段/屬性需要在WinRT中表達,所以它們不能是C++類型。

答案是你可以做一個包裝類,你只需要將它們標記爲內部。我不會讓靜態的,因爲那將是整個包裝類型的所有實例相同,只讓它作爲一個實例變量:

//NavigationData.h 
ref class NavigationData 
{ 
    internal: 
     bibitem passingbibitem; 
}; 
+0

謝謝你的想法,但是如果我嘗試用「internal:」 - 說明符編寫頭文件,它會給我一些錯誤。看起來好像不可能在正常的C++類中使用「internal:」 - 說明符。使用「ref class」可以使用「internal:」 - 說明符。但是,當我嘗試,Visual Studio引發3編譯器錯誤:C2664,C3073和C3076。一個是關於在我的類和對象之間轉換是不可能的,其他的是關於複製構造函數,而關於ref類型的實例的東西不能在系統相關類型中進行處理... – Henji

+0

對不起 - NavigationData填充類應該是'ref'類。你能提供一個更完整的例子來顯示編譯器錯誤和完整的編譯器輸出嗎?我無法根據錯誤代碼推斷出您的問題。 –