Image 我想为我的按钮设置背景图像。我撞车了

Image 我想为我的按钮设置背景图像。我撞车了,image,button,c++-cli,Image,Button,C++ Cli,我想为我的按钮设置背景图像。我的背景图像没有出现在按钮上。所以我决定记录我的按钮 this->myButton->BackgroundImage->FromFile( "c:\\red\\Desert.jpg"); myLog(this->myButton->BackgroundImage->ToString()); 我在记录字符串时发生崩溃: message Object reference not set to an instance of an obj

我想为我的按钮设置背景图像。我的背景图像没有出现在按钮上。所以我决定记录我的按钮

this->myButton->BackgroundImage->FromFile( "c:\\red\\Desert.jpg");
myLog(this->myButton->BackgroundImage->ToString());
我在记录字符串时发生崩溃:

message Object reference not set to an instance of an object

BackgroundImage属性是指向图像对象的指针,初始值为nullptr,因此您试图从不存在的对象访问方法。正确的代码可以是这样的:

this->myButton->BackgroundImage = Image::FromFile("c:\\red\\Desert.jpg");
// Now the image object is initialized and you can log it
myLog(this->myButton->BackgroundImage->ToString());

看起来您不需要在运行时这样做;您是否尝试过在运行时之前通过“属性”窗口进行设置?