Unable To Publish MAUI App for Mac OS from Windows - c#

I have Visual Studio Community (Version 17.4.3) in my PC.
I created a MAUI app and now need to publish it as a 'pkg' or 'app'
to a Mac device from Windows.
I have MacOS 12 running in VMWare Workstation to test the app.
I've not installed Visual Studio For Mac in VMWare because I'm not planning to develop in Mac.
My intension is to develop and build on Windows as self-contained & then transfer pkg file to Mac for just running and testing
The issue is I was unable to publish for Mac from Windows.
What am I doing wrong?
This is my simple app (It's just a test app)
This is the command I'm using to try publishing for Mac but it gives me errors.
dotnet publish -f net7.0-maccatalyst -r osx-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true
When I try to list all workloads installed, This is what I can see
But when I checked Individual Components in Visual Studio Installer, I'm assuming SDKs are installed
This is my csproj file
<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>MacTest</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Display name -->
<ApplicationTitle>MacTest</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>com.companyname.mactest</ApplicationId>
<ApplicationIdGuid>13c5b601-4ac2-49be-a1e0-86c80ce0f8b0</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>
<GenerateAppInstallerFile>False</GenerateAppInstallerFile>
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
<AppxSymbolPackageEnabled>False</AppxSymbolPackageEnabled>
<GenerateTestArtifacts>True</GenerateTestArtifacts>
<HoursBetweenUpdateChecks>0</HoursBetweenUpdateChecks>
</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>

.NET MAUI apps that target Mac Catalyst can only be launched and debugged using Visual Studio 2022 for Mac. Please see Build your first app. In addition, archiving and publishing a .NET MAUI macOS should be through the command-line, remote building/archiving/publishing from Windows are nor supported, please see: Publish a .NET MAUI app for macOS and
.NET MAUI macOS target publishing/archiving. #5399

Related

Project gets unloaded when opening with Visual Studio 2019

I am trying to open a solution using Visual studio 2019 and visual studio 2017. All the projects in the solution are loading except for one. When trying to load the unloaded project i get an error in the output window as
TakstMVC.csproj : error : The imported project
"....build\MSBuild.Community.Tasks.targets" was not found. Confirm
that the expression in the Import declaration
"TakstMVC\.....build\MSBuild.Community.Tasks.targets" is correct,
and that the file exists on disk. TakstMVC\FluentMigrator.targets
When i tried to open using VS 2017 i saw a migration report which said
TakstMVC.csproj: The application which this project type is based on
was not found. Please try this link for further information:
http://go.microsoft.com/fwlink/?LinkID=299083&projecttype=E3E379DF-F4C6-4180-9B81-6769533ABE47
Part of the .csproj of the project is as below:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A4354F5C-ECF5-4621-AA9E-B91FE543F096}</ProjectGuid>
<ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TakstMVC</RootNamespace>
<AssemblyName>TakstMVC</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>4.0</OldToolsVersion>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<MvcProjectUpgradeChecked>true</MvcProjectUpgradeChecked>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
I have the following installed in my computer
Microsoft .NET Framework 4.7.1 SDK
Microsoft .NET Framework 4.7.1 SDK Targeting Pack (ENU)
Microsoft .NET Core 3.1.1 - Windowsserver hosting
Microsoft .NET Core SDK 3.1.101 (x64)
Microsoft .NET Core SDK 2.2.207 (x64)
Microsoft .NET Core Runtime - 3.1.1
Microsoft .NET Core Runtime - 2.2.8
I tried to install dotnetfx35.exe but it doesnt even run when executed (not even a message or error).
The windows feature are as below:
How can I identify the target framework of the project and load it in visual studio successfully ? appreciate some advise on this.
The error is clear that you did not import the MSBuild.Community.Tasks.targets correctly on your local area. The reason is that you did not install MSBuild.Community.Tasks.targets on your PC or the import path from csproj file is incorrect.
You should check this document to install the right target.
First, remove xml node under csproj file like these:
<Import Project="..\build\MSBuild.Community.Tasks.targets" Condition="Exists('..\build\MSBuild.Community.Tasks.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\build\MSBuild.Community.Tasks.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\build\MSBuild.Community.Tasks.targets'))" />
</Target>
Second, install the msi file.
then, add these under the csproj file:
<Import Project="C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
Besides, you could also use nuget function. First, uninstall the nuget package if you have a old version on the project. Then, use the first suggestion to remove any previous import projects="xxx\MSBuildTasks.targets". After that, install the MSBuildTasks nuget package. That is the same.
Update
Try to You should change ..\..\ to ..\. Use the right path.
Or use this command under Tools-->Nuget Package Manager-->Package Manager Console
update-package -reinstall
Answer from the Customer
embarrassingly enough the whole issue was with the solution path where my solution was located in my local computer. I had a long path including a folder having two words as well. After trying everything else I moved the solution folder to C:\temp folder and the issue was no longer there. The error showed in VS was quite misleading. What a waist of time. Thank you for your efforts !

