Skip to content

Commit

Permalink
update usage doc (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelazhangmsft authored Oct 15, 2020
1 parent 5d10668 commit 23af004
Showing 1 changed file with 12 additions and 42 deletions.
54 changes: 12 additions & 42 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,20 @@

## Component Project

A component project adds a NuGet reference to C#/WinRT to invoke cswinrt.exe at build time, generate projection sources, and compile these into an interop assembly. For an example of this, see the [Test Projection](https://github.com/microsoft/CsWinRT/blob/master/Projections/Test/Test.csproj). Command line options can be displayed by running **cswinrt -?**. The interop assembly is then typically distributed as a NuGet package itself.
A component project adds a NuGet reference to C#/WinRT to invoke cswinrt.exe at build time, generate projection sources, and compile these into an interop assembly. For an example of this, see the [Projection Sample](https://github.com/microsoft/CsWinRT/tree/master/Samples/Net5ProjectionSample). Command line options can be displayed by running **cswinrt -?**. The interop assembly is then typically distributed as a NuGet package itself.

## Application Project
### Example

An application project adds NuGet references to both the component interop assembly produced above, and to C#/WinRT to include the winrt.runtime assembly. If a third party WinRT component is distributed without an official interop assembly, an application project may add a reference to C#/WinRT to generate its own private component interop assembly. There are versioning concerns related to this scenario, so the preferred solution is for the third party to publish an interop assembly directly.
To invoke cswinrt to generate projection sources for types in the **Contoso** namespace, you need to add the following property to your C# library project file. In this project you would also need to reference the CsWinRT NuGet package and the project-specific `winmd` files you want to project, whether through a NuGet package, project reference or direct reference. By default, the **Windows** and **Microsoft** namespaces are not projected.

### Sample
```
<PropertyGroup>
<CsWinRTIncludes>Contoso</CsWinRTIncludes>
</PropertyGroup>
```

The following msbuild project fragment demonstrates a simple invocation of cswinrt to generate projection sources for types in the Contoso namespace. These sources are then included in the project build.
To further customize C#/WinRT behavior, refer to the [CsWinRT NuGet documentation](https://github.com/microsoft/CsWinRT/blob/master/nuget/readme.md).

```
<Target Name="GenerateProjection">
<PropertyGroup>
<CsWinRTParams>
# This sample demonstrates using a response file for cswinrt execution.
# Run "cswinrt -h" to see all command line options.
-verbose
-target $(TargetFramework)
# Include Windows SDK metadata to satisfy references to
# Windows types from project-specific metadata.
-in 10.0.18362.0
# Don't project referenced Windows types, as these are
# provided by the Windows interop assembly.
-exclude Windows
# Reference project-specific winmd files, defined elsewhere,
# such as from a NuGet package.
-in @(ContosoWinMDs->'"%(FullPath)"', ' ')
# Include project-specific namespaces/types in the projection
-include Contoso
# Write projection sources to the "Generated Files" folder,
# which should be excluded from checkin (e.g., .gitignored).
-out "$(IntermediateOutputPath)/Generated Files"
</CsWinRTParams>
</PropertyGroup>
<WriteLinesToFile
File="$(CsWinRTResponseFile)" Lines="$(CsWinRTParams)"
Overwrite="true" WriteOnlyWhenDifferent="true" />
<Message Text="$(CsWinRTCommand)" Importance="$(CsWinRTVerbosity)" />
<Exec Command="$(CsWinRTCommand)" />
</Target>
<Target Name="IncludeProjection" BeforeTargets="CoreCompile" DependsOnTargets="GenerateProjection">
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)/Generated Files/*.cs" Exclude="@(Compile)" />
</ItemGroup>
</Target>
```
## Application Project

An application project adds NuGet references to both the component interop assembly produced above and to CsWinRT to include the `winrt.runtime.dll` assembly. If the interop assembly is distributed as a NuGet package itself, this package will require a dependency on C#/WinRT for .NET5 targets. If a third party WinRT component is distributed without an official interop assembly, an application project may add a reference to C#/WinRT to generate its own private component interop assembly. There are versioning concerns related to this scenario, so the preferred solution is for the third party to publish an interop assembly directly.

0 comments on commit 23af004

Please sign in to comment.