简体   繁体   English

参考Windows 仅项目来自.NET MAUI

[英]Reference Windows only project from NET MAUI

I have been working on tool that is done using.NET MAUI.我一直在研究使用 .NET MAUI 完成的工具。 Now I would like to automate Windows UI, but I can't figure out how to do that.现在我想自动化 Windows UI,但我不知道该怎么做。 I have tried to download FlaUI NuGet, but can't get it working as it requires project to be Net7.0-windows .我尝试下载 FlaUI NuGet,但无法正常工作,因为它要求项目为Net7.0-windows So I have created new project with reference to Net7.0-windows, but then my references in Net Maui get corrupted.所以我创建了一个参考 Net7.0-windows 的新项目,但是我在 Net Maui 中的参考被破坏了。

MauiApp1.csproj (project): MauiApp1.csproj (项目):

<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):项目ClassLibrary1.csproj (项目):

<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:目前如果:

在此处输入图像描述

I get following errors:我收到以下错误:

在此处输入图像描述

My question is How to reference ClassLibrary1.csproj in MauiApp1.csproj in.NET MAUI?我的问题是 How to reference ClassLibrary1.csproj in MauiApp1.csproj in.NET MAUI? Is there some way to get this setup working?有什么方法可以使此设置正常工作吗?

If your ClassLibrary1 project going to support all platforms then you should add this lines to your ClassLibrary1.cspoj file too.如果您的ClassLibrary1项目要支持所有平台,那么您也应该将此行添加到您的ClassLibrary1.cspoj文件中。

<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;如果您的ClassLibrary1项目仅支持 windows,那么您可以做两件事;

  1. If your MAUI project just going to run on windows only, on your MauiApp1.csproj file you should delete these lines;如果您的 MAUI 项目仅在 windows 上运行,则应在您的MauiApp1.csproj文件中删除这些行;

     <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.如果您的 MAUI 应用程序要同时支持其他平台,您应该单独添加平台特定的库。 I am using this for my multiplatform MAUI apps.我将其用于我的多平台 MAUI 应用程序。

    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.然后打开您的MauiApp1.csproj文件,找到您的参考并向您的参考添加条件。 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 .我创建了一个名为 MauiApp1 的 MAUI 项目和一个名为MauiApp1MauiLib1库。 MauiLib1 only supports Windows platform and MauiApp1 supports all platforms. MauiLib1只支持Windows平台, MauiApp1支持所有平台。 I've added MauiLib1 to MauiApp1 but for only Windows platform.我已将MauiLib1添加到MauiApp1 ,但仅适用于 Windows 平台。 Required .csproj files and platform specific codes can be seen below.所需的.csproj文件和特定于平台的代码可以在下面看到。

  • 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. MauiLib1项目中的一个简单的 class。 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.稍微更改了MauiApp1项目的默认MainPage.xaml.cs代码。 Here you can see Windows specific codes.在这里你可以看到Windows的具体代码。 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.单击 3 次后,您将在按钮中看到我们的字符串,这是解决方案资源管理器的屏幕截图。 As you can see reference added for windows only.如您所见,仅为 windows 添加了参考。

    描述 描述

Note 1注1

Don't forget to change your current platform on the Visual Studio.不要忘记在 Visual Studio 上更改当前平台。 It's very important.这很重要。 Otherwise, your platform specific codes will not be compiled.否则,您的平台特定代码将不会被编译。 描述

Note 2笔记2

I deleted the parts unrelated to the question from the code.我从代码中删除了与问题无关的部分。 So everyone can see clearly what I've changed.这样每个人都可以清楚地看到我改变了什么。

Note 3注3

If your library supports more than one platform you can add more conditions to your reference in the .csproj file.如果您的库支持多个平台,您可以在.csproj文件中为您的参考添加更多条件。

I know little about FlaUI, but removing this line in MauiApp1.csproj might work:我对 FlaUI 知之甚少,但在MauiApp1.csproj中删除这一行可能会起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM