c# - 引用 NET MAUI 中的仅限 Windows 的项目

标签 c# xaml maui flaui

我一直在开发使用 NET MAUI 完成的工具。现在我想自动化 Windows UI,但我不知道如何做到这一点。我尝试下载 FlaUI NuGet,但无法使其工作,因为它要求项目为 Net7.0-windows。因此,我引用 Net7.0-windows 创建了新项目,但随后我在 Net Maui 中的引用被损坏了。

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>

项目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>

目前如果:

enter image description here

我收到以下错误:

enter image description here

我的问题是如何在NET MAUI中引用MauiApp1.csproj中的ClassLibrary1.csproj?有什么方法可以让这个设置工作吗?

最佳答案

如果您的 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>

如果您的 ClassLibrary1 项目仅支持 Windows,那么您可以做两件事;

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

    解决方案资源管理器 > 您的 MAUI 项目 > 依赖项 > 右键单击​​您的平台 > 添加引用

    然后打开您的 MauiApp1.csproj 文件,找到您的引用并向您的引用添加条件。您的引用资料最后应该是这样的;

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

示例

我举了一个例子。我创建了一个名为 MauiApp1 的 MAUI 项目和一个名为 MauiLib1 的类库。 MauiLib1仅支持Windows平台,MauiApp1支持所有平台。我已将 MauiLib1 添加到 MauiApp1 但仅适用于 Windows 平台。所需的 .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>
    
  • MauiLib1 项目中的一个简单类。这样我们就可以进行测试了。

     public static class MauiLib1TestClass
     {
         public static string testString = "ehehehe";
     }
    
  • 稍微更改了 MauiApp1 项目的默认 MainPage.xaml.cs 代码。在这里您可以看到 Windows 特定的代码。如果没有这些条件,您的应用程序将会崩溃。因为不会有该平台的引用。

     #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);
         }
     }
    
  • 点击 3 次后,您将在按钮中看到我们的字符串,这是解决方案资源管理器的屏幕截图。正如您所看到的,仅针对 Windows 添加了引用。

    Desc Desc

注1

不要忘记更改 Visual Studio 上当前的平台。这很重要。否则,您的平台特定代码将不会被编译。 Desc

注2

我从代码中删除了与问题无关的部分。这样大家都能清楚地看到我改变了什么。

注3

如果您的库支持多个平台,您可以在 .csproj 文件中的引用中添加更多条件。

关于c# - 引用 NET MAUI 中的仅限 Windows 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75264103/

相关文章:

c# - .NET MAUI 将列表绑定(bind)到上下文 View

.net - .MAUI : How to add context menu into control by using community-toolkit-markup or C#

c# - WCF 中的 CustomBinding 中的客户端和服务绑定(bind)可能不匹配

c# - 如何测试与 ModelState 一起使用的 ActionFilterAttribute?

c# - 如何正确处理元素

c# - 不要重复自己 - XAML 和 WPF

c# - .Net MAUI : How to select single and multiple items using MVVM and CollectionView

c# - 尝试将焦点更改按钮从选项卡更改为在 C# Windows 窗体中输入

c# - WPF 控件的条件加载

c# - UserControl 中的依赖属性绑定(bind)