實際上,我正在爲通用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設置爲默認值 的函數。現在它似乎可以在單獨的課程中使用靜態項目 。但是,如果有人知道的 任何其他方式傳遞數據,隨意張貼在這裏
C++/CX是C++的限制超集。就像傳遞其他C++對象一樣,傳遞你的對象。如果你想變得很花哨,把它們包裝在'ref class'中,並免費獲得資源管理。但沒有代碼,我們無法幫助。 – IInspectable