|
| 1 | +# Introduction |
| 2 | + |
| 3 | +> [!CAUTION] |
| 4 | +> Before the project is ready for production, it may contain unstable APIs and features, including breaking changes. Please use with caution. |
| 5 | +
|
| 6 | +## Environment Setup |
| 7 | + |
| 8 | +> [!NOTE] |
| 9 | +> The following methods apply to Windows. We have not yet tested this workflow on Linux/macOS. |
| 10 | +
|
| 11 | +* [Zig](https://ziglang.org/) provides `linker/sysroot` support for [OpenHarmony-NET.PublishAotCross](https://github.com/OpenHarmony-NET/PublishAotCross?tab=readme-ov-file#openharmony-netpublishaotcross) to allow cross-compilation for `linux-x64/linux-arm64/linux-musl-x64/linux-musl-arm64` targets on Windows |
| 12 | + |
| 13 | +```shell |
| 14 | +# Install Zig on Windows via winget |
| 15 | +winget install zig.zig |
| 16 | +``` |
| 17 | + |
| 18 | +* [LLVM](https://releases.llvm.org/download.html) is needed for the compilation process that uses `llvm-objcopy.exe`, ensure it exists in your environment variables |
| 19 | + |
| 20 | +* .NET 8.0 SDK and .NET 9.0 SDK |
| 21 | + |
| 22 | +* Latest version of DevEco Studio |
| 23 | + |
| 24 | +* _Optional_ `Visual Studio 2022` |
| 25 | + |
| 26 | +* _Optional_ `JetBrains Rider` |
| 27 | + |
| 28 | +## Build and Run |
| 29 | + |
| 30 | +* Pull the latest code from [OpenHarmony.Avalonia](https://github.com/OpenHarmony-NET/OpenHarmony.Avalonia) |
| 31 | + |
| 32 | +```shell |
| 33 | +git clone https://github.com/OpenHarmony-NET/OpenHarmony.Avalonia.git --recursive |
| 34 | +``` |
| 35 | + |
| 36 | +* Enter the project directory `OpenHarmony.Avalonia` |
| 37 | + |
| 38 | +```shell |
| 39 | +|-- Directory.Build.props |
| 40 | +|-- OHOS_Project |
| 41 | +|-- OpenHarmony.Avalonia.sln |
| 42 | +|-- README.md |
| 43 | +|-- Src |
| 44 | +|-- ThirdParty |
| 45 | + |
| 46 | +3 directories, 3 files |
| 47 | +``` |
| 48 | + |
| 49 | +Then execute the following command or use `Visual Studio 2022` to open the `OpenHarmony.Avalonia.sln` solution, right-click on the `Entry` project and publish |
| 50 | + |
| 51 | +> [!NOTE] |
| 52 | +> `arm64-v8a` is suitable for physical devices, while `x86_64` is for emulators. Please choose according to your device type. |
| 53 | +
|
| 54 | +### [Physical Device](#tab/physical) |
| 55 | + |
| 56 | +```shell |
| 57 | +dotnet publish ./Src/Entry/Entry.csproj -c Release -r linux-musl-arm64 -p:PublishAot=true -o OHOS_Project/entry/libs/arm64-v8a |
| 58 | +``` |
| 59 | + |
| 60 | +Or select `PublishArm64.pubxml` in the publish page, then click the publish button |
| 61 | + |
| 62 | +### [Emulator](#tab/virtual) |
| 63 | + |
| 64 | +```shell |
| 65 | +dotnet publish ./Src/Entry/Entry.csproj -c Release -r linux-musl-x64 -p:PublishAot=true -o OHOS_Project/entry/libs/x86_64 |
| 66 | +``` |
| 67 | + |
| 68 | +Or select `PublishAmd64.pubxml` in the publish page, then click the publish button |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +* Open DevEco Studio, open the `OHOS_Project` directory, then click the run button. DevEco Studio will automatically install and run the application on your physical device/emulator |
| 73 | + |
| 74 | +> [!NOTE] |
| 75 | +> Running on a physical device may require signing and other procedures, consistent with normal HarmonyOS software development |
| 76 | +
|
| 77 | +## Run Your Own Project |
| 78 | + |
| 79 | +* Add your project reference to `Entry` (i.e., the Entry project references your project) |
| 80 | + |
| 81 | +[And replace this namespace with your own software's namespace](https://github.com/OpenHarmony-NET/OpenHarmony.Avalonia/blob/2f0af9d19832c48a69e972eb263caf4a68f381c6/Src/Entry/XComponentEntry.cs#L5) |
| 82 | + |
| 83 | +```diff |
| 84 | +using System.Runtime.CompilerServices; |
| 85 | +using System.Runtime.InteropServices; |
| 86 | +using Avalonia.OpenHarmony; |
| 87 | +using OpenHarmony.NDK.Bindings.Native; |
| 88 | +- using AOOH_Gallery; |
| 89 | ++ using YourProjectNamespace; |
| 90 | + |
| 91 | +namespace Entry; |
| 92 | +``` |
| 93 | + |
| 94 | +[Also change the `App` class's project affiliation](https://github.com/OpenHarmony-NET/OpenHarmony.Avalonia/blob/2f0af9d19832c48a69e972eb263caf4a68f381c6/Src/Entry/XComponentEntry.cs#L21) |
| 95 | + |
| 96 | +```diff |
| 97 | + try |
| 98 | + { |
| 99 | + Ace.OH_NativeXComponent_RegisterOnFrameCallback(component, &OnSurfaceRendered); |
| 100 | + if (XComponents.TryGetValue((nint)component, out var xComponent)) |
| 101 | + return; |
| 102 | ++ xComponent = new AvaloniaXComponent<App>((nint)component, (nint)window); |
| 103 | + XComponents.Add((nint)component, xComponent); |
| 104 | + xComponent.OnSurfaceCreated(); |
| 105 | + } |
| 106 | +``` |
0 commit comments