[英]Xamarin Forms Event
我是 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)
注冊和調用事件是兩件不同的事情。 我建議您在構造函數中注冊您的事件:(注銷只是一種最佳做法)
pdfExport.HeaderAndFooterExporting -= PdfExport_HeaderAndFooterExporting;
pdfExport.HeaderAndFooterExporting += PdfExport_HeaderAndFooterExporting;
並在您需要的地方調用它(在事件 SaveAsPdf 中,而不是您當前的注冊中):
HeaderAndFooterExporting?.Invoke (this, null);
實際上,您是在此處注冊一個事件而不是調用它。 檢查這個
注冊事件不像調用函數或方法。 為了調用你必須做的事件
HeaderAndFooterExporting?.Invoke (this, null);
希望這對你有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.