为什么变量声明抛出异常?
Why is variable declaration throwing an exception?
为翻译单元声明为全局变量的声明引发异常。
Visual Studio 2017 Community,版本 15.9.5,安装了 C++/WinRT 扩展。项目从“空白应用程序 (C++/WinRT) 模板开始。
我想要一个变量数组,SolidColorBrush myBrushes[2];
,只在一个翻译单元中全局使用。它在翻译单元的命名空间中声明。
我已经尝试完全限定类型,将类型标记为 static
,并尝试不使用数组指定。
#include "pch.h"
#include "MainPage.h"
//#include <winrt/Windows.UI.Xaml.Media.h>
using namespace winrt;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
//*************************************************************************************************
namespace winrt::event_Experiment::implementation
{
//*************************************************************************************************
//Windows::UI::Xaml::Media::SolidColorBrush myBrushes[2];
//static SolidColorBrush myBrushes[2];
SolidColorBrush myBrushes[2];
//*************************************************************************************************
MainPage::MainPage()
{
InitializeComponent();
//myBrush.Color(Windows::UI::Colors::Blue());
//myBrush[1].Color(Windows::UI::Colors::Red());
//myStackPanel().Background() = myBrush;
//SolidColorBrush tempBrush = SolidColorBrush(winrt::Windows::UI::Colors::Blue());
//myBrush(tempBrush);
myButton2().Click({ this, &MainPage::ClickHandler2 });
myStackPanel().PointerPressed({ this, &MainPage::spPointerPressed });
//myBrushes[0].Color(Windows::UI::Colors::Blue());
//myBrushes[1].Color(Windows::UI::Colors::Red());
}
抛出的异常如下图所示。
微软在 2018 年 5 月宣布更新 C++/WinRT 时声称,它是直接的 C++ 17,允许这样的声明。
我怎样才能让它工作?谢谢
"The claim made by Microsoft at the May, 2018 announcement of the update to C++/WinRT is that it is straight C++ 17 which would allow such a declaration."
C++/WinRT 是标准的 C++17,作为标准的 C++ 库,类型的构造函数可能会抛出异常,表明您使用错误。在这种情况下,构造函数失败,因为 SolidColorBrush 只能在 Xaml UI 线程上构造。正如雷蒙德在他的评论中指出的那样,您需要确保在创建 Xaml 资源之前初始化 Xaml 。一种方法是使用 nullptr 构造函数将构造推迟到以后。例如:
SolidColorBrush brush{ nullptr };
您可以在准备好后为画笔分配一个值。
brush = SolidColorBrush(...);
为翻译单元声明为全局变量的声明引发异常。
Visual Studio 2017 Community,版本 15.9.5,安装了 C++/WinRT 扩展。项目从“空白应用程序 (C++/WinRT) 模板开始。
我想要一个变量数组,SolidColorBrush myBrushes[2];
,只在一个翻译单元中全局使用。它在翻译单元的命名空间中声明。
我已经尝试完全限定类型,将类型标记为 static
,并尝试不使用数组指定。
#include "pch.h"
#include "MainPage.h"
//#include <winrt/Windows.UI.Xaml.Media.h>
using namespace winrt;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
//*************************************************************************************************
namespace winrt::event_Experiment::implementation
{
//*************************************************************************************************
//Windows::UI::Xaml::Media::SolidColorBrush myBrushes[2];
//static SolidColorBrush myBrushes[2];
SolidColorBrush myBrushes[2];
//*************************************************************************************************
MainPage::MainPage()
{
InitializeComponent();
//myBrush.Color(Windows::UI::Colors::Blue());
//myBrush[1].Color(Windows::UI::Colors::Red());
//myStackPanel().Background() = myBrush;
//SolidColorBrush tempBrush = SolidColorBrush(winrt::Windows::UI::Colors::Blue());
//myBrush(tempBrush);
myButton2().Click({ this, &MainPage::ClickHandler2 });
myStackPanel().PointerPressed({ this, &MainPage::spPointerPressed });
//myBrushes[0].Color(Windows::UI::Colors::Blue());
//myBrushes[1].Color(Windows::UI::Colors::Red());
}
抛出的异常如下图所示。
微软在 2018 年 5 月宣布更新 C++/WinRT 时声称,它是直接的 C++ 17,允许这样的声明。
我怎样才能让它工作?谢谢
"The claim made by Microsoft at the May, 2018 announcement of the update to C++/WinRT is that it is straight C++ 17 which would allow such a declaration."
C++/WinRT 是标准的 C++17,作为标准的 C++ 库,类型的构造函数可能会抛出异常,表明您使用错误。在这种情况下,构造函数失败,因为 SolidColorBrush 只能在 Xaml UI 线程上构造。正如雷蒙德在他的评论中指出的那样,您需要确保在创建 Xaml 资源之前初始化 Xaml 。一种方法是使用 nullptr 构造函数将构造推迟到以后。例如:
SolidColorBrush brush{ nullptr };
您可以在准备好后为画笔分配一个值。
brush = SolidColorBrush(...);