使用PdfSharp.Xamarin.Forms创建PDF时无法显示ListView数据
我是xamarin的新手,我正在使用PdfSharp.xamarin.Forms nuget为Android和iOS创建xamarin表单中的PDF。问题是我无法呈现ListView。他们已经提到了它,需要为它编写一个渲染器。但我不知道如何创建和绑定它 我就是这样做的使用PdfSharp.Xamarin.Forms创建PDF时无法显示ListView数据,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我是xamarin的新手,我正在使用PdfSharp.xamarin.Forms nuget为Android和iOS创建xamarin表单中的PDF。问题是我无法呈现ListView。他们已经提到了它,需要为它编写一个渲染器。但我不知道如何创建和绑定它 我就是这样做的 <Grid x:Name="mainGrid"> <ScrollView> <StackLayout Padding="4" Orientation
<Grid x:Name="mainGrid">
<ScrollView>
<StackLayout Padding="4" Orientation="Vertical">
<!--<Image HeightRequest="80" Source="logojpeg.jpg" Margin="0,0,0,5"/>-->
<Label FontSize="18" TextColor="Black" FontFamily="{StaticResource timesNewRomanBold}" HorizontalOptions="CenterAndExpand" Text="Monthly Motor Renew List of Jayasekara (900585) as at January, 2020"/>
<Label FontSize="18" TextColor="Black" FontFamily="{StaticResource timesNewRomanBold}" HorizontalOptions="CenterAndExpand" Text="Report generated on 27 December, 2019" Margin="0,0,0,5"/>
<ListView x:Name="renewListView"
Footer=""
pdf:PdfRendererAttributes.ListRendererDelegate="{StaticResource PDFSampleListRendererDelegate}"
BackgroundColor="White"
SeparatorVisibility="None"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell IsEnabled="false">
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
</Grid>
在代码隐藏中
public partial class MotorRenewalFinalPrint : ContentPage
{
public MotorRenewalFinalPrint()
{
InitializeComponent();
}
public MotorRenewalFinalPrint (List<MotorRenewalPrintData> newdd)
{
InitializeComponent ();
Title = "Save as PDF";
renewListView.ItemsSource = newdd;
}
private void pdf_Clicked(object sender, EventArgs e)
{
var pdf = PDFManager.GeneratePDFFromView(mainGrid);
var fileManager = DependencyService.Get<IFileIO>();
string filePath = Path.Combine(fileManager.GetMyDocumentsPath(), "formpdf.pdf");
DependencyService.Get<IPdfSave>().Save(pdf, filePath);
DependencyService.Get<IPDFPreviewProvider>().TriggerPreview(filePath);
}
}
公共部分类MotorRenewalFinalPrint:ContentPage
{
公共汽车更新FinalPrint()
{
初始化组件();
}
公共汽车更新最终打印(列表更新)
{
初始化组件();
Title=“另存为PDF”;
renewListView.ItemsSource=newdd;
}
私有无效pdf_已单击(对象发送者,事件参数e)
{
var pdf=PDFManager.generatePDFromView(主网格);
var fileManager=DependencyService.Get();
字符串filePath=Path.Combine(fileManager.GetMyDocumentsPath(),“formpdf.pdf”);
DependencyService.Get().Save(pdf,文件路径);
DependencyService.Get().TriggerPreview(文件路径);
}
}
已更新…
主类
public partial class MainPage : ContentPage
{
private List<Customer> Cus = new List<Customer>();
public MainPage()
{
InitializeComponent();
Customer ss1 = new Customer { Names = "test1", Ages = "10"};
Customer ss2 = new Customer { Names = "test2", Ages = "30" };
Cus.Add(ss1);
Cus.Add(ss2);
//rListView.ItemsSource = Cus;
}
private void Button_Clicked(object sender, System.EventArgs e)
{
var pdf = PDFManager.GeneratePDFFromView(mainGrid);
var fileManager = DependencyService.Get<IFileIO>();
string filePath = Path.Combine(fileManager.GetMyDocumentsPath(), "testpdf.pdf");
DependencyService.Get<IPdfSave>().Save(pdf, filePath);
DependencyService.Get<IPDFPreviewProvider>().TriggerPreview(filePath);
}
}
public分部类主页面:ContentPage
{
私有列表Cus=新列表();
公共主页()
{
初始化组件();
客户ss1=新客户{Names=“test1”,Ages=“10”};
客户ss2=新客户{Names=“test2”,Ages=“30”};
客户添加(ss1);
客户添加(ss2);
//rListView.ItemsSource=Cus;
}
已单击私有无效按钮(对象发送者,System.EventArgs e)
{
var pdf=PDFManager.generatePDFromView(主网格);
var fileManager=DependencyService.Get();
字符串filePath=Path.Combine(fileManager.GetMyDocumentsPath(),“testpdf.pdf”);
DependencyService.Get().Save(pdf,文件路径);
DependencyService.Get().TriggerPreview(文件路径);
}
}
查看
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TestPDF"
xmlns:pdf="clr-namespace:PdfSharp.Xamarin.Forms;assembly=PdfSharp.Xamarin.Forms"
x:Class="TestPDF.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<local:PDFSampleListRendererDelegate x:Key="PDFSampleListRendererDelegate" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid x:Name="mainGrid">
<ScrollView>
<StackLayout Margin="0,0,0,5">
<Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="CenterAndExpand" TextColor="Black" FontSize="18" VerticalOptions="Center" />
<ListView pdf:PdfRendererAttributes.ListRendererDelegate="{DynamicResource PDFSampleListRendererDelegate}" HeightRequest="150"/>
<Button Text="click" Clicked="Button_Clicked"/>
</StackLayout>
</ScrollView>
</Grid>
</ContentPage.Content>
</ContentPage>
PDFSampleListRenderDelegate代表
public class PDFSampleListRendererDelegate : PdfListViewRendererDelegate
{
public override void DrawCell(ListView listView, int section, int row, XGraphics page, XRect bounds, double scaleFactor)
{
XFont font = new XFont("times" ?? GlobalFontSettings.FontResolver.DefaultFontName, 15);
var yourObject = (listView.ItemsSource as List<Customer>).ElementAt(row);
page.DrawString(yourObject.Names, font, XBrushes.Black, bounds,
new XStringFormat
{
LineAlignment = XLineAlignment.Center,
Alignment = XStringAlignment.Center,
});
}
public override void DrawFooter(ListView listView, int section, XGraphics page, XRect bounds, double scaleFactor)
{
base.DrawFooter(listView, section, page, bounds, scaleFactor);
}
public override double GetFooterHeight(ListView listView, int section)
{
return base.GetFooterHeight(listView, section);
}
}
公共类PDFSampleListRenderDelegate:PDFListViewRenderDelegate
{
公共覆盖无效绘图单元(ListView ListView、int节、int行、XGraphics页、XRect边界、双缩放因子)
{
XFont font=新XFont(“时间”?GlobalFontSettings.FontResolver.DefaultFontName,15);
var yourObject=(listView.ItemsSource作为列表).ElementAt(行);
page.DrawString(yourObject.Names、字体、XBrushes.Black、bounds、,
新XStringFormat
{
LineAlignment=XLineAlignment.Center,
对齐=XStringAlignment.Center,
});
}
public override void DrawFooter(ListView ListView、int部分、XGraphics页面、XRect边界、双缩放因子)
{
DrawFooter(listView、section、page、bounds、scaleFactor);
}
公共覆盖双GetFooterHeight(ListView ListView,int部分)
{
返回base.GetFooterHeight(listView,section);
}
}
您应该重写DrawCell
方法
i、 e:
public-override-void-DrawCell(ListView-ListView、int-section、int-row、XGraphics-page、XRect-bounds、double-scaleFactor)
{
XFont font=新XFont(您的自定义字体??GlobalFontSettings.FontResolver.DefaultFontName,label.FontSize*scaleFactor);
var yourObject=(listView.ItemSource作为列表).ElementAt(行);
page.DrawString(yourObject.Text、字体、XColors.Black、bounds、,
新XStringFormat{
LineAlignment=XLineAlignment.Center,
对齐=XStringAlignment.Center,
});
}
如何绑定PDFSampleListRenderDelegate??我已经在我的主项目中创建了这个类。在Xaml
中添加到Resources
:这不太可能。我仍然无法摆脱Xamarin.Forms.Xaml.XamlParseException:position149:35。找不到密钥PDFSampleListRenderDelegate的StaticResource错误。我已经用两个listview标签将测试简单项目发布到github。如果您能修改它以显示listview,我将非常感谢您。感谢链接:现在使用StaticResource
而不是Dynamicno错误。如何将列表数据绑定到要显示的PDFSampleListRenderDelegate?我已经用修改过的类更新了这个问题
public override void DrawCell(ListView listView, int section, int row, XGraphics page, XRect bounds, double scaleFactor)
{
XFont font = new XFont(yourCustomFont ?? GlobalFontSettings.FontResolver.DefaultFontName, label.FontSize * scaleFactor);
var yourObject = (listView.ItemSource as List<YourObjType>).ElementAt(row);
page.DrawString(yourObject.Text, font, XColors.Black, bounds,
new XStringFormat {
LineAlignment = XLineAlignment.Center,
Alignment = XStringAlignment.Center,
});
}