DotNetCore3.0 WPF - error - Project file is incomplete. Expected imports are missing

I've created a WPF application using VisualStudio 2017 by selecting the .Net framework 4.6. Know I'm trying to configure it for .Net Core 3.0 for WPF. But unfortunately, after changing my configuration, When reloading my projects, I got this error.
Project file is incomplete. Expected imports are missing.
I've checked all the reference files are there.
Here is my .csproj file.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>HelloWorld</AssemblyName>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<ApplicationDefinition Include="..\WpfApp\App.xaml" Link="App.xaml" />
<Compile Include="..\WpfApp\App.xaml.cs" Link="App.xaml.cs" />
</ItemGroup>
<ItemGroup>
<Page Include="..\WpfApp\MainWindow.xaml" Link="MainWindow.xaml" />
<Compile Include="..\WpfApp\MainWindow.xaml.cs" Link="MainWindow.xaml.cs" />
</ItemGroup>
</Project>
I've installed this SDK version 3.0.100-preview5-011568. I've also enabled the Use previews of .Net Core SDK in VisualStudio.
You should upgrade to Visual Studio 2019. It includes a project template for WPF and Windows Forms applications targeting .NET Core 3.
From the docs:
Visual Studio 2017 doesn't support .NET Core 3.0 projects.

Can't use Microsoft.Build.Evaluation in VS2017

I built a Web Forms website using VS2015 where I user Microsoft.Build.Evaluation
so that I can grammatically go through the files in my project.
When using VS2017 I get this error:
Microsoft.Build.Exceptions.InvalidProjectFileException: 'The imported
project "C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
C:\Users\MyUser\Source\Workspaces\MyProject
TFVC2\Gemstar\MyProject.csproj'
Here is my code:
using Microsoft.Build.Evaluation;
Project project = new Project();
if (ProjectCollection.GlobalProjectCollection.GetLoadedProjects(mPath + "MyProject.csproj").Count == 0)
{
project = new Project(mPath + "MyProject.csproj");
}
else
{
project = ProjectCollection.GlobalProjectCollection.GetLoadedProjects(mPath + "MyProject.csproj").FirstOrDefault();
}
I want to mention that I installed BuildTools for VS2017 from https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15
Mac OS -
I resolved it by installing the latest version of Mono.
Installing Mono on macOS.
Search "omnisharp" in settings (command + ',').
Changed the setting Omnisharp: Use Global Mono to always.
Reload the vscode.
Can't use Microsoft.Build.Evaluation in VS2017
It seems the old value of MSBuildExtensionsPath32 set in the Microsoft.Build.Evaluation.
According to the error info:
The imported project "C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets"
was not found.
The import path for Microsoft.WebApplication.targets is not correct.
In the project file .csproj, we notice following import:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
So the value for MSBuildExtensionsPath32 is C:\Program Files (x86)\MSBuild in the Microsoft.Build.Evaluation, which is not correct for Visual Studio 2017.
The value of MSBuildExtensionsPath32 for Visual Studio 2017 should be:
BuildTool Installed:
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0
Visual Studio Installed:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v15.0
To resolve this issue, you could verride the value in your project file:
<PropertyGroup>
<MSBuildExtensionsPath32>C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0</MSBuildExtensionsPath32>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
See the similar issue for some more details.
Hope this helps.
Install Wix Toolset Visual Studio 2017 Extension.

Windows Store App DLL 32bit and 64bit

