C++/CX:Platform :: String vs. std :: wstring

Pau*_*ams 3 stringbuilder windows-8 c++-cx

似乎C++/CX没有StringBuilder类或等价的,所以我认为我们是为了这个而使用STL?

这是我的第一个 C++/CX应用程序.用户在TextBox中输入一些文本,点击ENTER按钮,文本被附加到TextBlock"控制台".这段代码确实有效,但最佳做法是什么?

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);
};
Run Code Online (Sandbox Code Playgroud)

...

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());
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*lis 5

似乎C++/CX没有StringBuilder类或等价的,所以我认为我们是为了这个而使用STL?

是的,一般来说,您更喜欢在C++代码中使用C++类型.Platform::String仅在您必须的位置使用Windows运行时类型(例如),例如,跨ABI边界或跨组件边界传递数据时.

您的代码段中的字符串处理看起来很好 - 复制到std::wstringfor变异是合理的.