我有一个C#UWP应用程序,它使用用Visual C ++编写的自定义UWP运行时库。
在我的一个Visual C ++类中,我将字符串向量转换为平台字符串集合,这对C#UWP应用程序是可以理解的。
我的问题是Windows Runtime Bindable异常仅在应用程序的发布模式下引发。一切都在调试模式下完美运行。
Visual C ++:
// internalModel.cpp - the model I get my vector of strings from
std::vector<std::string> InternalModel::getAllPackages() {
std::vector<std::string> names;
for (auto i = 0U; i < packageOptions.size(); i++) {
// these are strings
names.push_back(packageOptions[i]["name"]);
}
return names;
}
// .h - the method signature called by my C# program
Windows::Foundation::Collections::IVector<Platform::String^>^ GetAllPackages();
// .cpp - the method body called by my C# program
Windows::Foundation::Collections::IVector<Platform::String^>^ VisualModel::GetAllPackages() {
auto s_packages = internalModel->getAllPackages();
auto p_packages = ref new Platform::Collections::Vector<Platform::String^>();
for each (auto s_package in s_packages)
{
p_packages->Append(WinUtil::toPlat(s_package));
}
return p_packages;
}
// WinUtil.h - the util class that converts standard string vector to platform collection of strings
template <class A, class B>
static Platform::Collections::Vector<B^>^ toPlat(std::vector<A*>* s_vector) {
auto p_vector = ref new Platform::Collections::Vector<B^>();
for each (A* s_elem in s_vector) {
p_vector->Append(ref new B(s_elem));
}
return p_vector;
}
C#:
// where I call the Visual C++ from
var packages = VisualLib.GetAllPackages();
foreach (var package in packages)
{
// package should be string here
}
我无法找出导致异常的原因,因为我的调试器无法进入UWP运行时库。
也许我将矢量转换为平台集合的方式不对,但话说再次,它在调试模式下工作正常。我的程序的其他部分也使用相同的实用程序功能:
static Platform::Collections::Vector<B^>^ toPlat(std::vector<A*>* s_vector)
引发的异常:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Exception_WasThrown, Microsoft.CSharp.RuntimeBinder.RuntimeBinderException. For more information, visit http://go.microsoft.com/fwlink/?LinkId=623485'