'Xamarin.Forms : Error running program in debug mode after reinstalling Visual Studio 2019 v 16.11.11 preview 1.0
I had to reinstall Visual Studio 2019 after a Windows 10 crash. My program was developed under VS 2019 16.9.3, and the reinstalled version is 16.11.11 preview 1.0. I already had a problem with Mono.Android because the ‘Community’ folder has been replaced by ‘Preview’ (this issue was fixed in a previous post – thanks to Liyun Zhang).
I now have a new problem running the program in debug mode. Here is the error message:
Failed to instantiate task "XamlCTask" from "C:\Users\marce.nuget\packages\xamarin.forms\4.5.0.396\build\net46\Xamarin.Forms.Build.Tasks.dll". Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The specified file cannot be found. MemoCourses.Android C:\Users\marce.nuget\packages\xamarin.forms\4.5.0.396\build\Xamarin.Forms.targets.
Here is the code in Xamarin.Forms.targets :
<XamlCTask
Assembly = "$(IntermediateOutputPath)$(TargetFileName)"
ReferencePath = "@(ReferencePath)"
OptimizeIL = "true"
DebugSymbols = "$(DebugSymbols)"
DebugType = "$(DebugType)"
ValidateOnly = "$(_XFXamlCValidateOnly)"
KeepXamlResources = "$(XFKeepXamlResources)" />
I uninstalled Xamarin.Forms 4.5.0.396 and reinstalled version 5.0.0.2401: The reset failed. However, the update from 4.5.0.396 to 5.0.0.2401 worked. But running in debug mode my application always gives me the same error and an additional error: The "XamlCTask" task was declared or used incorrectly, or failed during construction. Check the spelling of the task name and assembly. MemoCourses.AndroidC:\Users\marce.nuget\packages\xamarin.forms\4.5.0.396\build\Xamarin.Forms.targets114
The problem is probably due to the new version which changes the name of the folders, but how to solve it? Thank you for helping me.
Solution 1:[1]
Editing the project (several hundred pages long) never worked. I found at https://my.visualstudio.com/ the way to download Visual Studio 16.11.13 and installed it. I opened my project and had no mistakes. Execution in debug mode was successful from the first time. There is therefore an incompatibility between version 16.11 and newer versions, such as preview, 17.1 and later.
Conclusion: The only way to resolve this issue is to reinstall the same version that was used to develop the solution.
Solution 2:[2]
Normally, the issue caused by the Xamarin.Forms version is not compatible/matching with your NuGet package which you have installed.
Right click the project solution to open the NuGet Package Manager to install the package and the matching Xamarin.Forms version for the Forms project and android platform.
Solution 3:[3]
Having too many issues with Visual Studio 2019, I uninstalled it and reinstalled Visual Studio 2022. But the following line of code
Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
gives me the error: The type or namespace name 'Bitmap' could not be found (are you missing a using directive or an assembly reference?). If I click on 'Add Reference to Mono.Android.dll', a message appears at the top of the title bar: 'CSharpAddImportCodeFixProvider' encountered an error and was disabled.'
The detail: System.ArgumentException : Unable to find an appropriate reference type for this file: "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v9.0\Mono.Android.dll". at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAVSReferences.Add(String bstrPath) at Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.VisualStudioWorkspaceImpl.ApplyMetadataReferenceAdded(ProjectId projectId,MetadataReference metadataReference) at Microsoft.CodeAnalysis.Workspace.ApplyProjectChanges(ProjectChanges projectChanges) at Microsoft.CodeAnalysis.Workspace.TryApplyChanges(Solution newSolution,IProgressTracker progressTracker) at Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.VisualStudioWorkspaceImpl.TryApplyChanges(Solution newSolution,IProgressTracker progressTracker) at Microsoft.CodeAnalysis.CodeActions.ApplyChangesOperation.TryApplyAsync(Workspace workspace,IProgressTracker progressTracker,CancellationToken cancellationToken) at async Microsoft.CodeAnalysis.Editor.Implementation.CodeActions.CodeActionEditHandlerService.ProcessOperationsAsync() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Editor.Implementation.CodeActions.CodeActionEditHandlerService.ApplyAsync() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.InvokeWorkerAsync() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformActionAsync().
However, if I create a new program with the Bitmap statement, adding Reference works. So I decided to start from an empty project, copy all the pages of my project and modify the existing pages (it's a huge job!). I very much regret that Microsoft did not ensure compatibility in this situation. Thank you anyway to all those who made suggestions to me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Wendy Zang - MSFT |
Solution 3 | Marcel Delhaye |