ライブ環境でテストする
ウォーターマークなしで本番環境でテストしてください。
必要な場所でいつでも動作します。
C#のイベントは、イベント駆動型プログラミングの基本的な部分です。 それらは、オブジェクトが通信を行い、興味深いことが発生したときに他のオブジェクトに通知することを可能にします。 このガイドでは、イベントの宣言と使用方法を探ります。 一つ一つ順を追って分解し、明確に理解できるようにしましょう。 また、以下を探りますIronPDFC#アプリケーションでのPDF操作のために。
C# のイベントはオブジェクト間の通信を可能にします。 イベントが発生すると、他のオブジェクトがそれに応答できます。 イベントは、メソッドへの型安全なポインタとして機能するデリゲートに依存します。 イベントデリゲート型は、公開イベントを処理できるメソッドのシグネチャを定義し、イベントデータの処理に一貫性を確保します。
イベントを完全に理解するために、主な構成要素を見てみましょう。
パブリッシャークラスはイベントのソースです。 これは、特定のアクションや条件が発生したときにイベントを宣言し、発生させる役割を担っています。 このプロセスには通常、イベントが発生したときを判断するためのイベントハンドラーメソッドが含まれます。 発行者はまた、イベントを処理できるメソッドのシグネチャを定義するためにイベントデリゲートを使用します。 たとえば、グラフィカルユーザーインターフェースでは(GUI (グラフィカルユーザーインターフェース))ボタンコントロールは「クリック」イベントを発生させたときにパブリッシャーとして機能します。
サブスクライバークラスはイベントを監視し、それに反応します。 サブスクライバーは、イベントハンドリングメソッドをイベントに接続することによって、イベントへの関心を登録します。 パブリッシャーがイベントを発生させると、サブスクライバーのイベントハンドラーメソッドが実行されます。 単一のイベントには複数のサブスクライバーが存在でき、各サブスクライバーはイベントが発生したときに異なる方法で応答します。
デリゲートはC#におけるイベントの基盤です。 それらはメソッドへの型安全なポインタであり、すべてのイベントハンドラが従うべき契約を定義します。 デリゲートは特定のシグネチャを持つメソッドのみがイベントを処理できるようにし、一貫性がありエラーのないイベント処理メカニズムを提供します。
イベントハンドラーは、イベントがトリガーされたときに実行されるサブスクライバークラスのメソッドです。 それらは、UIの更新、データのログ記録、計算の実行など、イベントを処理するためのロジックを含んでいます。 イベントハンドラーのシグネチャは、イベントに関連付けられたデリゲートの型と一致しなければなりません。 さらに、他のクラスはイベントハンドラーを使用して共有イベントに応答することができます。 イベントをモジュラーで再利用可能な方法で実装することを容易にします。
多くの場合、イベントはサブスクライバーに追加情報を伝える必要があります。 これはイベントデータクラスを使用して達成され、基本となるEventArgsクラスから派生しています。 イベントデータには、メッセージ、ステータス、その他の関連情報など、イベントに関する具体的な詳細が含まれています。
デリゲートは、イベントハンドラのメソッドシグネチャを定義します。 この例では、2つのパラメータを持つイベントハンドラを表すデリゲートを作成します。パラメータは、object sender と EventArgs e です。
public delegate void MyEventHandler(object sender, EventArgs e);
public delegate void MyEventHandler(object sender, EventArgs e);
Public Delegate Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
イベントは、eventキーワードを使用して宣言され、デリゲート型に基づいています。 例を挙げよう:
public class Publisher
{
public event MyEventHandler Notify; // Declare the event.
}
public class Publisher
{
public event MyEventHandler Notify; // Declare the event.
}
Public Class Publisher
Public Event Notify As MyEventHandler ' Declare the event.
End Class
イベントは、デリゲートを呼び出し、必要なパラメーターを渡すことによって発生します。
public void TriggerEvent()
{
if (Notify != null) // Check if there are subscribers.
{
Notify(this, EventArgs.Empty); // Raise the event.
}
}
public void TriggerEvent()
{
if (Notify != null) // Check if there are subscribers.
{
Notify(this, EventArgs.Empty); // Raise the event.
}
}
Public Sub TriggerEvent()
If Notify IsNot Nothing Then ' Check if there are subscribers.
Notify(Me, EventArgs.Empty) ' Raise the event.
End If
End Sub
購読者は、+=演算子を使用してイベントハンドラーを登録します。
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber();
publisher.Notify += subscriber.OnNotify; // Subscribe to the event.
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber();
publisher.Notify += subscriber.OnNotify; // Subscribe to the event.
Dim publisher As New Publisher()
Dim subscriber As New Subscriber()
publisher.Notify += subscriber.OnNotify ' Subscribe to the event.
イベントハンドラーは、デリゲート署名と一致するサブスクライバークラスのメソッドです。
public void OnNotify(object sender, EventArgs e)
{
Console.WriteLine("Event received!");
}
public void OnNotify(object sender, EventArgs e)
{
Console.WriteLine("Event received!");
}
Public Sub OnNotify(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Event received!")
End Sub
.NET で PDF を操作するための多用途なライブラリである IronPDF は、C# アプリケーションとシームレスに統合されます。 C# の イベントと組み合わせることで、進捗の更新、エラー処理、通知などのリアルタイムシナリオを動的に処理する方法を提供できます。PDF生成または操作。 この関係を魅力的に探求しましょう。 C#では、イベントは何かが発生したことを通知するための方法です。 それにより、プログラムのある部分が他の部分に特定の事象、例えばファイルの処理、タスクの完了、エラーの発生について通知することができます。
IronPDFは、PDFの生成、変更、セキュリティ保護を可能にし、イベントと統合することでアプリケーションをよりインタラクティブにすることができます。例えば:
こちらは、イベントを使用したIronPDFのシンプルな例です。
using IronPdf;
using System;
// Program class
class Program
{
// Define a custom event for progress updates
public static event Action<int> ProgressUpdated;
// public static void Main
public static void Main()
{
License.LicenseKey = "License-Key";
// Subscribe to the ProgressUpdated event
ProgressUpdated += DisplayProgress;
Console.WriteLine("Generating PDF...");
GeneratePdf();
}
static void GeneratePdf()
{
try
{
var Renderer = new ChromePdfRenderer();
for (int i = 0; i <= 100; i += 20)
{
// Simulate progress
System.Threading.Thread.Sleep(500);
ProgressUpdated?.Invoke(i); // Trigger event with progress value
}
// Generate a PDF
var PdfDocument = Renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF with Events!</h1>");
PdfDocument.SaveAs("IronPDF\example.pdf");
ProgressUpdated?.Invoke(100); // Final update
Console.WriteLine("PDF generated successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
static void DisplayProgress(int progress)
{
Console.WriteLine($"Progress: {progress}%");
}
}
using IronPdf;
using System;
// Program class
class Program
{
// Define a custom event for progress updates
public static event Action<int> ProgressUpdated;
// public static void Main
public static void Main()
{
License.LicenseKey = "License-Key";
// Subscribe to the ProgressUpdated event
ProgressUpdated += DisplayProgress;
Console.WriteLine("Generating PDF...");
GeneratePdf();
}
static void GeneratePdf()
{
try
{
var Renderer = new ChromePdfRenderer();
for (int i = 0; i <= 100; i += 20)
{
// Simulate progress
System.Threading.Thread.Sleep(500);
ProgressUpdated?.Invoke(i); // Trigger event with progress value
}
// Generate a PDF
var PdfDocument = Renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF with Events!</h1>");
PdfDocument.SaveAs("IronPDF\example.pdf");
ProgressUpdated?.Invoke(100); // Final update
Console.WriteLine("PDF generated successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
static void DisplayProgress(int progress)
{
Console.WriteLine($"Progress: {progress}%");
}
}
Imports IronPdf
Imports System
' Program class
Friend Class Program
' Define a custom event for progress updates
Public Shared Event ProgressUpdated As Action(Of Integer)
' public static void Main
Public Shared Sub Main()
License.LicenseKey = "License-Key"
' Subscribe to the ProgressUpdated event
AddHandler Me.ProgressUpdated, AddressOf DisplayProgress
Console.WriteLine("Generating PDF...")
GeneratePdf()
End Sub
Private Shared Sub GeneratePdf()
Try
Dim Renderer = New ChromePdfRenderer()
For i As Integer = 0 To 100 Step 20
' Simulate progress
System.Threading.Thread.Sleep(500)
RaiseEvent ProgressUpdated(i)
Next i
' Generate a PDF
Dim PdfDocument = Renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF with Events!</h1>")
PdfDocument.SaveAs("IronPDF\example.pdf")
RaiseEvent ProgressUpdated(100)
Console.WriteLine("PDF generated successfully!")
Catch ex As Exception
Console.WriteLine($"Error: {ex.Message}")
End Try
End Sub
Private Shared Sub DisplayProgress(ByVal progress As Integer)
Console.WriteLine($"Progress: {progress}%")
End Sub
End Class
C#のイベントとIronPDFを組み合わせることで、動的なPDF生成と管理のための強力なシステムを構築できます。 イベントはPDF操作を非同期に処理するためのクリーンで効率的な方法を提供しますが、IronPDFは.NETプラットフォーム全体でPDFの作成、編集、操作のための強力な機能を提供します。 IronPDFは無料試用すべての機能を制限なくテストするために。 商用ライセンスは749ドルからで、PDF生成および処理機能のフルスイートへのアクセスを提供します。
10 の .NET API 製品 オフィス文書用