当我在 onUpdateSlabPicture 函数中创建一个字符串类型变量时,我收到一个Null Reference异常(见下文)。
注意“ ApplicationManager.Istance.Paths.SlabPictureFile”在每个部分(Istance,Paths和SlabPictureFile)中都是不为空,我可以在手表中看到它们,而我可以将它们分配给变量。
这种情况:
// This method starts a task and when it's finished, it calls the other method below
private void takeAndCalibratePhoto()
{
if(!_startExecutionTakeAndCalibratePhoto)
{
MiMessage viewMessage = new MiMessage();
waitOperation(
(token) =>
{
_startExecutionTakeAndCalibratePhoto = true;
string errorMessage = "";
if(!_CameraManager.TakeAndCalibratePhoto(viewMessage, token, out errorMessage))
{
StopAction(null);
}
return errorMessage;
},
(p) =>
{
string errorMessage = (string)p;
if(errorMessage.Length > 0)
{
StopAction(null);
ApplicationManager.Istance.ShowInformationMessage(errorMessage);
}
else
{
onUpdateSlabPicture();
}
_startExecutionTakeAndCalibratePhoto = false;
},
viewMessage, true);
}
}
protected override void onUpdateSlabPicture()
{
if (_CameraManager.Photo.SlabBitmap != null)
{
// vvvvvvvv Null reference Exception vvvvvvvvvvvvvvvv
string fileDaSalvare = ApplicationManager.Istance.Paths.SlabPictureFile;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if (_Options.SelectionSlabImage)
{
List<string> listaNomi = new List<string>();
ApplicationManager.Istance.ShowQuertyPad("Slab Name".Translate(), System.IO.Path.GetFileNameWithoutExtension(ApplicationManager.Istance.Paths.SlabPictureFile), listaNomi, (name) => { fileDaSalvare = name; });
}
_CameraManager.Photo.SlabBitmap.Save(fileDaSalvare);
_CameraManager.Photo.SlabBitmap.Dispose();
_CameraManager.Photo.SlabBitmap = null;
}
}
为什么?此外,还有一个不太重要的问题:为什么我要用这种方式编写代码:
if (_CameraManager.Photo.SlabBitmap != null)
{
string fileDaSalvare;
if (_Options.SelectionSlabImage)
{
List<string> listaNomi = new List<string>();
ApplicationManager.Istance.ShowQuertyPad("Slab Name".Translate(), System.IO.Path.GetFileNameWithoutExtension(ApplicationManager.Istance.Paths.SlabPictureFile), listaNomi, (name) => { fileDaSalvare = name; });
}
else
{
fileDaSalvare = ApplicationManager.Istance.Paths.SlabPictureFile;
}
_CameraManager.Photo.SlabBitmap.Save(fileDaSalvare);
}
}
编译器无法识别匿名方法中字符串的初始化吗?