DevExpress 示例:在 Console、WinForms 和 WPF 应用中集成 AI 文本扩展
DevExpress-Examples/winforms-wpf-ai-text-extension
DevExpress 发布示例,演示如何在 .NET 控制台、WinForms 和 WPF 应用中注册 Azure OpenAI 服务并启用 AI 文本处理功能,覆盖 WinForms MemoEdit、RichEdit 及 WPF TextEdit、RichEditControl 组件。
Integrate DevExpress AI-Powered Text Extensions into Console, WinForms, and WPF Apps
This example registers an Azure OpenAI service and uses AI APIs within a .NET console application and adds AI-powered text processing features to the following DevExpress UI components:
Note
DevExpress does not provide a REST API or include built-in LLMs/SLMs. To use AI services, you need an active Azure/OpenAI subscription to obtain the necessary REST API endpoint, key, and model deployment name. This information must be added at application startup to register AI clients and enable DevExpress AI-powered features in your application.
If these environment variables are not set, the sample falls back to the DevExpress demo proxy (https://api.devexpress.com/demo-openai) so you can try it out of the box. The demo proxy is rate-limited and intended for evaluation only — use your own Azure/OpenAI credentials for development.
Implementation Details
Install the following NuGet packages:
-
Azure.AI.OpenAI (minimal version - 2.3.0-beta.2)
-
DevExpress.AIIntegration
-
Microsoft.Extensions.AI.OpenAI
Note
We use the following versions of the Microsoft.Extensions.AI.* libraries in our source code:
Microsoft.Extensions.AI| 9.7.1Microsoft.Extensions.AI.OpenAI| 9.7.1-preview.1.25365.4
We do not guarantee compatibility or correct operation with higher versions. Refer to the following announcement for additional information: DevExpress.AIIntegration moves to a stables version.
Register AI Services
The following code in the Program.cs file registers an Azure AI service in the application:
AIExtensionsContainerDefault defaultAIContainer; public SampleAITextModifier() { // Modify the following lines to obtain and pass your personal Azure OpenAI credentials to the `Register~` method. string azureOpenAIEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); if (string.IsNullOrEmpty(azureOpenAIEndpoint)) azureOpenAIEndpoint = "https://api.devexpress.com/demo-openai";//DevExpress demo proxy-server string azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY"); if (string.IsNullOrEmpty(azureOpenAIKey)) azureOpenAIKey = "DEMO";//Demo key string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENTNAME"); if (string.IsNullOrEmpty(deploymentName)) deploymentName = "demo";//DevExpress demo deployment IChatClient azureOpenAIClient = new AzureOpenAIClient(new Uri(azureOpenAIEndpoint), new System.ClientModel.ApiKeyCredential(azureOpenAIKey)).GetChatClient(deploymentName).AsIChatClient(); AIExtensionsContainerDesktop.Default.RegisterChatClient(azureOpenAIClient); }
Tip
Refer to the following help topic for instructions on registering OpenAI, Azure OpenAI, Ollama, and Semantic Kernel: Register AI Clients.
WinForms
Use the BehaviorManager component to activate AI-powered text transform extensions for DevExpress WinForms UI controls.
Drop the BehaviorManager component from the Toolbox onto a Form. Open the behavior manager's smart tag menu and click "Edit Behaviors" to edit behaviors. In the Behavior Collection Editor, create desired AI-powered text processing behaviors, attach them to corresponding UI controls, and configure behavior settings (for example, the Temperature):
Run the application. Select text in the MemoEdit or RichTextEdit control and invoke the context menu. The context menu will display an "AI Text Transform" submenu with AI-driven commands.
Click the desired command. AI will process the command and generate an answer. The AI-generated answer is displayed within a dialog. You can insert the answer directly into a document or text field with a single click. You can insert AI answers above/below the cursor, replace all content or selected text, or copy the answer to the clipboard.
WPF
To introduce AI-powered capabilities in DevExpress WPF UI controls, add the following namespaces to a Window:
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:dxai="http://schemas.devexpress.com/winfx/2008/xaml/ai"
The following code snippet attaches AI-powered text processing behaviors to a DevExpress Text Editor:
<dxe:TextEdit TextWrapping="Wrap"> <dxmvvm:Interaction.Behaviors> <dxai:ExpandBehavior /> <dxai:ShortenBehavior/> <dxai:SummarizeBehavior/> <dxai:ExplainBehavior/> <dxai:ChangeToneBehavior /> <dxai:ProofreadBehavior/> <dxai:ChangeStyleBehavior/> <dxai:TranslateBehavior> <dxai:LanguageInfo Culture="en-US"/> <dxai:LanguageInfo Culture="de-DE"/> <dxai:LanguageInfo Culture="fr-FR"/> </dxai:TranslateBehavior> <dxai:CustomRequestBehavior/> </dxmvvm:Interaction.Behaviors> </dxe:TextEdit>
Files to Review
- Console App: Program.cs
- WinForms App:
- WPF App:
- App.xaml.cs (VB: App.xaml.vb)
- MainWindow.xaml (VB: MainWindow.xaml)
- RichEditWindow.xaml (VB: RichEditWindow.xaml)
Related Examples
- Rich Text Editor and HTML Editor for Blazor - How to integrate AI-powered extensions
- AI Chat for Blazor - How to add DxAIChat component in Blazor, MAUI, WPF, and WinForms applications
Does This Example Address Your Development Requirements/Objectives?
(you will be redirected to DevExpress.com to submit your response)
来源:GitHub · github.com
