Skip to content

Commit e9615ac

Browse files
authored
Merge branch 'main' into main
2 parents 96970da + a569ca1 commit e9615ac

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
id: host-avalonia-controls-in-winforms
3+
title: Host Avalonia controls in Windows Forms
4+
---
5+
6+
# Host Avalonia controls in Windows Forms
7+
8+
Avalonia controls can be hosted in Windows Forms applications. This enables a step-by-step migration of existing Windows Forms applications to Avalonia.
9+
10+
## Overview
11+
12+
An exemplary Windows Forms application with Avalonia controls requires at least two projects:
13+
14+
1. `YourApp` Cross platform library where you put your Avalonia controls
15+
2. `YourApp.WinForms` Your existing Windows Forms application
16+
3. `YourApp.Desktop` (optional) Executable Avalonia application, required only for the Visual Studio Avalonia designer
17+
18+
As Windows Forms is only supported on Microsoft Windows, adding Avalonia controls to your app will not make it cross platform.
19+
20+
## Step-by-step instructions
21+
22+
These instruction assume that you use Visual Studio 2022 with the Avalonia extension.
23+
You can adjust the steps and leave out the `YourApp.Desktop` project if you are using VS Code or Rider.
24+
25+
1. Create both Avalonia projects
26+
- In Visual Studio add a new project to your solution
27+
- Choose _Avalonia C# Project_
28+
- Select at least _Desktop_ as target platforms
29+
- Click _Create_
30+
- You should now have a `YourApp` and a `YourApp.Desktop` project in your solution
31+
2. Add references to your existing Windows Forms project
32+
- A package reference to `Avalonia.Desktop`
33+
- A package reference to `Avalonia.Win32.Interoperability`
34+
- A project reference to `YourApp.csproj`
35+
3. Add the following lines in your `Program.cs` before you call `Application.Run()`
36+
```cs
37+
AppBuilder.Configure<App>()
38+
.UsePlatformDetect()
39+
.SetupWithoutStarting();
40+
```
41+
4. Add an `WinFormsAvaloniaControlHost` control from the Toolbox
42+
5. Add the following line to your form's constructor after `InitializeComponent()`
43+
```cs
44+
winFormsAvaloniaControlHost1.Content = new MainView { DataContext = new MainViewModel() };
45+
```
46+
47+
Now you should see Avalonia's default main view _Welcome to Avalonia_ in your Windows Forms application.
48+
49+
:::info
50+
You cannot use ReactiveUI for your Windows Forms and Avalonia controls at the same time.
51+
52+
If you want to use it for Avalonia you must register ReactiveUI at the `AppBuilder` with `.UseReactiveUI()` in `Program.cs`.
53+
Do not include a reference to `ReactiveUI.WinForms` otherwise interactions will not work (see [#16478](https://github.com/AvaloniaUI/Avalonia/discussions/16478)).
54+
:::

docs/guides/styles-and-resources/resources.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ To add a resource dictionary file, follow this procedure:
108108
- Type the file name you want to use.
109109
- Click **Add**.
110110

111+
:::note
112+
After the resource file is created you have to correctly include it into your application. See [Include and Merge Resources](#include-and-merge-resources) section.
113+
:::
114+
111115
You can now add the resources you want to define in the position indicated. It looks like this:
112116

113117
```xml
@@ -173,6 +177,22 @@ Here the border control is using the resource with the key 'Warning'. This is de
173177

174178
Resources can be included from a resource dictionary file, and merged with the resources defined in another file (even if there are not any).
175179

180+
<img src="/img/gitbook-import/assets/image (1) (4).png" alt=""/>
181+
182+
In case, if you would like to merge resource dictionary on the whole application level, you have to declare a resource dictionary in the in the **Application.Resources** section of application XAML **App.axaml** file, like this
183+
184+
```xml
185+
<Application.Resources>
186+
<ResourceDictionary>
187+
<ResourceDictionary.MergedDictionaries>
188+
<MergeResourceInclude Source="/Assets/AppResources.axaml" />
189+
</ResourceDictionary.MergedDictionaries>
190+
</ResourceDictionary>
191+
</Application.Resources>
192+
```
193+
194+
You can also merge resource dictionary to declare merged resources to be specific to a style.
195+
176196
<img src="/img/gitbook-import/assets/image (1) (3).png" alt=""/>
177197

178198
This means that you can implement styles in one file, and use resources defined in another. This keeps your styling consistent, and your application solution well organised and easy to maintain.
@@ -189,7 +209,7 @@ To include the resources dictionary from a file in a styles file, add the follow
189209
</Styles.Resources>
190210
```
191211

192-
In the above example, the resources file `AppResources.axaml` is located in the `/Assets` project folder. You can then define the styles using the resources, for example:
212+
In the above examples, the resources file `AppResources.axaml` is located in the `/Assets` project folder. You can then define the styles using the resources, for example:
193213

194214
```xml
195215
<Style Selector="Button.btn-info">

sidebars.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ const sidebars = {
380380
'guides/platforms/ios/build-and-run-your-application-on-a-simulator',
381381
],
382382
},
383+
{
384+
'type': 'category',
385+
'label': 'Windows',
386+
'items': [
387+
'guides/platforms/windows/host-avalonia-controls-in-winforms',
388+
],
389+
},
383390
{
384391
'type': 'category',
385392
'label': 'Raspberry PI',
7.81 KB
Loading

0 commit comments

Comments
 (0)