1

I have developed utility source code to work with WebView2 component, and I want to use it in 2 different projects, one is WPF, the other WinForms.

The problem is that WebView2 asks for different "using", one for each project.

In the WPF class, I have to set the "using" this way:

using Microsoft.Web.WebView2.Wpf;   // NOTE!!!

namespace Utility
{
    public static class WebView2_Helper
    {
    ...
    (code here, using the WebView2 component)
    ...
    }
}

And for the WinForms, I have to create another file and duplicate all the code, just to change the "using"...:

using Microsoft.Web.WebView2.WinForms;   // NOTE!!!

namespace Utility
{
    public static class WebView2_Helper
    {
    ...
    (code here... same as above!)
    ...
    }
}

The code after the "using" is the same, in both cases.

In C/C++, a solution could be saving the common code to a file, and then include it inside both classes.

What should be the solution in C#?

4
  • 1
    It may be as simple as conditional compilation. Just as you would do in C/C++. Commented Dec 4, 2024 at 14:52
  • @Clemens Do you know if there are predefined symbols to distinguish between WPF and WinForms projects? A quick search doesn't solve to me... Or may I have to define them? Anyway, it's a good solution. If you place it as answer, I will mark it as such. Commented Dec 4, 2024 at 15:05
  • No, afaik. You have to define one yourself in the project file. Commented Dec 4, 2024 at 15:15
  • My $.02 is that this is a good candidate for abstraction and dependency injection. You have two libraries - one for Forms and one for WPF .You define a common interface or base class that encapsulates all the browser functionality you will be using, and a means for providing (injecting) an instance of that object to your core code. This is, indeed, exactly what WebView2 is relative to the native Win32 implementation, so you'd just be adding another layer of indirection - which as we all know, solves all problems. Best of all, later moving to another browser like CEF would then be trivial. Commented Dec 6, 2024 at 19:01

1 Answer 1

2

If your utility is its own library you can define two different outputs, one for usage with WinForms and one for WPF.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)'=='WPF'">
    <DefineConstants>WPF</DefineConstants>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)'=='WINFORMS'">
    <DefineConstants>WINFORMS</DefineConstants>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

Then in your code:

namespace Utility
{
    public static class WebView2_Helper
    {
#if WPF
        using Microsoft.Web.WebView2.Wpf;
#elif WINFORMS
        using Microsoft.Web.WebView2.WinForms;
#endif

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.