【发布时间】:2023-03-27 02:35:01
【问题描述】:
我是 Xamarin 和 C# 的新手。
首先我正在展示我的视图 customer.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:gridlook="clr-namespace:BlankApp5.Helpers"
xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
x:Class="BlankApp5.Views.Customer">
<ContentPage.ToolbarItems>
<ToolbarItem Text="SAVE AS PDF" Order="Secondary" Clicked="SaveAsPdf" />
</ContentPage.ToolbarItems>
<ContentPage.Resources>
<ResourceDictionary>
<gridlook:Gridhelp x:Key="gridhelp" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<syncfusion:SfDataGrid
Grid.Row="0"
x:Name="dataGrid_cust"
GridStyle="{StaticResource gridhelp}"
ItemsSource="{Binding Customer_data}"
ColumnSizer="Star"
AutoGenerateColumns="False"
AllowSorting="True"
SelectionMode="Single"
GridTappedCommand="{Binding Tapped}"
EnableDataVirtualization="True"
AllowResizingColumn="True">
<syncfusion:SfDataGrid.Columns
x:TypeArguments="syncfusion:Columns">
<syncfusion:GridTextColumn HeaderText="Cust ID"
HeaderFontAttribute="Bold"
MappingName="Id" />
<syncfusion:GridTextColumn MappingName="Name" HeaderFontAttribute="Bold"/>
<syncfusion:GridTextColumn HeaderText="Balance"
HeaderFontAttribute="Bold"
MappingName="CurrentBalance" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
<Grid Grid.Row="1"
BackgroundColor="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
Text="TOTAL BALANCE"
TextColor="Black"
FontAttributes="Bold"
VerticalOptions="Center"
/>
<Label
Grid.Column="1"
Text="{Binding Total}"
TextColor="Black"
HorizontalOptions="End"
FontAttributes="Bold"
VerticalOptions="Center"/>
</Grid>
</Grid>
</ContentPage.Content>
</ContentPage>
您可以在工具栏项目中看到 Clicked 事件
现在我将展示背后的代码
public partial class Customer : ContentPage
{
public Customer()
{
InitializeComponent();
}
private void SaveAsPdf(object sender, EventArgs e)
{
DataGridPdfExportingController pdfExport = new DataGridPdfExportingController();
pdfExport.HeaderAndFooterExporting += PdfExport_HeaderAndFooterExporting;
MemoryStream stream = new MemoryStream();
var pdfDoc = new PdfDocument();
PdfPage page = pdfDoc.Pages.Add();
PdfGraphics graphics = page.Graphics;
var exportToPdfGrid = pdfExport.ExportToPdfGrid(this.dataGrid_cust, this.dataGrid_cust.View, new DataGridPdfExportOption()
{
FitAllColumnsInOnePage = true,
}, pdfDoc);
PdfStringFormat drawFormat = new PdfStringFormat();
drawFormat.WordWrap = PdfWordWrapType.Word;
drawFormat.Alignment = PdfTextAlignment.Center;
drawFormat.LineAlignment = PdfVerticalAlignment.Top;
PdfStringFormat drawFormatright = new PdfStringFormat();
drawFormatright.WordWrap = PdfWordWrapType.Word;
drawFormatright.Alignment = PdfTextAlignment.Right;
drawFormatright.LineAlignment = PdfVerticalAlignment.Top;
//Set the font.
PdfFont fontco = new PdfStandardFont(PdfFontFamily.Helvetica,20f,PdfFontStyle.Bold);
PdfFont fonthe = new PdfStandardFont(PdfFontFamily.Helvetica,15f,PdfFontStyle.Bold);
//Create a solid brush.
PdfBrush brush = new PdfSolidBrush(new PdfColor(Syncfusion.Drawing.Color.Black));
RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height ));
graphics.DrawString(CheckFirst.CompData[0], fontco, brush, bounds.X+240 ,bounds.Y , drawFormat);
graphics.DrawString(CheckFirst.CompData[1], fonthe, brush, bounds.X+20, bounds.Y + 35, drawFormat);
graphics.DrawString(CheckFirst.CompData[1], fonthe, brush, page.Graphics.ClientSize.Width-30, bounds.Y + 35, drawFormat);
exportToPdfGrid.Draw(page, new PointF(10, 80));
pdfDoc.Save(stream);
pdfDoc.Close(true);
if (Device.RuntimePlatform == "WinPhone" || Device.RuntimePlatform == "Windows")
{
Xamarin.Forms.DependencyService.Get<ISaveWidowsPhone>().Save(CheckFirst.CompanyName+"_CUSTOMER_REPORT.pdf", "application/pdf", stream);
}
else
Xamarin.Forms.DependencyService.Get<ISave>().Save(CheckFirst.CompanyName + "_CUSTOMER_REPORT.pdf", "application/pdf", stream);
}
private void PdfExport_HeaderAndFooterExporting(object sender, PdfHeaderFooterEventArgs e)
{
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold);
var width = e.PdfPage.GetClientSize().Width;
PdfPageTemplateElement header = new PdfPageTemplateElement(width, 38);
header.Graphics.DrawString("Order Details", font, PdfPens.Black, 70, 3);
e.PdfDocumentTemplate.Top = header;
PdfPageTemplateElement footer = new PdfPageTemplateElement(width, 38);
footer.Graphics.DrawString("Order Details", font, PdfPens.Black, 70, 3);
e.PdfDocumentTemplate.Bottom = footer;
}
}
}
代码执行没有任何错误,但是
pdfExport.HeaderAndFooterExporting += PdfExport_HeaderAndFooterExporting;
此行不会执行我在下面定义的事件。我尝试了断点。该行已执行,但它从未将执行传递给
private void PdfExport_HeaderAndFooterExporting(object sender, PdfHeaderFooterEventArgs e)
【问题讨论】:
-
该行不控制函数,而是注册事件处理程序。 IE;如果触发 HeaderAndFooterExporting 事件,则调用该事件。但是您所做的只是注册它并不能保证会触发偶数。
-
在所有情况下我都需要触发它。你能帮我这样做吗? .实际上,我从 xamarin syncfusion sfdatagrid 文档中获得了代码。我不知道它是如何工作的。我只需要在每次执行代码时触发该事件。
标签: c# xamarin events xamarin.forms