-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed sanity check of test-drive (with Rider)
- Loading branch information
1 parent
de1bcb1
commit cd228fe
Showing
15 changed files
with
190 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
id: respond-to-an-event | ||
title: Respond to an Event | ||
--- | ||
import useBaseUrl from '@docusaurus/useBaseUrl'; | ||
import EventDebugOutputScreenshot from '/img/get-started/test-drive/event-debug-output.png'; | ||
import RiderDebugButton from '/img/get-started/test-drive/rider-toolbar-debug.png'; | ||
|
||
|
||
|
||
There are a number of ways you can implement actions in an Avalonia application. On this page, you will see how to use | ||
one of the simplest: how to write event handling code for a button click. | ||
|
||
To start, you will write a button click event handler that does not interact with any of the other controls. | ||
|
||
## Code-Behind | ||
|
||
XAML files can have C# source files associated with it referred to as by "code-behind". Code-behind is used to access | ||
named controls and handle events for its associated XAML. When using an IDE, you can find this file in | ||
the **Solution Explorer** as a sub-item of the `.axaml` file. | ||
|
||
<ThemedImage | ||
alt="Solution Explorer" | ||
className="center" | ||
sources={{ | ||
light: useBaseUrl('/img/get-started/test-drive/codebehind-mainwindow-light.png'), | ||
dark: useBaseUrl('/img/get-started/test-drive/codebehind-mainwindow-dark.png'), | ||
}} | ||
/> | ||
|
||
To change the code-behind for `MainWindow`: | ||
|
||
- Open the `MainWindow.axaml.cs` file | ||
|
||
You should see C# code like this: | ||
|
||
```csharp | ||
using Avalonia.Controls; | ||
|
||
namespace GetStartedApp.Views; | ||
|
||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
``` | ||
|
||
The partial class `MainWindow` corresponds to the `Window` created by Avalonia as a result of the XAML you already | ||
have. The namespace and class name must be the same in both XAML and code-behind. You can find this class name in the | ||
root XAML tag: | ||
|
||
```xml | ||
<UserControl x:Class="GetStartedApp.Views.MainWindow" | ||
...> | ||
</UserControl> | ||
``` | ||
|
||
To add an event handler for the `Button`, follow this procedure: | ||
|
||
- Locate the `MainWindow` constructor in the code-behind file (see above instructions). | ||
- After the constructor, add the following code: | ||
|
||
```csharp | ||
private void Button_OnClick(object? sender, RoutedEventArgs e) | ||
{ | ||
Debug.WriteLine("Click!"); | ||
} | ||
``` | ||
|
||
This will require the following using statements: | ||
|
||
```cs | ||
using Avalonia.Interactivity; | ||
using System.Diagnostics; | ||
``` | ||
|
||
- Switch to the XAML file and locate the `<Button>` tag. | ||
- Enter the `Click` attribute at the end of the tag, as follows: | ||
|
||
```xml | ||
<Button Grid.Row="2" Grid.Column="1" Click="Button_OnClick">Calculate</Button> | ||
``` | ||
|
||
- Run the app in Debug mode and click the Calculate button. | ||
|
||
|
||
<Tabs groupId="ide"> | ||
<TabItem value="rider" label="Rider"> | ||
<img className="center" src={RiderDebugButton} alt="" /> | ||
|
||
You should see the result on the Output window for Debug, like this: | ||
<ThemedImage | ||
alt="Application running" | ||
className="center" | ||
sources={{ | ||
light: useBaseUrl('/img/get-started/test-drive/rider-event-debug-output-light.png'), | ||
dark: useBaseUrl('/img/get-started/test-drive/rider-event-debug-output-dark.png'), | ||
}} | ||
/> | ||
|
||
</TabItem> | ||
<TabItem value="vs" label="Visual Studio"> | ||
You should see the result on the Output window for Debug, like this: | ||
|
||
<img className="center" src={EventDebugOutputScreenshot} alt="" /> | ||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
|
||
|
||
On the next page, you will see how to use code-behind to read and change the properties of Avalonia controls at runtime. |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.