为什么此页面保持为零?这是scope/hositing/reference/etc吗。问题?

Why is this page staying at zero? Is this a scope/hositing/reference/etc. issue?

基本上,我有一个功能

Action NewPageIfNecessary = () => {
    if ( curY > (page.Height - 20 ) ) 
    {
        D.AddPage();
        curY = 0;
        page = D.Pages[++pagenum];
    }
};

我写的是为了让我的 PDFsharp 绘图程序知道在我们离上一页太远的情况下创建并移至新页面。那夹在像

这样的代码之间
PdfDocument D = new PdfDocument();
D.Info.Title = "Self-Assessment Results Summary";
D.AddPage();
int pagenum = 0;
PdfPage page = D.Pages[pagenum];
page.Orientation = PageOrientation.Portrait;

foreach ( var Survey in Surveys )
{
    NewPageIfNecessary();
    gfx.DrawString(Survey.Title, new XFont("Arial", 24), XBrushes.Black, 20, curY += 32);
    foreach ( var S in Survey.SectionAverages )
    {
        NewPageIfNecessary();
        gfx.DrawString(S.SectionTitle, new XFont("Arial", 20), XBrushes.Black, 30, curY += 30); 

    }
    foreach ( var S in Survey.ViewModel ) 
    {
        NewPageIfNecessary();
        gfx.DrawString(S.Title, new XFont("Arial", 20), XBrushes.Black, 30, curY += 30);
        foreach ( var SS in S.SubSections )

但我注意到的问题是

page = D.Pages[++pagenum];

没有让我进入新页面。知道为什么吗?

您没有显示分配 gfx 的代码。您的 page 变量未定义绘制内容的位置。释放 gfx 并为新页面创建一个新页面。

另请参阅: