|
| 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 | + ``` |
0 commit comments