Home > Net >  How can I support different TFM in C#?
How can I support different TFM in C#?

Time:09-21

I want to send a local toast notification from WPF program. Now I am using the latest .net 6.

And here is the tutorial of Microsoft:https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=uwp

In the tutorial it said:

.NET apps must use one of the Windows TFMs, otherwise the toast sending and management APIs like Show() will be missing. Set your TFM to net6.0-windows10.0.17763.0 or later.

In windows10.0.17763.0 or later, I want to use the toast notification.

In windows10.0.17763.0 earlier or even windows 7, I will make a notification control by myself.

However, after I set the TFM to net6.0-windows10.0.17763.0. My program will not support windows10.0.17763.0 earlier or even windows 7 any more.

Is there any way I can support the two platforms both? Thank you.

CodePudding user response:

If you want to use the ToastContentBuilder.Show API in our app but still support Windows 7 using an alternative API, you could specify two TFMs in your project file:

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFrameworks>net6.0-windows7;net6.0-windows10.0.17763.0</TargetFrameworks>
  <Nullable>enable</Nullable>
  <UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
</ItemGroup>

You could then use a preprocessor directive to conditionally compile your code based on the TFM:

#if WINDOWS10_0_17763_0_OR_GREATER
    new ToastContentBuilder()....Show();
#else
    //custom toast...
#endif
  • Related