2

I have been working on tool that is done using NET MAUI. Now I would like to automate Windows UI, but I can't figure out how to do that. I have tried to download FlaUI NuGet, but can't get it working as it requires project to be Net7.0-windows. So I have created new project with reference to Net7.0-windows, but then my references in Net Maui get corrupted.

MauiApp1.csproj (project):

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

    <PropertyGroup>
        <TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
        <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
        <!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
        <OutputType>Exe</OutputType>
        <RootNamespace>MauiApp1</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>

        <!-- Display name -->
        <ApplicationTitle>MauiApp1</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.mauiapp1</ApplicationId>
        <ApplicationIdGuid>67771146-3ce8-452f-860b-3669d8a9e4a0</ApplicationIdGuid>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
    </ItemGroup>

</Project>

Project ClassLibrary1.csproj (project):

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

  <PropertyGroup>
    <TargetFramework>net7.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FlaUI.Core" Version="4.0.0" />
    <PackageReference Include="FlaUI.UIA3" Version="4.0.0" />
  </ItemGroup>

</Project>

Currently if:

enter image description here

I get following errors:

enter image description here

My question is How to reference ClassLibrary1.csproj in MauiApp1.csproj in NET MAUI? Is there some way to get this setup working?

10101
  • 2,232
  • 3
  • 26
  • 66
  • *"but then my references in Net Maui get corrupted"* What does that mean? If you attempted to reference Project Y, please show the .csproj lines for that reference, and show the complete error message you got. – ToolmakerSteve Jan 28 '23 at 00:05
  • @ToolmakerSteve I have edited my question. Basically my target is to get FlaUI somehow working using NET MAUI – 10101 Jan 28 '23 at 09:08
  • Puzzling. MauiApp1.csproj doesn't mention ClassLibrary1. Yet your screenshot shows it as a Reference. I don't know how that is possible, without it being in MauiApp1.csproj. – ToolmakerSteve Jan 28 '23 at 20:04

2 Answers2

3

If your ClassLibrary1 project going to support all platforms then you should add this lines to your ClassLibrary1.cspoj file too.

<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>

If your ClassLibrary1 project going to support just windows then you can do two things;

  1. If your MAUI project just going to run on windows only, on your MauiApp1.csproj file you should delete these lines;

    <TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
    
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    
  2. If your MAUI App going to support other platforms at the same time, you should add your platform specific libraries separate. I am using this for my multiplatform MAUI apps.

    Solution Explorer > Your MAUI Project > Dependencies > Right Click Your Platform > Add reference

    then open your MauiApp1.csproj file, find your reference and add an condition to your reference. Your reference should be like this at the end;

    <ItemGroup Condition="$(TargetFramework.Contains('-windows')) != false ">
      <ProjectReference Include="..\MauiLib1\MauiLib1.csproj" />
    </ItemGroup>
    

EXAMPLE

I've made an example. I created a MAUI project named MauiApp1 and a class library named MauiLib1. MauiLib1 only supports Windows platform and MauiApp1 supports all platforms. I've added MauiLib1 to MauiApp1 but for only Windows platform. Required .csproj files and platform specific codes can be seen below.

  • MauiApp1.csproj

     <PropertyGroup>
         <TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
         <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
    
         <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
         <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
         <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
         <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
         <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
         <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
     </PropertyGroup>
    
     <ItemGroup Condition="$(TargetFramework.Contains('-windows')) != false ">
       <ProjectReference Include="..\MauiLib1\MauiLib1.csproj" />
     </ItemGroup>
    
  • MauiLib1.csproj

     <PropertyGroup>
         <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
    
         <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
         <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
     </PropertyGroup>
    
  • A simple class in MauiLib1 project. So we can make a test.

     public static class MauiLib1TestClass
     {
         public static string testString = "ehehehe";
     }
    
  • Changed MauiApp1 project's default MainPage.xaml.cs code a little bit. Here you can see Windows specific codes. Without these conditions your app will crash. Because there will not be a reference for that platform.

     #if WINDOWS
     using MauiLib1;
     #endif
    
     public partial class MainPage : ContentPage
     {
         int count = 0;
    
         public MainPage()
         {
             InitializeComponent();
         }
    
         private void OnCounterClicked(object sender, EventArgs e)
         {
             count++;
    
             if (count == 1)
                 CounterBtn.Text = $"Clicked {count} time";
     #if WINDOWS
             else if (count == 3)
                 CounterBtn.Text = MauiLib1TestClass.testString;
     #endif
             else
                 CounterBtn.Text = $"Clicked {count} times";
    
             SemanticScreenReader.Announce(CounterBtn.Text);
         }
     }
    
  • After 3 clicks, you'll see our string in the button and here it's solution explorer's screenshot. As you can see reference added for windows only.

    Desc Desc

Note 1

Don't forget to change your current platform on the Visual Studio. It's very important. Otherwise, your platform specific codes will not be compiled. Desc

Note 2

I deleted the parts unrelated to the question from the code. So everyone can see clearly what I've changed.

Note 3

If your library supports more than one platform you can add more conditions to your reference in the .csproj file.

Schia
  • 232
  • 1
  • 11
  • For (2), can you show the lines that get added to .csproj? – ToolmakerSteve Jan 28 '23 at 19:53
  • 1
    @ToolmakerSteve Thanks for your comment. When I opened one of my projects, I noticed there are mistakes in my answer! I've corrected my answer and added an example. – Schia Jan 28 '23 at 22:53
  • Thank you for answer! Can you try to get it working with FlaUI installed in `MauiLib1.csproj`? For me it keeps saying `MauiLib1.csproj` should be `net7.0-windows` and not `net7.0-windows10.0.19041.0` – 10101 Jan 29 '23 at 17:45
  • If [this](https://www.nuget.org/packages/FlaUI.Core#supportedframeworks-body-tab) page not wrong FlaUI is supported on only Windows 7 right now. MAUI apps supported Windows 10 1809 or higher versions. You can check it [here](https://learn.microsoft.com/en-us/dotnet/maui/supported-platforms?view=net-maui-7.0). As far as I can see, these libraries are not compatible with each other. Somehow If you can run FlaUI on Windows 10, can you provide more details about it? – Schia Jan 29 '23 at 18:06
  • 1
    I'm not sure about it but if you look the FlaUI's nuget page, I guess they're planning support other platforms too. It's just not supported right now. – Schia Jan 29 '23 at 18:06
1

I know little about FlaUI, but removing this line in MauiApp1.csproj might work:

<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>

yueyinqiu
  • 369
  • 1
  • 7
  • What if I want to automate Mac and Windows UIs utilising different projects and libraries? – 10101 Jan 28 '23 at 11:46
  • I know little about 'automate'. Will your code be completely different since you're using different 'automate' libraries? If yes, I might just create 3 different maui projects and share my common business code with another class library project. – yueyinqiu Jan 28 '23 at 12:07