C++ 在Visual Studio 2013 Gid应用程序中,如何使用组合框更改json文件?
我正在努力解决这个问题,因为我的编程知识非常有限 我在VisualStudio中使用C++,我使用网格应用程序模板创建一个应用程序,我希望能够使用多个JSON文件从中提取必要的数据。在SampleDataSource.cpp中,我使用ComboBoxSelectionChanged函数构建了一个if语句,表示如果选择了特定索引,请将json文件更改为必要的json 这样,我就可以允许用户查看他们使用组合框选择的任何数据集。到目前为止,所有操作都正常执行,但当程序运行时,无论选择什么,数据都是相同的。我不确定它是否需要重新绘制或更新,但经过几天的研究,我不知所措 我提前感谢你的帮助 编辑:我正在尝试发布代码 很抱歉,我无法正确显示代码的第一部分,但我一直在编辑的最重要的部分大约是一半 ` SampleDataItem::SampleDataItem(字符串^uniqueId、字符串^title、字符串^subtitle、字符串^imagePath、字符串^description、, 字符串^content): _唯一的, _头衔(头衔), _副标题(副标题),, _描述(描述), _imagePath(imagePath), _内容(content) { }C++ 在Visual Studio 2013 Gid应用程序中,如何使用组合框更改json文件?,c++,json,visual-studio-2013,C++,Json,Visual Studio 2013,我正在努力解决这个问题,因为我的编程知识非常有限 我在VisualStudio中使用C++,我使用网格应用程序模板创建一个应用程序,我希望能够使用多个JSON文件从中提取必要的数据。在SampleDataSource.cpp中,我使用ComboBoxSelectionChanged函数构建了一个if语句,表示如果选择了特定索引,请将json文件更改为必要的json 这样,我就可以允许用户查看他们使用组合框选择的任何数据集。到目前为止,所有操作都正常执行,但当程序运行时,无论选择什么,数据都是相同
String^SampleDataItem::UniqueId::get()
{
返回(uniqueId);;
}
字符串^SampleDataItem::Title::get()
{
返回标题;
}
字符串^SampleDataItem::Subtitle::get()
{
返回字幕;
}
字符串^SampleDataItem::Description::get()
{
返回描述;
}
字符串^SampleDataItem::Content::get()
{
返回内容;
}
字符串^SampleDataItem::ImagePath::get()
{
返回图像路径;
}
Windows::UI::Xaml::Data::ICustomProperty^SampleDataItem::GetCustomProperty(平台::字符串^name)
{
返回空ptr;
}
Windows::UI::Xaml::Data::ICustomProperty^SampleDataItem::GetIndexedProperty(平台::字符串^name,Windows::UI::Xaml::Interop::TypeName类型)
{
返回空ptr;
}
平台::字符串^SampleDataItem::GetStringRepresentation()
{
返回标题;
}
Windows::UI::Xaml::Interop::TypeName SampleDataItem::Type::get()
{
返回此->GetType();
}
//
//样本数据组
//
SampleDataGroup::SampleDataGroup(字符串^uniqueId、字符串^title、字符串^subtitle、字符串^imagePath、字符串^description):
_唯一的,
_头衔(头衔),
_副标题(副标题),,
_描述(描述),
_imagePath(imagePath),
_项目(参考新向量())
{
}
字符串^SampleDataGroup::UniqueId::get()
{
返回(uniqueId);;
}
字符串^SampleDataGroup::Title::get()
{
返回标题;
}
字符串^SampleDataGroup::Subtitle::get()
{
返回字幕;
}
字符串^SampleDataGroup::Description::get()
{
返回描述;
}
IObservableVector^SampleDataGroup::Items::get()
{
退货(物品);;
}
字符串^SampleDataGroup::ImagePath::get()
{
返回图像路径;
}
Windows::UI::Xaml::Data::ICustomProperty^SampleDataGroup::GetCustomProperty(平台::字符串^name)
{
返回空ptr;
}
Windows::UI::Xaml::Data::ICustomProperty^SampleDataGroup::GetIndexedProperty(平台::字符串^name,Windows::UI::Xaml::Interop::TypeName类型)
{
返回空ptr;
}
平台::字符串^SampleDataGroup::GetStringRepresentation()
{
返回标题;
}
Windows::UI::Xaml::Interop::TypeName SampleDataGroup::Type::get()
{
返回此->GetType();
}
//
//样本数据源
//
字符串^address=“ms-appx:///DataModel/SampleData1900.json";
void Flag_Project::GroupedItemsPage::ComboBox_SelectionChanged(平台::对象^sender,Windows::UI::Xaml::控件::SelectionChangedEventArgs ^e)
{
int selectedIndex=combobox1->selectedIndex;
对象^selectedItem=combobox1->selectedItem;
如果(已选择索引=1)
{
address=“ms-appx:///DataModel/SampleData.json";
这个->框架->导航(TypeName(GroupedItemsPage::typeid));
}
}
SampleDataSource::SampleDataSource()
{
_组=参考新向量();
Uri^Uri=ref新Uri(地址);
创建任务(StorageFile::GetFileFromApplicationUrisync(uri))
。然后([](存储文件^StorageFile)
{
返回FileIO::ReadTextAsync(storageFile);
})
。然后([this](字符串^jsonText)
{
JsonObject^JsonObject=JsonObject::Parse(jsonText);
auto jsonVector=jsonObject->GetNamedArray(“组”)->GetView();
for(const auto&jsonGroupValue:jsonVector)
{
JsonObject^groupObject=jsonGroupValue->GetObject();
SampleDataGroup^group=ref新建SampleDataGroup(groupObject->GetNamedString(“UniqueId”),
groupObject->GetNamedString(“标题”),
groupObject->GetNamedString(“字幕”),
groupObject->GetNamedString(“ImagePath”),
groupObject->GetNamedString(“说明”);
auto-jsonItemVector=groupObject->GetNamedArray(“项目”)->GetView();
for(const auto&jsonItemValue:jsonItemVector)
{
JsonObject^itemObject=jsonItemValue->GetObject();
SampleDataItem^item=ref新建SampleDataItem(itemObject->GetNamedString(“UniqueId”),
itemObject->GetNamedString(“标题”),
itemObject->GetNamedString(“字幕”),
itemObject->GetNamedString(“ImagePath”),
itemObject->GetNamedString(“说明”),
itemObject->GetNamedString(“内容”);
组->项目->追加(项目);
};
_组->附加(组);
String^ SampleDataItem::UniqueId::get()
{
return _uniqueId;
}
String^ SampleDataItem::Title::get()
{
return _title;
}
String^ SampleDataItem::Subtitle::get()
{
return _subtitle;
}
String^ SampleDataItem::Description::get()
{
return _description;
}
String^ SampleDataItem::Content::get()
{
return _content;
}
String^ SampleDataItem::ImagePath::get()
{
return _imagePath;
}
Windows::UI::Xaml::Data::ICustomProperty^ SampleDataItem::GetCustomProperty(Platform::String^ name)
{
return nullptr;
}
Windows::UI::Xaml::Data::ICustomProperty^ SampleDataItem::GetIndexedProperty(Platform::String^ name, Windows::UI::Xaml::Interop::TypeName type)
{
return nullptr;
}
Platform::String^ SampleDataItem::GetStringRepresentation()
{
return Title;
}
Windows::UI::Xaml::Interop::TypeName SampleDataItem::Type::get()
{
return this->GetType();
}
//
// SampleDataGroup
//
SampleDataGroup::SampleDataGroup(String^ uniqueId, String^ title, String^ subtitle, String^ imagePath, String^ description) :
_uniqueId(uniqueId),
_title(title),
_subtitle(subtitle),
_description(description),
_imagePath(imagePath),
_items(ref new Vector<SampleDataItem^>())
{
}
String^ SampleDataGroup::UniqueId::get()
{
return _uniqueId;
}
String^ SampleDataGroup::Title::get()
{
return _title;
}
String^ SampleDataGroup::Subtitle::get()
{
return _subtitle;
}
String^ SampleDataGroup::Description::get()
{
return _description;
}
IObservableVector<SampleDataItem^>^ SampleDataGroup::Items::get()
{
return _items;
}
String^ SampleDataGroup::ImagePath::get()
{
return _imagePath;
}
Windows::UI::Xaml::Data::ICustomProperty^ SampleDataGroup::GetCustomProperty(Platform::String^ name)
{
return nullptr;
}
Windows::UI::Xaml::Data::ICustomProperty^ SampleDataGroup::GetIndexedProperty(Platform::String^ name, Windows::UI::Xaml::Interop::TypeName type)
{
return nullptr;
}
Platform::String^ SampleDataGroup::GetStringRepresentation()
{
return Title;
}
Windows::UI::Xaml::Interop::TypeName SampleDataGroup::Type::get()
{
return this->GetType();
}
//
// SampleDataSource
//
String^ address = "ms-appx:///DataModel/SampleData1900.json";
void Flag_Project::GroupedItemsPage::ComboBox_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
int selectedIndex = combobox1->SelectedIndex;
Object^ selectedItem = combobox1->SelectedItem;
if (selectedIndex = 1)
{
address = "ms-appx:///DataModel/SampleData.json";
this->Frame->Navigate(TypeName(GroupedItemsPage::typeid));
}
}
SampleDataSource::SampleDataSource()
{
_groups = ref new Vector<SampleDataGroup^>();
Uri^ uri = ref new Uri(address);
create_task(StorageFile::GetFileFromApplicationUriAsync(uri))
.then([](StorageFile^ storageFile)
{
return FileIO::ReadTextAsync(storageFile);
})
.then([this](String^ jsonText)
{
JsonObject^ jsonObject = JsonObject::Parse(jsonText);
auto jsonVector = jsonObject->GetNamedArray("Groups")->GetView();
for (const auto &jsonGroupValue : jsonVector)
{
JsonObject^ groupObject = jsonGroupValue->GetObject();
SampleDataGroup^ group = ref new SampleDataGroup(groupObject->GetNamedString("UniqueId"),
groupObject->GetNamedString("Title"),
groupObject->GetNamedString("Subtitle"),
groupObject->GetNamedString("ImagePath"),
groupObject->GetNamedString("Description"));
auto jsonItemVector = groupObject->GetNamedArray("Items")->GetView();
for (const auto &jsonItemValue : jsonItemVector)
{
JsonObject^ itemObject = jsonItemValue->GetObject();
SampleDataItem^ item = ref new SampleDataItem(itemObject->GetNamedString("UniqueId"),
itemObject->GetNamedString("Title"),
itemObject->GetNamedString("Subtitle"),
itemObject->GetNamedString("ImagePath"),
itemObject->GetNamedString("Description"),
itemObject->GetNamedString("Content"));
group->Items->Append(item);
};
_groups->Append(group);
};
})
.then([this](task<void> t)
{
try
{
t.get();
}
catch (Platform::COMException^ e)
{
OutputDebugString(e->Message->Data());
// TODO: If App can recover from exception,
// remove throw; below and add recovery code.
throw;
}
// Signal load completion event
_loadCompletionEvent.set();
});
}
IObservableVector<SampleDataGroup^>^ SampleDataSource::Groups::get()
{
return _groups;
}
SampleDataSource^ SampleDataSource::_sampleDataSource = nullptr;
task<void> SampleDataSource::Init()
{
if (_sampleDataSource == nullptr)
{
_sampleDataSource = ref new SampleDataSource();
}
return create_task(_sampleDataSource->_loadCompletionEvent);
}
task<IIterable<SampleDataGroup^>^> SampleDataSource::GetGroups()
{
return Init()
.then([]() -> IIterable<SampleDataGroup^> ^
{
return _sampleDataSource->Groups;
});
}
task<SampleDataGroup^> SampleDataSource::GetGroup(String^ uniqueId)
{
return Init()
.then([uniqueId]() -> SampleDataGroup ^
{
// Simple linear search is acceptable for small data sets
for (const auto& group : _sampleDataSource->Groups)
{
if (group->UniqueId == uniqueId)
{
return group;
}
}
return nullptr;
});
}
task<SampleDataItem^> SampleDataSource::GetItem(String^ uniqueId)
{
return Init()
.then([uniqueId]() -> SampleDataItem ^
{
// Simple linear search is acceptable for small data sets
for (const auto& group : _sampleDataSource->Groups)
{
for (const auto& item : group->Items)
{
if (item->UniqueId == uniqueId)
{
return item;
}
}
}
return nullptr;
});
}`