如何在Visual FoxPro 9中集成WPF用户控件或窗口?求完整COM互操作示例
Hey RobDog, I totally get where you're coming from—integrating Visual FoxPro 9 with WPF via COM interop can feel like searching for a complete guide when all you turn up are scattered snippets. Let's build a fully working "Hello World" example that covers both the FoxPro side and the WPF side step by step, so you have a solid foundation to build on for your migration.
This example will create a COM-visible WPF component that VFP can instantiate and call, passing data between the two systems.
WPF Side: Build a COM-Visible Component
Step 1: Create a WPF Class Library Project
- Open Visual Studio and create a new WPF Class Library project (name it
WpfComInteropDemofor consistency). - Delete the default
Class1.csfile—we'll add a window and a COM-visible wrapper class instead.
Step 2: Configure COM Visibility
- Right-click the project → Properties → Build tab.
- Check the box for "Make assembly COM-Visible".
- Under the Debug tab, check "Register for COM Interop" (this auto-registers the DLL during debugging; for deployment, we'll use
regasmlater).
Step 3: Add the WPF Window
Add a new WPF Window to the project (name it HelloWindow). Update its XAML to display a dynamic greeting:
<Window x:Class="WpfComInteropDemo.HelloWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF Hello World" Height="200" Width="350"> <Grid> <TextBlock x:Name="GreetingText" FontSize="22" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Window>
Update the code-behind for HelloWindow.cs to accept a username and set the greeting:
using System; using System.Windows; namespace WpfComInteropDemo { public partial class HelloWindow : Window { // Public property to receive data from VFP public string UserName { get; set; } public HelloWindow() { InitializeComponent(); } protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); GreetingText.Text = $"Hello, {UserName}! This is WPF talking to VFP."; } } }
Step 4: Create the COM-Visible Wrapper Class
Add a new class file (WpfComWrapper.cs) to expose the WPF window to COM. Generate unique GUIDs for the interface and class (use Visual Studio's Tools → Create GUID tool, select format 5):
using System; using System.Runtime.InteropServices; using System.Windows; namespace WpfComInteropDemo { // COM interface to define accessible methods [ComVisible(true)] [Guid("YOUR-UNIQUE-GUID-HERE")] // Replace with your generated GUID public interface IWpfHelloService { void ShowGreetingWindow(string userName); } // COM-visible class implementing the interface [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] [Guid("ANOTHER-UNIQUE-GUID-HERE")] // Replace with your generated GUID public class WpfHelloService : IWpfHelloService { public void ShowGreetingWindow(string userName) { // Initialize WPF application context and show the window var app = new Application(); var helloWindow = new HelloWindow { UserName = userName }; app.Run(helloWindow); } } }
Step 5: Compile and Register the COM Component
- Build the project (Ctrl+Shift+B).
- For deployment (or if you didn't check "Register for COM Interop"), open an elevated Visual Studio Command Prompt and run:
This registers the DLL in the Windows Registry so VFP can find it.regasm /codebase "C:\Path\To\Your\Project\Bin\Debug\WpfComInteropDemo.dll"
FoxPro Side: Call the WPF COM Component
Create a new VFP program file (CallWpfHello.prg) with the following code:
* Create an instance of the WPF COM component loWpfService = CREATEOBJECT("WpfComInteropDemo.WpfHelloService") * Pass a username to the WPF component and show the window loWpfService.ShowGreetingWindow("RobDog") * Clean up the object reference RELEASE loWpfService
Run the program in VFP—you should see the WPF window pop up with your custom greeting!
Key Notes for Your Migration
- Threading: Both VFP and WPF use STA (Single-Threaded Apartment) threading by default, so you shouldn't hit major thread conflicts here. If you need async operations, add proper thread marshaling in the WPF component.
- Data Exchange: For more complex data (not just strings), create COM-visible DTO (Data Transfer Object) classes in WPF, or use ADO.NET in WPF to directly read/write VFP DBF files (via the
System.Data.OleDbprovider). - Gradual Migration: Start with small, self-contained modules (like a report viewer or data entry form) as COM components, test them in VFP, then gradually replace larger parts of your app.
- Deployment: When deploying to end users, include the WPF DLL, its dependencies (e.g.,
PresentationFramework.dll,WindowsBase.dll), and runregasmduring installation (or use a setup project to handle registration).
内容的提问来源于stack exchange,提问作者RobDog