I've written a small Media Foundation Transform and added the C++ DLL to my C# Windows Store App project.
The 32bit version of the DLL running the x86 configuration works just fine but x64 doesn't work (it throws an Exception with the following message:
"MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED : HRESULT - 0x800700C1")
If I add the 64bit version it's the same just the other way around x64 works and x86 doesn't.
Is there any way I can set it up so that it uses the x86 version of the DLL for the x86 configuration and the x64 version for the x64 configuration?
Took me some time but I figured out how to do it using NuGet.
First I added a location on my PC as package source as explained here.
In VS2013 CE you do this by opening the NuGet Package Manager Settings (Tools > NuGet Package Manager > Package Manager Settings) and under Package Sources you add a location on your computer.
To create the NuGet I combined several HowTo's since one alone didn't work.
The main one was this.
I built the dlls for my required platforms and created the following folder structure
Since it might be a little hard to see ProjectName.props and ProjectName.targets are located in the netcore451 folder.
The .dll, .pri and .winmd in lib are the x86 version. According to the HowTo it's redundant and you can just ignore these files but without them VS might not work correctly in design mode.
In the folder that contains build and lib I created a file ProjectName.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd">
<metadata minClientVersion="2.5">
<id>ProjectName</id>
<version>1.0.0</version>
<authors>Stefan Fabian</authors>
<owners>Stefan Fabian</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Description</description>
<releaseNotes>First release.</releaseNotes>
<copyright>Copyright 2015</copyright>
<references>
<group targetFramework=".NETCore4.5.1">
<reference file="ProjectName.winmd" />
</group>
</references>
</metadata>
</package>
ProjectName.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
ProjectName.targets
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="PlatformCheck" BeforeTargets="InjectReference"
Condition=" ( ('$(Platform)' != 'x86') AND ('$(Platform)' != 'AMD64') AND ('$(Platform)' != 'Win32') AND ('$(Platform)' != 'ARM') AND ('$(Platform)' != 'x64') )">
<Error Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)'
platform. You need to specify platform (x86 / x64 or ARM)." />
</Target>
<Target Name="InjectReference" BeforeTargets="ResolveAssemblyReferences">
<ItemGroup Condition="'$(Platform)' == 'x86' or '$(Platform)' == 'Win32'">
<Reference Include="ProjectName">
<HintPath>$(MSBuildThisFileDirectory)x86\ProjectName.winmd</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="'$(Platform)' == 'x64' or '$(Platform)' == 'AMD64'">
<Reference Include="ProjectName">
<HintPath>$(MSBuildThisFileDirectory)x64\ProjectName.winmd</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="'$(Platform)' == 'ARM'">
<Reference Include="ProjectName">
<HintPath>$(MSBuildThisFileDirectory)ARM\ProjectName.winmd</HintPath>
</Reference>
</ItemGroup>
</Target>
</Project>
I'm not sure if it's necessary to check for x86 and Win32 but it works for me.
Disclaimer
This was the first time ever I created a NuGet package and the code above might suck.

cmake build x64 using whichever visual studio version is installed

I have a C# project with two platforms: x86 and x64. This C# project depends on a native dll that must also be built as x86 or x64.
So far, I have successfully added cmake as a pre-build event to the .csproj of the C# project:
<Target Name="BeforeBuild">
<Message Text="Building native library" />
<Exec Command="cmake -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
This builds and copies the native dll to the output directory of the project, matching the selected configuration (e.g. bin/x86/Debug or bin/x64/Release).
If I use the x86 configuration in Visual Studio, then everything is fine. However, if I use the x64 configuration I get a failure, because the native library is still built as x86. In other words, I need to find a way to inform cmake to build a x64 binary when $(Platform) is 'x64'.
Is there a commandline switch to instruct cmake to built a x64 binary? I tried the -G and -T options, but they don't appear to support this (or I wasn't able to find the correct way to use them).
Ideas welcome!
Edit: I've managed to get a little bit closer by passing the $(VisualStudioVersion) property to cmake.
<PropertyGroup>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'x86'">-G"Visual Studio $(VisualStudioVersion)"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'AnyCPU'">-G"Visual Studio $(VisualStudioVersion) Win64"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' != 'Windows_NT'"></CMakeGenerator>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Building native library" />
<Exec Command="cmake $(CMakeGenerator) -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
Unfortunately, $(VisualStudioVersion) returns a decimal number (e.g. 12.0 for VS2013), whereas cmake expects an integer (e.g. 12 for VS2013). If I can convert $(VisualStudioVersion) to an integer, then this should work! Can this be done?
Edit 2: solved!
MSBuild 4.0/4.5 adds property functions that can be used to modify properties. In this case, the following works perfectly:
<PropertyGroup>
<CMakeVSVersion>$(VisualStudioVersion.Substring(0, $(VisualStudioVersion).IndexOf(".")))</CMakeVSVersion>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'x86'">-G"Visual Studio $(CMakeVSVersion)"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' == 'Windows_NT' and '$(Platform)' == 'AnyCPU'">-G"Visual Studio $(CMakeVSVersion) Win64"</CMakeGenerator>
<CMakeGenerator Condition="'$(OS)' != 'Windows_NT'"></CMakeGenerator>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Building native library" />
<Exec Command="cmake $(CMakeGenerator) -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
Hopefully someone might find this useful in the future!
Unfortunately, the Visual Studio generators require the Visual Studio version to be explicitly specified for x64 builds. However, you should be able to make this work with the NMake generator:
<Target Name="BeforeBuild">
<Message Text="Build native library" />
<Exec Command="cmake -G"NMake Makefiles" -DINSTALL_DIR=../bin/$(Platform)/$(Configuration) ../src/native" />
<Exec Command="cmake --build . --target install" />
</Target>
The NMake generator expects to find everything set up correctly on the command line (including path to the appropriate platform compiler), but that's taken care of by the target platform selected in the driving VS.
I don't have a way of testing it right now, so the actual syntax for the project file might differ, but the idea should work.

Categories