Skip to content

Commit 69f7554

Browse files
Add usage docs (#130)
* add usage docs * typos * update usage doc
1 parent 629b004 commit 69f7554

4 files changed

Lines changed: 94 additions & 37 deletions

File tree

README.md

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
# The JS/WinRT language projection
22

3-
JavaScript WinRT (JS/WinRT) is a module for React Native Windows which provides access to WinRT APIs from JS. It uses components from [xlang](https://github.com/Microsoft/xlang) to [parse metadata](https://github.com/microsoft/winmd) for public or private WinRT COM APIs and generate JSI (JavaScript Interface) wrappers for [C++/WinRT](https://github.com/microsoft/cppwinrt) types at compile-time and the code generator was itself derived from cppwinrt.exe.
4-
5-
# Building JS/WinRT Sample
6-
7-
* Open a dev command prompt pointing at the root of the repo.
8-
* Run `npm install` under ["\samples\RNWinRTTestApp\"](./samples/RNWinRTTestApp)
9-
* Open the [`RNWinRTTestApp.sln`](./samples/RNWinRTTestApp/windows/RNWinRTTestApp.sln) solution.
10-
* Build the x64 Release or Debug configuration.
11-
* If running Debug: Run 'yarn start' under ["\samples\RNWinRTTestApp\"](./samples/RNWinRTTestApp)
12-
* F5 deploy the sample app.
13-
14-
# Adding the projection to your own solution
15-
> **DISCLAIMER**: These steps are temporary and subject to change as the projection evolves.
16-
17-
* Copy the [`WinRTTurboModule`](./samples/RNWinRTTestApp/windows/WinRTTurboModule) (from the sample) source directory to your project.
18-
* Add [`WinRTTurboModule.vcxproj`](./samples/RNWinRTTestApp/windows/WinRTTurboModule/WinRTTurboModule.vcxproj) to your solution.
19-
* Make sure your project depends on the WinRTTurboModule project.
20-
* Turn off deploy for the WinRTTurboModule project.
21-
* Add `import './WinRTTurboModule';` to your project's index.js.
22-
* Add `PackageProviders().Append(winrt::WinRTTurboModule::ReactPackageProvider());` to your project's App.cpp
23-
* Add `#include <winrt\WinRTTurboModule.h>` to your project's pch.h
24-
* Create `WinRTTurboModule.js` under your project (next to index.js) and copy the contents of [jswinrt/js/WinRTTurboModule.js](./jswinrt/js/WinRTTurboModule.js) to it. This should be something like the following:
25-
```
26-
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
27-
const module = TurboModuleRegistry.get('WinRTTurboModule');
28-
if (module) {
29-
module.initialize();
30-
}
31-
export default module;
32-
```
33-
* Add `-include [winrt namespaces]` inside <JsWinRTParameters> in your WinRTTurboModule.vcxproj. Ex:
34-
```
35-
<JsWinRTParameters>-include Windows.Globalization</JsWinRTParameters>
36-
```
37-
# Contributing
3+
JavaScript WinRT (JS/WinRT) is a module for React Native Windows which provides access to WinRT APIs from Javascript. It uses components from [xlang](https://github.com/Microsoft/xlang) to [parse metadata](https://github.com/microsoft/winmd) for public or private WinRT COM APIs and generate JSI (JavaScript Interface) wrappers for [C++/WinRT](https://github.com/microsoft/cppwinrt) types at compile-time and the code generator was itself derived from `cppwinrt.exe`.
4+
5+
## Building JS/WinRT Sample
6+
7+
- Open a dev command prompt pointing at the root of the repo.
8+
- Run `npm install` under ["\samples\RNWinRTTestApp\"](./samples/RNWinRTTestApp)
9+
- Open the [`RNWinRTTestApp.sln`](./samples/RNWinRTTestApp/windows/RNWinRTTestApp.sln) solution.
10+
- Build the x64 Release or Debug configuration.
11+
- If running Debug: Run 'yarn start' under ["\samples\RNWinRTTestApp\"](./samples/RNWinRTTestApp)
12+
- F5 deploy the sample app.
13+
14+
## Usage
15+
16+
Refer to the [usage docs](docs/USAGE.md) for instructions on how to use react-native-winrt in your React Native Windows app to call projected WinRT APIs in Javascript.
17+
18+
## Contributing
3819

3920
This project welcomes contributions and suggestions of all types. See our [Contributor's Guide](/CONTRIBUTING.md) for more information.
40-
41-
# Trademarks
21+
22+
## Trademarks
4223

4324
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

docs/USAGE.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Usage
2+
3+
The following steps demonstrate how to use the Microsoft.ReactNative.WinRT NuGet package to consume WinRT APIs in a React Native Windows (RNW) app.
4+
5+
1. Open your existing RNW app, or follow [these docs](https://microsoft.github.io/react-native-windows/docs/getting-started) to create a new RNW app.
6+
7+
1. Follow these steps to add and configure the `WinRTTurboModule` project to your RNW project. Note that the `WinRTTurboModule` project already includes a reference to the Microsoft.ReactNative.WinRT NuGet package.
8+
9+
1. Copy the [`WinRTTurboModule`](../samples/RNWinRTTestApp/windows/WinRTTurboModule) source directory from the RnWinRTTestApp sample to the `windows` directory in your React Native Windows app. Your directory structure will look something like this:
10+
11+
<img src="images/winrtturbomodule-file-explorer.png" alt="File Explorer folder structure" width="200">
12+
13+
1. Add [`WinRTTurboModule.vcxproj`](../samples/RNWinRTTestApp/windows/WinRTTurboModule/WinRTTurboModule.vcxproj) to your solution. In Visual Studio: right click on the solution, and click **Add** -> **Existing Project**. Navigate to `WinRTTurboModule.vcxproj` in the file picker dialog and open the file.
14+
15+
1. Turn off deploy for the WinRTTurboModule project. In Visual Studio, go to **Build** -> **Configuration Manager**, and uncheck the **Deploy** option for the Build/Configuration being used.
16+
17+
<img src="images/winrtturbomodule-uncheck-deploy.png" alt="Uncheck deploy" width="350">
18+
19+
1. Specify the WinRT namespaces that you want to consume in the <RnWinRTParameters> property in `WinRTTurboModule.vcxproj`. In Visual Studio, right click on the `WinRTTurboModule` project and select **Unload Project**. You can then directly edit the `WinRTTurboModule.vcxproj`. Here is an example of how to set the `RnWinRTParameters` property:
20+
21+
```xml
22+
<RnWinRTParameters>-include Windows.Globalization -include Windows.Storage</RnWinRTParameters>
23+
```
24+
25+
1. Reload and build the `WinRTTurboModule` project.
26+
27+
1. Follow these steps to include the projected WinRT namespaces and consume them in your RNW app.
28+
29+
1. Add a project reference from your React Native Windows app to the `WinRTTurboModule` project. In Visual Studio, right click on your React Native Windows app project, select **Add Project Reference** and select **WinRTTurboModule**.
30+
31+
1. Add the following line to to your project's `pch.h` file.
32+
33+
```cpp
34+
#include <winrt/WinRTTurboModule.h>
35+
```
36+
37+
1. Add the following line to your project's `App.cpp` file, underneath the line `PackageProviders().Append(make<ReactPackageProvider>());`:
38+
39+
```cpp
40+
PackageProviders().Append(winrt::WinRTTurboModule::ReactPackageProvider());`
41+
```
42+
43+
1. Add the following line to your project's `index.js` file:
44+
45+
```js
46+
import './WinRTTurboModule';
47+
```
48+
49+
1. Create a file named `WinRTTurboModule.js` under your root project directory (next to `index.js`), and copy the contents of [jswinrt/js/WinRTTurboModule.js](../jswinrt/js/WinRTTurboModule.js) to it. This should be something like the following:
50+
51+
```js
52+
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
53+
const module = TurboModuleRegistry.get('WinRTTurboModule');
54+
if (module) {
55+
module.initialize();
56+
}
57+
export default module;
58+
```
59+
60+
1. You can now call any of the projected WinRT APIs in your RNW app (in Javascript or Typescript). See [App.tsx](../samples/RNWinRTTestApp/App.tsx) in the sample for an example.
61+
62+
1. Build and deploy/run your RNW app. If running Debug, first run `yarn start` in the command prompt from the root directory of your app.
63+
64+
### Troubleshooting
65+
66+
- If running Debug, make sure to run `yarn start` in the command prompt before running the app. Otherwise, you may see an error with "connection with server could not be established"
67+
68+
- When running Debug, you may see the error: "ReferenceError: Windows is not defined". The browser debugger is not supported with React Native Windows, so you may need to change this line in `App.cpp`:
69+
70+
```cpp
71+
`InstanceSettings().UseWebDebugger(true);`
72+
```
73+
to this:
74+
```cpp
75+
`InstanceSettings().UseWebDebugger(false);`
76+
```
21.3 KB
Loading
46.8 KB
Loading

0 commit comments

Comments
 (0)