在Visual Studio for Mac中基于C#创建跨平台桌面应用并在Windows端编译的实现方案问询
Hey there! I totally get wanting to code on your Mac (way nicer than a VM, right?) and then ship your app to run on Windows 10. Let's walk through the best options that fit your exact needs, including a clear path for .NET MAUI (the successor to Xamarin.Forms) which is perfect for this scenario:
Option 1: .NET MAUI (Recommended)
This is Microsoft's official cross-platform framework for building desktop (Windows/Mac) and mobile apps with shared C#/XAML code—exactly what you were looking for with Xamarin.Forms, but updated and fully supported across both Visual Studio for Mac and Windows.
Steps to implement:
Create the project on Visual Studio for Mac
- Open VS for Mac, click "Create a new project"
- Search for
.NET MAUI App, select it, then click "Next" - Name your project, pick a save location, and choose a stable .NET version (7 or 8 work great)
- Once created, you can edit code, test the Mac desktop version, and share 90%+ of your code with Windows
Move the project to your Windows VM
- Sync the entire project folder to your VM using a shared folder, cloud storage (OneDrive/Dropbox), or direct file transfer
- Open Visual Studio for Windows, click "Open a project or solution", and select the
.slnfile from your synced folder
Compile and run on Windows 10
- First, make sure VS for Windows has the
.NET Multi-platform App UI developmentworkload installed (open the VS Installer, check this under "Desktop & Mobile") - In the VS toolbar, set the target platform to
Windows Machine(orWindows (XAML)) - Hit the run button—your app will compile and launch directly on Windows 10!
- First, make sure VS for Windows has the
Option 2: WPF with .NET 5+
If you only need a Windows desktop app (no Mac desktop support), you can still write all your code on Mac and compile it on Windows. VS for Mac lets you edit WPF code (XAML and C#), even though you can't run it locally.
Steps to implement:
- Create a WPF project on Visual Studio for Mac
- New project → search for
WPF App (.NET) - Configure your project settings and create it—you can edit all your UI and logic code here
- New project → search for
- Sync to Windows VM
- Transfer the project folder to your VM as described above
- Compile on Windows
- Open the solution in VS for Windows (make sure the
.NET Desktop Developmentworkload is installed) - Compile and run—WPF will natively run on Windows 10
- Open the solution in VS for Windows (make sure the
Option 3: Electron (Web-Based)
If you prefer working with HTML/CSS/JS, Electron lets you build cross-platform desktop apps using web tech. You can edit code on Mac (VS for Mac or VS Code works great) and then run/pack it on Windows.
Steps to implement:
- Set up the Electron project on Mac
- Open Terminal, create a new folder, run
npm initto set up your project - Install Electron:
npm install electron --save-dev - Write your main process code (
main.js) and UI (index.html)—VS for Mac can open the folder for editing - Test locally on Mac with
npx electron .
- Open Terminal, create a new folder, run
- Run on Windows VM
- Sync the project folder to your VM, ensure Node.js and npm are installed on Windows
- Run
npm installto set up dependencies, thennpx electron .to launch the app - To package it as a Windows executable, install
electron-packagerwithnpm install electron-packager --save-dev, then runnpx electron-packager . --platform=win32
Quick Tips
- Keep .NET versions aligned: Make sure both Mac and Windows have the same .NET SDK version installed to avoid compilation errors
- Simplify file sync: Use a shared folder between your Mac and VM (most virtualization tools like Parallels/VirtualBox support this) for seamless project access
- Skip VM coding entirely: Since you're only compiling/running on the VM, you can do all your development on Mac and just switch to the VM when you need to test Windows-specific behavior
内容的提问来源于stack exchange,提问作者dennis605

