在 Windows-10 的 Windows Universal App 中使用打印功能时,PrintingRoot.Children.Add(firstPage) Getting Null 异常错误
While Using a Print Functionality in Windows Universal App in Windows-10 ,the PrintingRoot.Children.Add(firstPage) Getting Null exception Error
我正在实现一个几乎完成的打印功能,但是当单击打印按钮时,它会在 PrintingRoot.Children.Add(firstPage)
.
中抛出一个空异常
代码:
protected override void PreparePrintContent()
{
try
{
trys = trysgetorderdetail;
OrderDetailResponse obj1 = JsonConvert.DeserializeObject<OrderDetailResponse>(trys);
if (firstPage == null)
{
firstPage = new popup(obj1);
}
PrintingRoot.Children.Add(firstPage);
PrintingRoot.InvalidateMeasure();
PrintingRoot.UpdateLayout();
}
catch (HttpRequestException ex)
{
}
catch (Exception ex) { }
}
protected virtual Canvas PrintingRoot
{
get
{
return FindName("printingRoot") as Canvas;
}
}
所以建议我一个解决方案
根据您的代码路径,空引用异常可能有多种原因。并且这个异常的解决方法应该是通过调试找到的。
我会做的是:
检查 PrintingRoot
是否为空
检查 PrintingRoot.Children
是否为空
如果存在我们在此处看不到的路径,则继续这样。
您可以如下查看:
if(PrintingRoot == null) throw new Exception("PrintingRoot object is not set");
if(PrintingRoot.Children == null) throw new Exception("PrintingRoot.Children object is not set");
之后你应该为你找到的相应句柄设置一个实例。
更新:
现在你知道什么是 null 了。显然 FindName("printingRoot") as Canvas
没有返回任何有效对象。
因此,原因可能是您的 Canvas
对象名称为空,或者是其他名称。确保您输入正确。下面是一个例子。
<Canvas Name="PrintingRoot" HorizontalAlignment="Left" Height="100" Margin="215,127,0,0" VerticalAlignment="Top" Width="100"/>
注意 Name
属性!
我正在实现一个几乎完成的打印功能,但是当单击打印按钮时,它会在 PrintingRoot.Children.Add(firstPage)
.
代码:
protected override void PreparePrintContent()
{
try
{
trys = trysgetorderdetail;
OrderDetailResponse obj1 = JsonConvert.DeserializeObject<OrderDetailResponse>(trys);
if (firstPage == null)
{
firstPage = new popup(obj1);
}
PrintingRoot.Children.Add(firstPage);
PrintingRoot.InvalidateMeasure();
PrintingRoot.UpdateLayout();
}
catch (HttpRequestException ex)
{
}
catch (Exception ex) { }
}
protected virtual Canvas PrintingRoot
{
get
{
return FindName("printingRoot") as Canvas;
}
}
所以建议我一个解决方案
根据您的代码路径,空引用异常可能有多种原因。并且这个异常的解决方法应该是通过调试找到的。
我会做的是:
检查
PrintingRoot
是否为空检查
PrintingRoot.Children
是否为空如果存在我们在此处看不到的路径,则继续这样。
您可以如下查看:
if(PrintingRoot == null) throw new Exception("PrintingRoot object is not set");
if(PrintingRoot.Children == null) throw new Exception("PrintingRoot.Children object is not set");
之后你应该为你找到的相应句柄设置一个实例。
更新:
现在你知道什么是 null 了。显然 FindName("printingRoot") as Canvas
没有返回任何有效对象。
因此,原因可能是您的 Canvas
对象名称为空,或者是其他名称。确保您输入正确。下面是一个例子。
<Canvas Name="PrintingRoot" HorizontalAlignment="Left" Height="100" Margin="215,127,0,0" VerticalAlignment="Top" Width="100"/>
注意 Name
属性!