0

I need create a nuget, it need for winui3 and wpf, because loading mainthread in winui3 is different with wpf see How to get DispatcherQueue in WinUI 3 Desktop using Windows App SDK, so i need distinguish they. But i see Windows.AppSdk just for net5-windows, don't distingush, how to distinguish they when i pack they to one nuget?

yangzhou
  • 58
  • 1
  • 6

1 Answers1

0

Bring in both the WPF and the WinUI assemblies in the shared project and check if System.Windows.Threading.Dispatcher.CurrentDispatcher is initialized in your code:

Shared.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>Shared</RootNamespace>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <UseWinUI>true</UseWinUI>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.ProjectReunion" Version="0.8.4" />
      <PackageReference Include="Microsoft.ProjectReunion.Foundation" Version="0.8.4" />
      <PackageReference Include="Microsoft.ProjectReunion.WinUI" Version="0.8.4" />
  </ItemGroup>
</Project>

public static class Helper
{
    public static void Dispatch(Action action)
    {
        if (Dispatcher.CurrentDispatcher != null)
        {
            // WPF
            Dispatcher.CurrentDispatcher.Invoke(action);
        }
        else
        {
            // WinUI
            Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread()?
                .TryEnqueue(new Microsoft.UI.Dispatching.DispatcherQueueHandler(action));
        }
    }
}

WPF App:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Shared\Shared.csproj" />
  </ItemGroup>

</Project>

WinUI App:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>WinUIApp</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <UseWinUI>true</UseWinUI>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ProjectReunion" Version="0.8.4" />
    <PackageReference Include="Microsoft.ProjectReunion.Foundation" Version="0.8.4" />
    <PackageReference Include="Microsoft.ProjectReunion.WinUI" Version="0.8.4" />
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Shared\Shared.csproj" />
  </ItemGroup>
</Project>

Shared.Helper.Dispatch(() => { /* ... */ });
mm8
  • 163,881
  • 10
  • 57
  • 88