It seems that C++/CX does not have a StringBuilder class, or equivalent, so I take it we are to be using STL for this?
This is my very first C++/CX app. The user enters some text in a TextBox, hits the ENTER button, and the text is appended to a TextBlock “console”. This code does work, but what’s the best practice?
public ref class MainPage sealed
{
public:
MainPage();
protected:
virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
private:
std::wstring _commandLine;
std::wstring _consoleText;
void Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
};
…
void HelloConsole::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
this->_commandLine = this->InputLine->Text->Data();
this->_commandLine.append(L"\n");
this->_consoleText += this->_commandLine;
this->InputLine->Text = "";
this->OutputConsole->Text = ref new Platform::String(_consoleText.c_str());
}
Yes, and in general, you should prefer to use C++ types in C++ code. Use the Windows Runtime types (like
Platform::String) only where you must, e.g. when passing data across the ABI boundary or across a component boundary.The string processing in your code snippet looks fine–copying into a
std::wstringfor mutation is reasonable.