3

I have a Xamarin forms project with Uwp, and I'd like to know how to upgrade the C# version from 7.3 to 8.0+.

I followed these links:

How to use the latest C# version in Xamarin Forms projects

https://montemagno.com/use-csharp-8-everywehre/

which suggest adding the following to my csproj file:

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <ProduceReferenceAssembly>true</ProduceReferenceAssembly>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

or

  <PropertyGroup>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

I get the following: Android: I get v8.0+ out of the box with no mod to csproj iOS: Adding the LangVersion as above to csproj successfully upgrades the version to later than 7.3 UWP: Adding the LangVersion has no effect.

How can I upgrade C# for UWP?

UPDATE

After adding:

<TargetFramework>netstandard2.0</TargetFramework>

OR

<TargetFramework>netstandard2.1</TargetFramework>

all my references get broken:

enter image description here

jho
  • 199
  • 1
  • 8
  • Hi Steve, according to what I've read, e.g. the links I've pasted above, that's not the way it works. I can't even find project properties for the cross-platform code. The platform specific code though has pages and pages of settings, though none that I can see to change the .NET or C# versions. If you are able to point me more specifically to which setting to change it would be much appreciated. – jho Jul 16 '22 at 20:28
  • 1
    Just checked my project files: only the platform specific projects have csprojs. The X-platform code has shproj & projitems files. Also a text search can only locate one file, an Android one with TargetFramework, with a value of 12 (Android version). My app is about 10 to 20% platform specific. Lots of renderers. – jho Jul 16 '22 at 21:15
  • *"The X-platform code has shproj & projitems files."* - ah ha! That explains it. **Older project format** where cross-platform code was "shared" - compiled directly into - the platform-specific projects. **Deleted my previous comments: they do not apply.** (Since ~2017, a separately-compiled cross-platform project, targetting ".Net Standard" has been the recommended architecture. But that requires moving platform-specific code into the individual projects - `#if __Android__` etc is no longer possible.) **I don't know** anything about using c# 8.0 in your situation. – ToolmakerSteve Jul 16 '22 at 21:53
  • Is there anyway to port the X platform code to a separately compiled project. I guess even if there was, I would still have to upgrade the uwp project, so the problem would still only be half solved. – jho Jul 17 '22 at 05:40
  • *"any way to port the X platform code to a separately compiled project. "* Yes. You create a new XForms project - that will have the desired arrangement of projects. Then move all your code over to the new projects. But as mentioned, you'll have to move `#if __Android__` etc logic to the appropriate projects. That likely will require using "DependencyServices" to access from main project. Re *"still have to upgrade the uwp project"*. Why? You don't need c# 8 features in your platform-specific renderers. – ToolmakerSteve Jul 17 '22 at 18:36
  • It's the convenience of C# 8 coding short cuts that I'd like if possible, eg `list[^1]` – jho Jul 18 '22 at 16:42
  • Did you try to set the framework as .NetStandard 2.1 like the second answer in the first link you provided? – Liyun Zhang - MSFT Jul 20 '22 at 09:51
  • Hi Liyun, please see my **UPDATE** above. – jho Jul 20 '22 at 11:09
  • It seems the packages you used don't support the .NetStandard 2.1. But I can't find other ways to change the C# version. – Liyun Zhang - MSFT Jul 21 '22 at 09:08
  • You can try to update the packages in your project to the last version after updating the framework. – Liyun Zhang - MSFT Jul 21 '22 at 09:11
  • Unfortunately once I add that code to my project, VS ceases to recognise it as a valid project. There is no option to check its references against Nuget, or even compile it. It appears that the code is completely incompatible with a UWP project. – jho Jul 21 '22 at 15:39
  • But it seems the only ways to change the C# version in the xamarin. – Liyun Zhang - MSFT Jul 25 '22 at 08:32
  • According to the official documentation, yes, it seems that is the proper way to do it. It's just that it's not working, for me at least. – jho Jul 25 '22 at 15:18
  • You can try to report it on the github. – Liyun Zhang - MSFT Jul 27 '22 at 07:16
  • Noted, will try and find the time to do that. Thanks – jho Jul 27 '22 at 19:48

0 Answers0