在页面之间导航时如何处理全局变量? (C++/CX XAML)

How to handle a global variable when Navigating between pages ? (C++/CX XAML)

尝试在 c++/cx XAML 中重现 this c# XAML 教程示例。 转到视频中的 this exact 处,了解我们添加全局变量的原因。

这是我如何声明我的变量"someImportantValue":

namespace NewMultiPages
{
    ref class App sealed
    {
    protected:
        virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ e) override;

    internal:
        App();
        static Platform::String ^someImportantValue;

    private:
        void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
        void OnNavigationFailed(Platform::Object ^sender, Windows::UI::Xaml::Navigation::NavigationFailedEventArgs ^e);
    };
}

这里是 page2 中的代码,我在这里点击按钮转到 page3:

void NewMultiPages::Page2::HyperlinkButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    App::someImportantValue = ValueTextBox->Text;
    this->Frame->Navigate(Page3::typeid, ValueTextBox->Text);
}

编译时出现此错误:

Erreur LNK2001 symbole externe non resolu "public: static class Platform::String ^ NewMultiPages::App::someImportantValue" (?someImportantValue@App@NewMultiPages@@2P$AAVString@Platform@@$AA) NewMultiPages C:\Users\lr\Documents\Visual Studio 2017\Projects\NewMultiPages\NewMultiPages\Page2。 xaml.obj 1

不确定引用 类 上静态字段的访问细节是什么,但是如果您从该字段中删除静态修饰符 - 您可以通过以下方式访问它:

static_cast<App^>(App::Current)->someImportantValue = ValueTextBox->Text;