PDFsharp中PdfPage的Width/Height已过时,如何修复相关警告?
解决PDFsharp 6.1过时API警告的代码修改方案
原代码
gfx.DrawString( "Bit Depth: " + lightning.BitDepth, new XFont("Arial", 14), XBrushes.Black, new XRect(50, 330, page.Width, page.Height), XStringFormats.TopLeft);
触发的警告
Severity Code Description Project File Line Suppression State
Warning (active) CS0618 'XUnit.implicit operator double(XUnit)' is obsolete: 'In PDFsharp 6.1 implicit conversion to double is marked obsolete, because it led to misunderstandings and unexpected behavior. Use the XUnit.Point property instead.' PdfReporter.cs 54
修改说明
警告提示使用XUnit.Point属性,但你提到实际仅存在XUnit.FromPoint方法。核心问题是避免XUnit隐式转换为double,需显式处理单位:
两种可行修改方式:
若
page.Width/page.Height为XUnit类型,且实际存在Point属性(建议优先验证,警告提示通常与实际API匹配,可能是查找疏漏):
直接调用Point属性获取数值,避免隐式转换:gfx.DrawString( "Bit Depth: " + lightning.BitDepth, new XFont("Arial", 14), XBrushes.Black, new XRect(50, 330, page.Width.Point, page.Height.Point), XStringFormats.TopLeft);若仅存在
XUnit.FromPoint方法:
确保XRect的参数为XUnit实例,而非double,避免触发隐式转换警告:gfx.DrawString( "Bit Depth: " + lightning.BitDepth, new XFont("Arial", 14), XBrushes.Black, new XRect(XUnit.FromPoint(50), XUnit.FromPoint(330), page.Width, page.Height), XStringFormats.TopLeft);
批量修改逻辑
所有同类警告都可按此规则处理:要么显式调用Point属性获取数值,要么使用FromPoint创建XUnit实例,彻底规避XUnit到double的隐式转换。
内容的提问来源于stack exchange,提问作者Daniel Lip
相关产品推荐
相关产品推荐

