Visual Studio for Mac中C#项目使用SoundPlayer类报错的解决建议咨询
Hey there! I've run into this exact issue before when working with SoundPlayer on Mac, so let's get you sorted out quickly.
The error message is telling you exactly what's wrong: SoundPlayer lives in the System.Windows.Extensions assembly, which isn't included by default in most .NET Core/.NET 5+ projects (which is what you're probably using in Visual Studio for Mac). Here's how to fix it step by step:
Add the required NuGet package
- In your Visual Studio for Mac solution explorer, right-click your project name and select Add > Add NuGet Package...
- In the search bar, type
System.Windows.Extensionsand hit enter. Look for the official package published by Microsoft. - Click Add Package to install it into your project.
Verify your using statement
Once the package is installed, make sure your code includes this at the top:using System.Media;Now the
SoundPlayertype should be recognized without errors.Quick test snippet
If you want to confirm everything's working, try this simple example (replace the file path with a valid WAV file on your Mac):var soundPlayer = new SoundPlayer("/Users/yourname/Documents/test.wav"); soundPlayer.Play();
A quick note: This fix applies to .NET Core 3.0+ and .NET 5/6/7/8 projects. If you're using an older .NET Framework project (less common on Mac), you'd instead add a direct assembly reference, but NuGet is the modern and recommended approach here.
内容的提问来源于stack exchange,提问作者Umberto Boldarin

