Skip to content

Commit cd228fe

Browse files
Completed sanity check of test-drive (with Rider)
1 parent de1bcb1 commit cd228fe

15 files changed

+190
-137
lines changed

docs/get-started/test-drive/code-with-controls.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@ To add control names, follow this procedure:
1717
- Add the `Name` attribute like this:
1818

1919
```xml
20-
<TextBox ... Name="celsius"/>
20+
<TextBox ... Name="Celsius"/>
2121
```
2222

2323
- Repeat the above for the Fahrenheit input:
2424

2525
```xml
26-
<TextBox ... Name="fahrenheit"/>
26+
<TextBox ... Name="Fahrenheit"/>
2727
```
2828

2929
## Get Control Values in Code-Behind
3030

3131
To access the `Text` value of the `celsius` input, follow this procedure:
3232

33-
- Switch to the **MainView.axaml.cs** code-behind file.
34-
- Locate the `ButtonClicked` event handler.
35-
- Alter the `Debug` statement to display the text property of the `celsius` input, like this:
33+
- Switch to the **MainWindow.axaml.cs** code-behind file.
34+
- Locate the `Button_OnClick` event handler.
35+
- Alter the `Debug` statement to display the text property of the `Celsius` input, like this:
3636

3737
```csharp
38-
Debug.WriteLine($"Click! Celsius={celsius.Text}");
38+
Debug.WriteLine($"Click! Celsius={Celsius.Text}");
3939
```
4040

41-
- Run the app to confirm that you can see the value in the Celsius appear in the debug window.
41+
- Run the app again (in debug mode) to confirm that you can see the value in the Celsius appear in the debug window.
4242

4343
## Set Control Values in Code-Behind
4444

@@ -51,7 +51,7 @@ Tf = Tc * (9/5) + 32
5151

5252
To add the conversion formula, follow this procedure:
5353

54-
- Locate the `ButtonClicked` event handler.
54+
- Locate the `Button_OnClick` event handler.
5555
- Validate the Celsius input text as a number.
5656
- Use the conversion formula.
5757
- Update the `Text` in the Fahrenheit input.
@@ -60,17 +60,17 @@ To add the conversion formula, follow this procedure:
6060
One implementation of the above is as follows:
6161

6262
```csharp
63-
public void ButtonClicked(object source, RoutedEventArgs args)
63+
private void Button_OnClick(object? sender, RoutedEventArgs e)
6464
{
65-
if (Double.TryParse(celsius.Text, out double C))
65+
if (double.TryParse(Celsius.Text, out double C))
6666
{
6767
var F = C * (9d / 5d) + 32;
68-
fahrenheit.Text = F.ToString("0.0");
68+
Fahrenheit.Text = F.ToString("0.0");
6969
}
7070
else
7171
{
72-
celsius.Text = "0";
73-
fahrenheit.Text = "0";
72+
Celsius.Text = "0";
73+
Fahrenheit.Text = "0";
7474
}
7575
}
7676
```

docs/get-started/test-drive/create-a-project.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import useBaseUrl from '@docusaurus/useBaseUrl';
77

88
import VsFindAvaloniaTemplateScreenshot from '/img/get-started/test-drive/vs-find-avalonia-template-screenshot.png';
99
import VsNewAvaloniaProjectScreenshot from '/img/get-started/test-drive/vs-new-avalonia-project-screenshot.png';
10-
import RiderRunScreenshot from '/img/get-started/test-drive/rider-run.png';
10+
import RiderRunScreenshot from '/img/get-started/test-drive/rider-toolbar-run.png';
1111
import InitialWindowScreenshot from '/img/get-started/test-drive/initial-window.png';
1212

1313
import vscode1 from '/img/get-started/test-drive/vscode-command-new-project.png';

docs/get-started/test-drive/main-window.mdx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,17 @@ Avalonia controls; the window will be drawn on the target platform with 4 **layo
2424

2525
<img src={LayoutZonesDiagram} alt="" />
2626

27-
In the current application, the content zone of the `Window` references another view: **`<views:MainView />`**. This is a
28-
reference to the **MainView.axaml** file, which is a `UserControl` that will be displayed in the content zone of the `Window`.
27+
## The MainWindow Content
2928

30-
## The MainView User control
31-
32-
Inside this user control, you will see a `<TextBlock>...</TextBlock>` XAML tag. This represents a `TextBlock` control which draws
29+
Inside the Windows Content, you will see a `<TextBlock>...</TextBlock>` XAML tag. This represents a `TextBlock` control which draws
3330
`Text` to the screen. The `Text` property of the `TextBlock` is bound to the **Greeting** property of
3431
the **MainViewModel** class.
3532

3633
```xml title='XAML'
3734
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
3835
```
3936

40-
You can change the text in the file **MainViewModel.cs** to see the change reflected in the user interface.
37+
You can change the text in the file **MainWindowViewModel.cs** to see the change reflected in the user interface.
4138

4239
```csharp title='C#'
4340
namespace GetStartedApp.ViewModels;
@@ -69,7 +66,7 @@ The XAML Previewer create an instance of your app in a special authoring mode, k
6966

7067
<Tabs groupId="ide">
7168
<TabItem value="rider" label="Rider">
72-
Navigate to the **MainView.axaml** file and click the **Split View** button at the top right of the editor window.
69+
Navigate to the **MainWindow.axaml** file and click the **Split View** button at the top right of the editor window.
7370

7471
<ThemedImage
7572
alt="Rider Avalonia Previewer Controls"

docs/get-started/test-drive/response-to-an-event.md

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
id: respond-to-an-event
3+
title: Respond to an Event
4+
---
5+
import useBaseUrl from '@docusaurus/useBaseUrl';
6+
import EventDebugOutputScreenshot from '/img/get-started/test-drive/event-debug-output.png';
7+
import RiderDebugButton from '/img/get-started/test-drive/rider-toolbar-debug.png';
8+
9+
10+
11+
There are a number of ways you can implement actions in an Avalonia application. On this page, you will see how to use
12+
one of the simplest: how to write event handling code for a button click.
13+
14+
To start, you will write a button click event handler that does not interact with any of the other controls.
15+
16+
## Code-Behind
17+
18+
XAML files can have C# source files associated with it referred to as by "code-behind". Code-behind is used to access
19+
named controls and handle events for its associated XAML. When using an IDE, you can find this file in
20+
the **Solution Explorer** as a sub-item of the `.axaml` file.
21+
22+
<ThemedImage
23+
alt="Solution Explorer"
24+
className="center"
25+
sources={{
26+
light: useBaseUrl('/img/get-started/test-drive/codebehind-mainwindow-light.png'),
27+
dark: useBaseUrl('/img/get-started/test-drive/codebehind-mainwindow-dark.png'),
28+
}}
29+
/>
30+
31+
To change the code-behind for `MainWindow`:
32+
33+
- Open the `MainWindow.axaml.cs` file
34+
35+
You should see C# code like this:
36+
37+
```csharp
38+
using Avalonia.Controls;
39+
40+
namespace GetStartedApp.Views;
41+
42+
public partial class MainWindow : Window
43+
{
44+
public MainWindow()
45+
{
46+
InitializeComponent();
47+
}
48+
}
49+
```
50+
51+
The partial class `MainWindow` corresponds to the `Window` created by Avalonia as a result of the XAML you already
52+
have. The namespace and class name must be the same in both XAML and code-behind. You can find this class name in the
53+
root XAML tag:
54+
55+
```xml
56+
<UserControl x:Class="GetStartedApp.Views.MainWindow"
57+
...>
58+
</UserControl>
59+
```
60+
61+
To add an event handler for the `Button`, follow this procedure:
62+
63+
- Locate the `MainWindow` constructor in the code-behind file (see above instructions).
64+
- After the constructor, add the following code:
65+
66+
```csharp
67+
private void Button_OnClick(object? sender, RoutedEventArgs e)
68+
{
69+
Debug.WriteLine("Click!");
70+
}
71+
```
72+
73+
This will require the following using statements:
74+
75+
```cs
76+
using Avalonia.Interactivity;
77+
using System.Diagnostics;
78+
```
79+
80+
- Switch to the XAML file and locate the `<Button>` tag.
81+
- Enter the `Click` attribute at the end of the tag, as follows:
82+
83+
```xml
84+
<Button Grid.Row="2" Grid.Column="1" Click="Button_OnClick">Calculate</Button>
85+
```
86+
87+
- Run the app in Debug mode and click the Calculate button.
88+
89+
90+
<Tabs groupId="ide">
91+
<TabItem value="rider" label="Rider">
92+
<img className="center" src={RiderDebugButton} alt="" />
93+
94+
You should see the result on the Output window for Debug, like this:
95+
<ThemedImage
96+
alt="Application running"
97+
className="center"
98+
sources={{
99+
light: useBaseUrl('/img/get-started/test-drive/rider-event-debug-output-light.png'),
100+
dark: useBaseUrl('/img/get-started/test-drive/rider-event-debug-output-dark.png'),
101+
}}
102+
/>
103+
104+
</TabItem>
105+
<TabItem value="vs" label="Visual Studio">
106+
You should see the result on the Output window for Debug, like this:
107+
108+
<img className="center" src={EventDebugOutputScreenshot} alt="" />
109+
</TabItem>
110+
111+
</Tabs>
112+
113+
114+
115+
On the next page, you will see how to use code-behind to read and change the properties of Avalonia controls at runtime.

docs/get-started/test-drive/the-design-preview.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)