diff --git a/accelerate/tools/vs-extension/getting-started.md b/accelerate/tools/vs-extension/getting-started.md index a26f7b446..ab9bf091a 100644 --- a/accelerate/tools/vs-extension/getting-started.md +++ b/accelerate/tools/vs-extension/getting-started.md @@ -8,7 +8,7 @@ It supports Visual Studio 2022 version 17.10 and above. In addition to downloading and installing the extension from the marketplace you can also search within the **Manage Extensions** window on the **Extensions** menu -![Avalonia extension in the Manage Extensions window](../../../static/img/get-started/avalonia-vs-extension-nuget.png) +![Avalonia extension in the Manage Extensions window](../../../static/img/vs-extension/avalonia-vs-extension-nuget.png) ## Features diff --git a/docs/get-started/choose-a-solution-template.md b/docs/get-started/choose-a-solution-template.md deleted file mode 100644 index adfacdb7e..000000000 --- a/docs/get-started/choose-a-solution-template.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -id: choose-a-solution-template -title: Choose a Template ---- - -import Highlight from '@site/src/components/Highlight'; - -The _Avalonia for Visual Studio_ extension includes some solution templates that you can use to start an _Avalonia UI_ project. To get started, you will use the simplest of these templates. - -Follow this procedure to choose the template: - -- Run **Visual Studio**, click **Create a new project**. -- Type 'Avalonia' in the search box. -- Click **Avalonia Application** (the first item in the search results - for C#), then click **Next**. - -
- -
- -- Complete the **Configure you new project** dialog, and click **Create**. - -The template will create a new solution and project for you with the Avalonia UI dependencies set, and 5 files. - -
- -
- -You are now ready to explore the app. \ No newline at end of file diff --git a/docs/get-started/getting-started.md b/docs/get-started/getting-started.md deleted file mode 100644 index d89f191a5..000000000 --- a/docs/get-started/getting-started.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -id: getting-started -title: Starting with the CLI ---- - -If you build your projects with the .NET CLI, then follow the procedures here to install the _Avalonia UI_ templates and create your first application. - -## Install Avalonia UI Templates - -To install the _Avalonia UI_ templates, run the following command: - -```bash -dotnet new install Avalonia.Templates -``` - -:::info -Note: For .NET 6.0 and earlier, you must use `--install` instead. -::: - -## Create a new Application - -Once the templates are installed, you can create a new _Avalonia UI_ application by running the following command: - -```bash -dotnet new avalonia.app -o MyApp -``` - -This will create a new folder called `MyApp` containing your application files. To run the application, navigate to the `MyApp` folder and run: - -```bash -cd MyApp -dotnet run -``` - -That is all there is to it! Your _Avalonia UI_ application is now up and running. Next you can open the `MyApp` folder to start improving and building your application further. diff --git a/docs/get-started/index.md b/docs/get-started/index.md deleted file mode 100644 index 886af04d3..000000000 --- a/docs/get-started/index.md +++ /dev/null @@ -1,6 +0,0 @@ -# Get Started - -import {DocsCardList} from '../../src/components/DocsCard'; -import {useCurrentSidebarCategory} from '@docusaurus/theme-common'; - - \ No newline at end of file diff --git a/docs/get-started/index.mdx b/docs/get-started/index.mdx new file mode 100644 index 000000000..79625220a --- /dev/null +++ b/docs/get-started/index.mdx @@ -0,0 +1,247 @@ +--- +id: index +title: Getting started +description: Get started with Avalonia. Install Avalonia and its prerequisites, then launch your first project using an Avalonia template. +--- +import DownloadAvaloniaRider from '/img/get-started/download-rider-avaloniarider.png'; +import DownloadAvaloniaExtensionForVS from '/img/get-started/download-vs-avalonia-extension.png'; +import WelcomeToAvalonia from '/img/get-started/welcome-to-avalonia.png'; + +import useBaseUrl from '@docusaurus/useBaseUrl'; + +# Getting started + +Let’s get started with Avalonia. This guide takes you through installing Avalonia and launching your first project in your integrated development environment (IDE). + +## Prerequisites + +You must have installed: + +- .NET (8.0 or above, as of October 2025) +- IDE of your choice (we recommend JetBrains Rider or Visual Studio) + +### Installing .NET + +Download from the [.NET website](https://dotnet.microsoft.com/en-us/download/dotnet), then follow the installation instructions. + +### Choosing an IDE + +Avalonia works best in Visual Studio or JetBrains Rider. + +Visual Studio Code is also supported, although we do not recommend it due to more limited features. + + + + We recommend [JetBrains Rider](https://www.jetbrains.com/rider/) if you use macOS or Linux. JetBrains Rider offers a complete, polished development experience on these operating systems, including built-in support for XAML. + + Consider installing the third-party plugin [AvaloniaRider](https://plugins.jetbrains.com/plugin/14839-avaloniarider), which enables live preview of your XAML as you work. + + To install AvaloniaRider: + + 1. In JetBrains Rider, go to **Settings → Plugins**. + 2. Go to the **Marketplace** tab. + 3. In the search bar, input “AvaloniaRider”. + 4. Click **Install**. + 5. Follow any additional installation instructions. You may need to close and reopen JetBrains Rider to complete the installation. + +
+ A screenshot displaying how to download the AvaloniaRider extension in Rider. +
+ + For more information on using the XAML previewer, see the page on [XAML previewers](/docs/get-started/xaml-previewers). +
+ + If you’re working on Windows, you can use [Visual Studio](https://visualstudio.microsoft.com/) with the Avalonia for Visual Studio extension. + + To install Avalonia for Visual Studio: + + 1. In Visual Studio, go to **Extensions → Manage Extensions**. + 2. In the search bar, input “Avalonia”. + 3. Click **Install**. + 4. Follow any additional installation instructions. You may need to close and reopen Visual Studio to complete the installation. + +
+ A screenshot displaying how to download the Avalonia extension in Visual Studio. +
+ + Alternatively, you can [download the extension from the Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaVS). + + :::note + If you are using an older version of Visual Studio, you may need to download an older version of the extension from the Marketplace, e.g., the [2019 version](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaforVisualStudio). + ::: + + For more information on using the XAML previewer, see the page on [XAML previewers](/docs/get-started/xaml-previewers). +
+ + If you prefer to use [Visual Studio Code](https://code.visualstudio.com/), the Avalonia for VSCode extension provides basic functionalities like autocomplete and previewer. You can [download the extension from the Visual Studio Code Marketplace](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.vscode-avalonia). + + Due to limited support, we do not recommend Visual Studio Code. + +
+ +## Installing Avalonia templates + +Depending on which IDE you are using, you may already have the [Avalonia templates](https://github.com/AvaloniaUI/avalonia-dotnet-templates) installed. This is the case for Visual Studio with the Avalonia for Visual Studio extension. + +You can run this command to check what .NET templates you have available: + +``` +dotnet new list +``` + +Look for these Avalonia templates among the other .NET templates: + +``` +Template Name Short Name Language Tags +-------------------------------------------- -------------------------- ---------- --------------------------------------------------------- +Avalonia .NET App avalonia.app [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia .NET MVVM App avalonia.mvvm [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia Cross Platform Application avalonia.xplat [C#],F# Desktop/Xaml/Avalonia/Browser/Mobile +``` + +If you do not see these templates on the list, you can install them by running this command: + +``` +dotnet new install Avalonia.Templates +``` + +Check the output. You should see that these templates are installed: + +``` +Template Name Short Name Language Tags +-------------------------------------------- -------------------------- ---------- --------------------------------------------------------- +Avalonia .NET App avalonia.app [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia .NET MVVM App avalonia.app [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia Cross Platform Application avalonia.xplat [C#],F# Desktop/Xaml/Avalonia/Web/Mobile +Avalonia Resource Dictionary avalonia.resource Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia Styles avalonia.styles Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia TemplatedControl avalonia.templatedcontrol [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia UserControl avalonia.usercontrol [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +Avalonia Window avalonia.window [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS +``` + +### Installation troubleshooting + +#### .NET is not a recognized program + +First, ensure the .NET SDK is installed. Run this command: + +``` +dotnet --list-sdks +``` + +If a .NET SDK is correctly installed, this returns an output similar to the following: + +``` +8.0.202 [C:\Program Files\dotnet\sdk] +``` + +If the terminal continues to report that .NET is missing, try restarting the terminal. + +#### `Avalonia.Templates` package cannot be found + +Ensure NuGet is correctly set up. Run this command: + +``` +dotnet nuget list source +``` + +Check that the output displays the following as a registered source: + +``` +nuget.org [Enabled] +https://api.nuget.org/v3/index.json +``` + +If this source is not listed, add it using this command: + +``` +dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org +``` + +If the package install continues to fail even with NuGet listed as a registered source, check your network connectivity or firewall settings. + +## Creating your first project + +For your first project, we’re going to use the MVVM template. + + + + 1. On the Rider startup screen, select **New Solution**. + 2. In the side bar, scroll down to “Custom Templates”. Select **Avalonia .NET MVVM App**. + 3. Name your solution “GetStartedApp”. + 4. Click **Create**. + + + 1. In Visual Studio, click **File → New → Project/Solution**. + 2. In the search box, input “Avalonia”. + 3. Select **Avalonia .NET MVVM App** from the search results. If Visual Studio offers multiple options, select the one for **C#**. Then, click **Next**. + 4. Name your project “GetStartedApp”. + 5. Change the target directory if desired. Then, click **Next**. + 6. Select your preferred version of **.NET** as the framework. + 7. If given the option to choose target platforms, select **Desktop**. + 8. Click **Create**. + + + 1. In Visual Studio Code, bring up the command palette. This is Ctrl + Shift + P on Windows, or Cmd + Shift + P on macOS. + 2. Input “.NET” into the search. + 3. From the search results, select **.NET: New Project...** + 4. From the list of project templates, select **Avalonia MVVM App**. + 5. Specify a directory on your device for the project. + 6. Name your project “GetStartedApp”. + 7. Click **Create project**. + + + 1. In the command line, run this command: + + ``` + dotnet new avalonia.mvvm -o GetStartedApp + ``` + + 2. Check your device for a new folder named **GetStartedApp**, containing the new project files. + + + +## Running your project + + + Click **Run** in the top toolbar. + + The solution builds and runs your app in a new window. By default, this displays the string, “Welcome to Avalonia!” + + + In the top toolbar, select "GetStartedApp" next to the **Run** button. Then, click **Run**. + + The solution builds and runs your app in a new window. By default, this displays the string, “Welcome to Avalonia!” + + + 1. Select **Run and Debug** in the side navigation bar. + 2. If prompted to select a debugger, choose **C#**. + 3. Click **Run and Debug.** + + The solution builds and runs your app in a new window. By default, this displays the string, “Welcome to Avalonia!” + + + 1. Navigate to the directory containing your **GetStartedApp** project. + 2. Run the command `dotnet run`. + + The solution builds and runs your app in a new window. By default, this displays the string, “Welcome to Avalonia!” + + + +
+A screenshot of the default Avalonia app running in a window. +
+ +## Next steps + +You have now installed Avalonia on your device and know how to create and run Avalonia projects. + +You can start developing an app right away, if you wish. + +Or, for a short tutorial building a simple app that introduces you to basic concepts and functions in Avalonia, proceed to the next page: + +
+
+
diff --git a/docs/get-started/install-the-avalonia-extension.md b/docs/get-started/install-the-avalonia-extension.md deleted file mode 100644 index 0c171939b..000000000 --- a/docs/get-started/install-the-avalonia-extension.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -id: install-the-avalonia-extension -title: Install the Avalonia Extension ---- - -import Highlight from '@site/src/components/Highlight'; - -To implement applications using _Avalonia UI_, you first need to install the _Avalonia for Visual Studio_ extension. - -
- -
- -The extension includes a designer with an XAML pane linked to a UI preview that is capable of updating what you see, as you write XAML. The extension also installs all the library files you need, provides Intellisense, and adds some solution templates. - -Follow this procedure to install the _Avalonia for Visual Studio_ extension: - -- In Visual Studio click **Manage Extensions** on the **Extensions** top menu. -- In the **Search** box, type 'Avalonia'.& -- Click **Download** and follow the instructions (you will need to close Visual Studio while the VSIX installation runs). - -
- -
- -:::info -Alternatively, without running Visual Studio, you can download the extension [here](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaVS). -::: - -:::info -If you are using VS2019 or VS2017 you will need to download the extension for older versions [here](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaforVisualStudio). -::: - -You can now create your first _Avalonia UI_ app. \ No newline at end of file diff --git a/docs/get-started/install.md b/docs/get-started/install.md deleted file mode 100644 index 6cef3660d..000000000 --- a/docs/get-started/install.md +++ /dev/null @@ -1,96 +0,0 @@ ---- -id: install -title: Install ---- - -## Preinstallation - -Please start with the supported IDE of your choice installed. Avalonia supports Visual Studio, Rider, and Visual Studio -Code. - -## Install Avalonia UI Templates - -The best way to get started with Avalonia is by creating an application using a project template. - -To install the [Avalonia templates](https://github.com/AvaloniaUI/avalonia-dotnet-templates), run the following command: - -```bash title='Bash' -dotnet new install Avalonia.Templates -``` - -:::note -For .NET 6.0 and earlier, replace `install` with `--install` -::: - -To list the installed templates run - -```bash title='Bash' - dotnet new list -``` - -You should see the installed Avalonia templates: - -``` -Template Name Short Name Language Tags --------------------------------------------- -------------------------- ---------- --------------------------------------------------------- -Avalonia App avalonia.app [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS -Avalonia MVVM App avalonia.mvvm [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS -Avalonia Cross Platform Application avalonia.xplat [C#],F# Desktop/Xaml/Avalonia/Web/Mobile -Avalonia Resource Dictionary avalonia.resource Desktop/Xaml/Avalonia/Windows/Linux/macOS -Avalonia Styles avalonia.styles Desktop/Xaml/Avalonia/Windows/Linux/macOS -Avalonia TemplatedControl avalonia.templatedcontrol [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS -Avalonia UserControl avalonia.usercontrol [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS -Avalonia Window avalonia.window [C#],F# Desktop/Xaml/Avalonia/Windows/Linux/macOS -``` - -## Create a new Application - -Once the project templates are installed, you can create a new _Avalonia UI_ application from CLI by running the following command: - -```bash title='Bash' -dotnet new avalonia.app -o MyApp -``` - -This will create a new folder called `MyApp` containing your application files. To run the application, navigate to the `MyApp` folder and run: - -```bash title='Bash' -cd MyApp -dotnet run -``` - -The project templates will also allow for project creation from your IDE. - -## Installation Troubleshooting - -### Ensure .NET SDK is installed - -```bash -dotnet --list-sdks - -8.0.202 [C:\Program Files\dotnet\sdk] <-- Your version may vary -``` - -If `dotnet` is not a recognized program, then ensure you've installed your IDE first. Next, ensure that `dotnet` is -associated with the terminal. On Windows, this involves checking environment variables: `echo %PATH%` in the command prompt or -`echo $Env:PATH` in PowerShell. - -### Ensure NuGet source is correct - -If while installing the project templates, you receive an error stating the `Avalonia.Templates` package cannot be found, -then ensure NuGet is correctly setup with .NET's standard global package source. - -```bash -dotnet nuget list source - -Registered Sources: - 1. nuget.org [Enabled] - https://api.nuget.org/v3/index.json -``` - -If this source is not listed, add it: - -```bash -dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org -``` - -If the package install still fails despite NuGet being listed, then suspect a network connectivity or firewall issue. diff --git a/docs/get-started/introduction.md b/docs/get-started/introduction.md deleted file mode 100644 index 4a61ad2aa..000000000 --- a/docs/get-started/introduction.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -id: introduction -title: Introduction ---- - -import Highlight from '@site/src/components/Highlight'; - -You can use this section to get started straight away with _Avalonia UI_ and _Visual Studio_. This will take you through a simple tutorial example step-by-step; and introduce you to some of the _Avalonia UI_ concepts and techniques. - -You do not need any prior knowledge of _Avalonia UI_ or other XAML-based frameworks to learn from this section. You will need _Microsoft Visual Studio_ installed. - -Click **Next** (right-arrow) to get started. - -:::info -If you are a more experienced user, you may want to extend your knowledge of _Avalonia UI_ instead, by using our Basics section [here](../basics) or one of the How-To Guides [here](../guides). -::: diff --git a/docs/get-started/set-up-an-editor.md b/docs/get-started/set-up-an-editor.md deleted file mode 100644 index b197dbe7f..000000000 --- a/docs/get-started/set-up-an-editor.md +++ /dev/null @@ -1,68 +0,0 @@ ---- -id: set-up-an-editor -title: Set Up an Editor ---- - -import AvaloniaVsExtensionMarketplaceScreenshot from '/img/get-started/avalonia-vs-extension-marketplace.png'; -import AvaloniaVsExtensionNuGetScreenshot from '/img/get-started/avalonia-vs-extension-nuget.png'; - -# Set Up an Editor - -You can create an Avalonia application using any code editor, but using an IDE will give you support for authoring Avalonia XAML files with a previewer and code completion. - -## Visual Studio - -If you're developing Avalonia with Visual Studio you should install the [Avalonia for Visual Studio](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaVS) extension. - -Avalonia Extension shown in the Marketplace - -The extension provides enhanced editor support for Avalonia XAML together with a previewer. - -To install the Avalonia for Visual Studio extension: - -* In Visual Studio click **Manage Extensions** on the **Extensions** menu -* In the **Search** box, type "Avalonia" -* Click **Download** and follow the instructions (you will need to close Visual Studio to complete the installation) - -Avalonia extension shown in the Manage Extensions window - -:::info -Alternatively you can [download the extension from the Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaVS). -::: - -:::info -If you are using VS2019 or VS2017 you will need to [download the version of extension for older Visual Studio versions](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.AvaloniaforVisualStudio). -::: - -## JetBrains Rider - -The [JetBrains Rider](https://www.jetbrains.com/rider/) IDE has built-in support for Avalonia XAML starting in 2020.3 including first-class support for Avalonia-specific XAML features and custom code inspections. Now that Rider is free for individual use, we strongly recommend it as the primary IDE for Avalonia development, especially for developers on macOS and Linux. - -Rider offers the most complete and polished development experience for Avalonia, with built-in features including: - -* Advanced XAML completion and navigation -* Rich code analysis and quick-fixes -* Comprehensive debugging tools -* Built-in performance profiling - -### AvaloniaRider Plugin - -The 3rd party [AvaloniaRider](https://plugins.jetbrains.com/plugin/14839-avaloniarider) plugin adds the [Avalonia file templates](./install.md) to Rider, as well as enables live XAML preview functionality. - -While Rider includes native Avalonia XAML support out of the box, This plugin provides a live preview of your XAML changes as you type, similar to the preview feature available in Visual Studio and Visual Studio Code. - -Note that the plugin is optional - you can develop Avalonia applications in Rider without it, but the live preview capability and file templates makes XAML development more efficient. - -## Visual Studio Code - -The Avalonia for [Visual Studio Code Extension](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.vscode-avalonia) contains basic support for Avalonia XAML autocomplete and previewer. While functional, the development experience is not as rich as what you'll find in Rider or Visual Studio. For developers on macOS and Linux requiring a full IDE experience, we recommend using JetBrains Rider instead. - -If you still prefer to use VS Code, you can install the extension from the [Visual Studio Code marketplace](https://marketplace.visualstudio.com/items?itemName=AvaloniaTeam.vscode-avalonia). - -## Editor Comparison - -For the best Avalonia development experience: - -* **Windows**: Use either Visual Studio or JetBrains Rider -* **macOS/Linux**: Use JetBrains Rider -* **Lightweight Editor**: Visual Studio Code can be used but offers a more limited feature set diff --git a/docs/get-started/starter-tutorial/adding-a-control.mdx b/docs/get-started/starter-tutorial/adding-a-control.mdx new file mode 100644 index 000000000..52d13b224 --- /dev/null +++ b/docs/get-started/starter-tutorial/adding-a-control.mdx @@ -0,0 +1,69 @@ +--- +id: adding-a-control +title: Adding a control +description: How to add a button to your temperature converter app. Part 2 of 7 the Avalonia starter tutorial. +--- + +import CalculateButtonLeft from '/img/get-started/calculate-button-left.png'; +import CalculateButtonCenter from '/img/get-started/calculate-button-center.png'; + +# Adding a control + +The first step for our temperature converter app is to add a **control**. By “controls”, we mean UI elements that allow interaction with your app. Some examples of controls are buttons, sliders, checkboxes or menus. + +For more information on controls, see the [Controls reference page](/docs/reference/controls). + +## Inserting a button + +Let’s try replacing the text in the app with a button. + +1. Stop the app if it is still running. +2. In the file **MainWindow.axaml**, locate this line: + +```xml + +``` + +3. Replace the entire line with this: + +```xml + +``` + +4. Run the app or check the previewer. You should now see a **Calculate** button in the app window. +5. You can try hovering over or clicking the button to see how its appearance changes. + +
+A screenshot of an app running in a window, with the button aligned left. +
+ +## Setting the button's attributes + +Controls in Avalonia use XML attributes to specify their presentation and behavior. + +Your **Calculate** button is currently aligned against the left edge of the window. This is because the default value of the `HorizontalAlignment` attribute is `Left`. Let’s change this attribute to put the button in the center instead. + +1. In the file **MainWindow.axaml**, go to the line you added for the button: + +```xml + +``` + +2. Add the `HorizontalAlignment` attribute to the ` +``` + +3. Run the app or check the previewer. You should see that the Calculate button moves to the center of the window. + +
+A screenshot of an app running in a window, with the button aligned center. +
+ +On the next page of this tutorial, you will learn how to add multiple elements to your app using layout controls. + +
+
+
diff --git a/docs/get-started/starter-tutorial/adding-some-layout.mdx b/docs/get-started/starter-tutorial/adding-some-layout.mdx new file mode 100644 index 000000000..ec61cbf99 --- /dev/null +++ b/docs/get-started/starter-tutorial/adding-some-layout.mdx @@ -0,0 +1,162 @@ +--- +id: adding-some-layout +title: Adding some layout +description: How to use layout controls to have multiple controls on your temperature converter app. Part 3 of 7 the Avalonia starter tutorial. +--- + +import TemperatureConverterTextOnly from '/img/get-started/temperature-converter-text-only.png'; +import TemperatureConverterBlueBorder from '/img/get-started/temperature-converter-blue-border.png'; +import TemperatureConverterEmptyGrid from '/img/get-started/temperature-converter-empty-grid.png'; +import TemperatureConverterFilledGrid from '/img/get-started/temperature-converter-filled-grid.png'; + +# Adding some layout + +At this point, your temperature converter app has a single button in the center of the window. You cannot add any more elements, because each Avalonia window allows only one control in its content zone. (More on layout zones later, on the next page: [Customizing the Avalonia window](/docs/get-started/starter-tutorial/customizing-the-avalonia-window).) + +To place multiple UI elements in the window, you must use a **layout control.** + +For more information on layout controls, see the [Layout controls page](/docs/reference/controls/layout-controls). + +## Inserting a stack panel + +We can use the `StackPanel` layout control to place some text above the button. + +1. In the file **MainWindow.axaml**, enclose your `Button` with a `...` tag. + +```xml + + + +``` + +2. Add a `TextBlock` above the Button. (You may recall the `TextBlock` tag from the default **MainWindow.axaml**—this control prints text in the window.) Set the attributes of the `TextBlock` as follows: + +- `Margin="5"` +- `FontSize="24"` +- `HorizontalAlignment="Center"` +- `Text="Temperature Converter"` + +```xml + + + + + + +``` + +3. Run the app or check the previewer. You should see the text “Temperature Converter” positioned above the **Calculate** button. + +
+A screenshot showing a work-in-progress app, with text above a button. +
+ +4. Enclose the `TextBlock` with a `...` tag. Set the attributes of the `Border` as follows: + +- `Margin="5"` +- `CornerRadius="10"` +- `Background="LightBlue"` + +```xml + + + + + + + + +``` + +5. Run the app or check the previewer. You should see that the text “Temperature Converter” is now inside a rounded blue box. + +
+A screenshot showing a work-in-progress app, with text enclosed inside a blue border above a button. +
+ +:::note +By default, `StackPanel` arranges elements in a vertical stack. You can change this to horizontal by setting the `Orientation` attribute to `Horizontal`. +::: + +## Inserting a grid + +Next, we’ll add a `Grid` layout control to our temperature converter app. `Grid` creates cells in rows and columns, into which you can place more controls. + +1. Stop the app if it is still running. +2. In the file **MainWindow.axaml**, insert a `...` tag between `` and ` + +``` + +We have specified some attributes for the `Grid`: + +- It has two columns and two rows. +- Gridlines are visible. +- Cell height automatically scales to match the content. Because the automatic height of an empty cell is zero, the `Grid` currently appears on your previewer as a horizontal straight line. + +
+A screenshot showing a work-in-progress app, with a title, a dotted line, and a button. +
+ +## Inserting controls in the grid + +1. Insert `TextBlock` controls in the left cells of the grid, using the `Grid.Row` and `Grid.Column` attributes to assign the target cells. Use these text blocks to fill the cells with the text “Celsius” and “Fahrenheit”. + +:::note +The first cell in a row or column of a `Grid` is numbered 0. +::: + +```xml + + Celsius + Fahrenheit + +``` + +2. Insert `TextBox` controls in the right cells of the grid, again using the `Grid.Row` and `Grid.Column` attributes to assign the target cells. `TextBox` is a control that creates an area for keyboard input. + +```xml + + Celsius + + Fahrenheit + + +``` + +3. Run the app or check the previewer. You should see your text and input boxes added to the window, within the cells marked by the gridlines. + +
+A screenshot showing a work-in-progress app, with a title, input boxes in a grid, and a button. +
+ +On the next page of this tutorial, you will learn how to adjust the size of the app window. + +
+
+
diff --git a/docs/get-started/starter-tutorial/converting-data.mdx b/docs/get-started/starter-tutorial/converting-data.mdx new file mode 100644 index 000000000..46e57b9a2 --- /dev/null +++ b/docs/get-started/starter-tutorial/converting-data.mdx @@ -0,0 +1,136 @@ +--- +id: converting-data +title: Converting data +description: How to use code-behind to execute a response when the button is clicked. Part 6 of 7 the Avalonia starter tutorial. +--- + +# Converting data + +To complete our temperature converter app, we need to give it the ability to take a numerical input and convert it into a different number using a specified formula. + +## Naming your controls + +To differentiate controls in your app, you can assign names to them. We’re going to do that for the `TextBox` controls. + +1. Stop the app if it is running. +2. In the file **MainWindow.axaml**, locate the Celsius text box: `` +3. Add the `Name` attribute to the `` tag, like so: + +```xml + +``` + +4. Locate the Fahrenheit text box: `` +5. Add the `Name` attribute to the `` tag, like so: + +```xml + +``` + +## Getting input values + +Next, we need the app to be able to access values entered into the `Celsius` text box. + +1. In the code-behind file **MainWindow.axaml.cs**, locate the `Button_OnClick` event handler that you [created earlier](/docs/get-started/starter-tutorial/establishing-events-and-responses). +2. Change the `Debug` statement to display the text entered into the `Celsius` text box, like so: + +```cs +Debug.WriteLine($"Click! Celsius={Celsius.Text}"); +``` + +3. Run the app again, in [debug mode if required by your IDE](/docs/get-started/starter-tutorial/establishing-events-and-responses). +4. Click the **Calculate** button a few times. +5. Change the number in the `Celsius` text box and click **Calculate** a few more times. +6. Check the debug output. Confirm that the input values of the `Celsius` text box are being printed. + +## Implementing the conversion formula + +The final step is to program the app to apply the mathematical formula that converts a Celsius value to Fahrenheit, then print the calculated output in the Fahrenheit text box. + +In case you aren’t familiar with the Celsius-Fahrenheit conversion formula, it is: + +> Fahrenheit = Celsius * (9/5) + 32 + +Here’s how we implement this logic in our C# code-behind. + +1. In the code-behind file **MainWindow.axaml.cs**, locate the `Button_OnClick` event handler you [created earlier](/docs/get-started/starter-tutorial/establishing-events-and-responses). +2. Delete the `Debug` statement. +3. (Optional) You can also delete the statement `using System.Diagnostics;` from the top of the file. It is no longer needed. +4. Within the `Button_OnClick` event handler, add this code to validate the Celsius input is a number: + +```cs +if (double.TryParse(Celsius.Text, out double C)) +``` + +5. Within the `if` conditional clause, add this code to apply the conversion formula: + +```cs +{ + var F = C * (9d / 5d) + 32; +``` + +6. Add this code to print the output as text to the Fahrenheit text box, then close the conditional clause: + +```cs + Fahrenheit.Text = F.ToString("0.0"); +} +``` + +7. Add an `else` conditional clause to reset the text boxes to 0 in case of invalid input: + +```cs +else +{ + Celsius.Text = "0"; + Fahrenheit.Text = "0"; +} +``` + +8. Your completed event handler should look like this: + +```cs +private void Button_OnClick(object? sender, RoutedEventArgs e) +{ + if (double.TryParse(Celsius.Text, out double C)) + { + var F = C * (9d / 5d) + 32; + Fahrenheit.Text = F.ToString("0.0"); + } + else + { + Celsius.Text = "0"; + Fahrenheit.Text = "0"; + } +} +``` + +## Checking your work + +1. Run your **GetStartedApp**. +2. Input the following numbers into the Celsius box, then click **Calculate**. Confirm the app returns the correct Fahrenheit values: + +| **Celsius** | **Fahrenheit** | +| --- | --- | +| -10 | 14.0 | +| 0 | 32.0 | +| 10 | 50.0 | +| 21 | 69.8 | +| 32.0 | 89.6 | + +3. Input something into the Fahrenheit box without altering the Celsius box. Confirm the app reverts the text in the Fahrenheit box to the formula output of the given Celsius value. +4. Input “abc” into the Celsius box. Confirm the app resets both text boxes to 0. + +:::note +Does it seem odd you that the app has the option to input numbers into the Fahrenheit box, but won’t convert them to Celsius? Not to worry—we’re going to make that box read-only in the exercises, coming up next. +::: + +Congratulations! You have created a temperature converter app using Avalonia. More importantly, you now have a solid foundation in the basics of the Avalonia framework. + +You can get to work developing your own apps now, if you wish. + +Or, to test your knowledge on three short exercises, proceed to the last page of this tutorial. + +
+
+
diff --git a/docs/get-started/starter-tutorial/customizing-the-avalonia-window.mdx b/docs/get-started/starter-tutorial/customizing-the-avalonia-window.mdx new file mode 100644 index 000000000..5112ba320 --- /dev/null +++ b/docs/get-started/starter-tutorial/customizing-the-avalonia-window.mdx @@ -0,0 +1,101 @@ +--- +id: customizing-the-avalonia-window +title: Customizing the Avalonia window +description: How to adjust the attributes of the Avalonia window. Part 4 of 7 the Avalonia starter tutorial. +--- + +import AvaloniaWindowLayout from '/img/get-started/avalonia-window-layout.png'; +import TemperatureConverterPortraitOrientation from '/img/get-started/temperature-converter-portrait-orientation.png'; + +# Customizing the Avalonia window + +As [mentioned earlier](/docs/get-started/starter-tutorial), the Avalonia window is where your app will run on the target platform. In a .axaml file, the `...` tag defines what goes in the window. + +## Layout zones + +Avalonia windows have four layout zones: (1) Margin, (2) Border, (3) Padding, and (4) Content. + +Some layout controls, like `StackPanel` which you used earlier, create smaller sub-windows with the same four layout zones. + +:::note +Each Avalonia window only accepts one control in its content zone. +::: + +
+A diagram displaying four overlapping, rectangular areas, representing the Avalonia window. +
+ +## Default window attributes + +Like controls, windows in Avalonia have XML attributes that define their presentation and behavior. + +Scroll to the top of the file **MainWindow.axaml** and examine the `` opening tag. You can see that it contains some default settings. + +```xml + +``` + +:::note +The first few lines of the `` tag declare the XML namespaces used by Avalonia. We use the namespace aliases `x`, `vm`, `d` and `mc`. +::: + +## Setting the window's attributes + +Let’s try adjusting the window’s dimensions so that it appears to be in portrait orientation, instead of landscape. This is something you may do when developing an app, to check how it looks on a mobile device. + +### Setting the previewer size + +Adjusting the values in `d:DesignWidth` and `d:DesignHeight` allows you to change the dimensions of the app in the live previewer, without affecting the actual dimensions of the window at runtime. + +1. In the file **MainWindow.axaml**, locate this line within the `...` tag: + +```xml +mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" +``` + +2. Set `d:DesignWidth` to 400. +3. Observe the previewer. You should see that the preview now resembles a mobile display. + +
+A screenshot of an app running in a window in portrait orientation. +
+ +### Setting the runtime window size + +To adjust to dimensions of the runtime app window, you need to add `Width` and `Height` attributes to the `` opening tag. + +1. In the file **MainWindow.axaml**, go to the bottom line of the `` opening tag: + +```xml +Title="GetStartedApp"> +``` + +2. Add the attributes `Width="400"` and `Height="450"` under that line. Remember to move the angle bracket to the end. + +```xml +Title="GetStartedApp" +Width="400" +Height="450"> +``` + +3. Run the app. You should see that the app window now resembles a mobile display. + +:::note +Most mobile platforms actually ignore window sizes and automatically resize windows to fit the screen. When developing, you won’t literally need to set a separate size for each target platform. +::: + +On the next page of this tutorial, you will learn how to create event handling so that the button responds to clicks. + +
+
+
diff --git a/docs/get-started/starter-tutorial/establishing-events-and-responses.mdx b/docs/get-started/starter-tutorial/establishing-events-and-responses.mdx new file mode 100644 index 000000000..6ddc86db2 --- /dev/null +++ b/docs/get-started/starter-tutorial/establishing-events-and-responses.mdx @@ -0,0 +1,106 @@ +--- +id: establishing-events-and-responses +title: Establishing events and responses +description: How to create an event handler to detect when the button is being clicked. Part 5 of 7 the Avalonia starter tutorial. +--- + +import MainWindowCodeBehindLocation from '/img/get-started/mainwindow-codebehind-location.png'; +import RiderRunDebugMode from '/img/get-started/rider-run-debug-mode.png'; +import VsDebugOutputClick from '/img/get-started/vs-debug-output-click.png'; + +# Establishing events and responses + +Our temperature converter app now looks reasonably functional, but doesn’t do anything yet. The next thing we need is for the **Calculate** button to be able to respond to a user action, such as a click. + +## Creating an event handler with code-behind + +XAML files can be associated with a C# source file. This file is where we can code event handling for the button. We call this “code-behind”. + +1. In your IDE, browse your project directory for **Views → MainWindow.axaml → MainWindow.axaml.cs**. This is the C# source file behind the main window XAML. + +
+A screenshot showing the location of the main window's code-behind file in a file tree. +
+ +2. Open **MainWindow.axaml.cs**. +3. Locate the `using` directives at the top of the file. At this point, there should only be the single line `using Avalonia.Controls;`. Add these two `using` directives: + +```cs +using Avalonia.Interactivity; +using System.Diagnostics; +``` + +4. Locate the line `public partial class MainWindow : Window` further down the file. This class currently contains only the constructor for the main window, `public MainWindow()`. Below the constructor, add the following code: + +```cs +private void Button_OnClick(object? sender, RoutedEventArgs e) +{ + Debug.WriteLine("Click!"); +} +``` + +5. Your C# file should now look like this: + +```cs +using Avalonia.Controls; +using Avalonia.Interactivity; +using System.Diagnostics; + +namespace GetStartedApp.Views; + +public partial class MainWindow : Window +{ + public MainWindow() + { + InitializeComponent(); + } + + private void Button_OnClick(object? sender, RoutedEventArgs e) + { + Debug.WriteLine("Click!"); + } +} +``` + +6. Switch to your XAML file, **MainWindow.axaml**. +7. Locate ` +``` + +## Checking the event handler works + +To verify we’ve created the event handler correctly, we can check the debug output to confirm "Click!" is being printed when we click the **Calculate** button. + + + + 1. Run GetStartedApp in debug mode. + +
+ A screenshot showing the location of the button to run a project in debug mode in Rider. +
+ + 2. Open the **Debug Output** tab in the bottom panel. + 3. In the running app window, click the **Calculate** button a few times. + 4. You should see “Click!” being printed in the debug output in Rider. +
+ + 1. Go to the **Output** window, located by default under the split view. From the “Show output from:” dropdown menu, select **Debug**. + 2. Run the app. + 3. In the running app window, click the **Calculate** button a few times. + 4. You should see “Click!” printed in the output window. + +
+ A screenshot showing the output screen in Visual Studio, with the word 'Click!' p[rinted inside. +
+
+
+ +On the next page, you will learn how to implement the formula that converts temperatures from Celsius to Fahrenheit. + +
+
+
diff --git a/docs/get-started/starter-tutorial/exercises.mdx b/docs/get-started/starter-tutorial/exercises.mdx new file mode 100644 index 000000000..b2eff58d3 --- /dev/null +++ b/docs/get-started/starter-tutorial/exercises.mdx @@ -0,0 +1,114 @@ +--- +id: exercises +title: Exercises +description: Test your knowledge of Avalonia on three coding exercises. Part 7 of 7 the Avalonia starter tutorial. +--- + +# Starter exercises + +Now that you have built a temperature converter app, try these three exercises to test your understanding of Avalonia. + +## Exercise 1: Change an existing attribute + +**Challenge level: ★** + +Make the gridlines invisible in **GetStartedApp**. + +
+ Hint + + You specified `` in MainWindow.axaml. +
+ +
+ Solution + + In **MainWindow.axaml**, locate the `` opening tag. Change the `ShowGridLines` attribute to `False`. + + ```xml + + ``` +
+ +## Exercise 2: Add a new attribute + +**Challenge level: ★★** + +Make it impossible for a user to input text into the Fahrenheit text box in **GetStartedApp**. + +
+ Hint (1st) + + Check the [API docs](https://api-docs.avaloniaui.net/) for more information on the `TextBox` control. +
+ +
+ Hint (2nd) + + Under the [API reference for `TextBox`](https://api-docs.avaloniaui.net/docs/T_Avalonia_Controls_TextBox), you’ll find the attribute `IsReadOnly`. +
+ +
+ Solution + + In **MainWindow.axaml**, locate the `` tag for the Fahrenheit box. Add the `IsReadOnly` attribute, and set it to `True`. + + ```xml + + ``` +
+ +## Exercise 3: Program a new event response + +**Challenge level: ★★★** + +Make **GetStartedApp** calculate the temperature conversion as the user types. + +
+ Hint (1st) + + Check the [API docs](https://api-docs.avaloniaui.net/) for more information on the `TextBox` control. +
+ +
+ Hint (2nd) + + Under the [API reference for `TextBox`](https://api-docs.avaloniaui.net/docs/T_Avalonia_Controls_TextBox), you’ll find the event `TextChanged`. +
+ +
+ Hint (3rd) + + Your event handler is defined in the C# code-behind MainWindow.axaml.cs. It is also referenced by the XAML file MainWindow.axaml. +
+ +
+ Solution + + 1. In **MainWindow.axaml**, locate the `` tag for the Celsius box. Add the `TextChanged` event, and give the event a name, e.g. `Celsius_TextChanged`. + + ```xml + + ``` + 2. In **MainWindow.axaml**, delete the entire line starting ``~~ + + 3. In **MainWindow.axaml.cs**, locate the event handler line starting `private void`. Change the event name from `Button_OnClick` to whatever you named it in the XAML file, e.g. `Celsius_TextChanged`. + + ```xml + private void Celsius_TextChanged(object? sender, RoutedEventArgs e) + ``` + + 4. Run the app to confirm that the value in the Fahrenheit box changes as you type in the Celsius box. +
+ +Congratulations! You have completed this starter tutorial for Avalonia! + +## Further reading + +[Controls reference](/docs/reference/controls) + +[API documentation](https://api-docs.avaloniaui.net/) diff --git a/docs/get-started/starter-tutorial/index.mdx b/docs/get-started/starter-tutorial/index.mdx new file mode 100644 index 000000000..504640d3d --- /dev/null +++ b/docs/get-started/starter-tutorial/index.mdx @@ -0,0 +1,50 @@ +--- +id: index +title: Starter tutorial +description: Learn the basics of Avalonia by building a temperature converter app. Part 1 of 7 the Avalonia starter tutorial. +--- + +import DocCardList from '@theme/DocCardList'; +import TemperatureConverterComplete from '/img/get-started/temperature-converter-complete.png'; +import MainWindowFileLocation from '/img/get-started/mainwindow-file-location.png'; + +# Starter tutorial + +## Build a temperature converter app + +Now that you’re set up with an Avalonia project in your integrated development environment (IDE), as covered on the [Getting Started page](/docs/get-started#creating-your-first-project), we can go through some basic concepts and functionalities in Avalonia. We’re going to do that by turning the default Avalonia template into a temperature converter app. + +Follow through this tutorial to create the app. As you do so, you will learn about: + + + +
+A screenshot of a completed app that converts temperatures from Celsius to Fahrenheit. +
+ +## .axaml + +Notice the files in your project directory ending `.axaml`? That’s short for Avalonia XAML, a file extension unique to Avalonia that differentiates Avalonia files from standard XAML files. + +## Check your XAML previewer works + +If you have newly installed Avalonia in Visual Studio or JetBrains Rider, and you are new to IDE development in general, this may be a good time to check you can use the XAML previewer. + +See the [XAML previewers page](/docs/get-started/xaml-previewers) for how to enable and test the previewer. + +## Open the main window file + +In the **Views** folder of your project directory, open the file **MainWindow.axaml**. We will mainly be working on this file throughout this tutorial. + +
+A screenshot showing the location of the main window file in a file tree. +
+ +Nearly everything in **MainWindow.axaml** goes between the `...` XAML tag. This tag represents the Avalonia window, where your app will run on the target platform. We’ll look at Avalonia windows in more detail later, when we get to [customizing the Avalonia window](/docs/get-started/starter-tutorial/customizing-the-avalonia-window). + +Proceed to the next page of this tutorial to learn how to add a button to the app. + +
+
+
diff --git a/docs/get-started/test-drive/add-a-control.md b/docs/get-started/test-drive/add-a-control.md deleted file mode 100644 index ffb586be9..000000000 --- a/docs/get-started/test-drive/add-a-control.md +++ /dev/null @@ -1,105 +0,0 @@ ---- -id: add-a-control -title: Add a Control ---- - -import Highlight from '@site/src/components/Highlight'; -import CalculateButton from '/img/get-started/test-drive/calculate-button.png'; -import ButtonIntellisenseScreenshot from '/img/get-started/test-drive/button-intellisense.png'; - -So far, the main window of your application displays only a text string. On this page, you will learn how to add some of the built-in controls that are part of Avalonia. - -## Button - -Avalonia contains a built-in control that creates a button. Follow this procedure to replace the text string currently in the `Window`'s content zone with a button control. - -- Stop the app if it is running. -- Locate the highlighted line of XAML in the `MainWindow.xaml` file. -```xml title='XAML' - - - - - - - - // highlight-next-line - - - -``` - -- Replace the entire line with the following: -```xml title='XAML' - -``` -- Your XAML should now look like this: -```xml title='XAML' - - - - - - - - // highlight-next-line - - -``` - -:::tip -If you're using the previewer, you will see the button appear in the preview pane as soon as the XAML is valid. You can -also try hovering and clicking the `Button` to see it change appearance in different states. -::: - -- Run the app to confirm that the presentation and behaviour of the button is the same at runtime. - -## Control Attributes - -XAML uses XML attributes to specify presentation and behavior for controls. These attributes can set properties, call -methods, and call event handlers in the controls created by the XAML. - -For example, the `Button` is currently positioned hard against the left edge of the `Window`. This is a result -of the default value (left) of its `HorizontalAlignment` property. Follow this -procedure to set the `HorizontalAlignment` to centered instead. - -- Add a new attribute to the Button tag as follows: - -```xml title='XAML' - -``` - -:::tip -If you're using an IDE, notice how the Avalonia code completion guides you as you add attributes to the XAML. - - -::: - -The `Button` should now move to the center of the window content zone. Horizontally because of the change and vertically -because of the Button's default. - -:::info -For full information about the complete range of Avalonia UI built-in controls, and their attributes, see the reference section [here](../../reference/controls). -::: - -On the next page, you will learn how to create a more complex layout. diff --git a/docs/get-started/test-drive/add-some-layout.mdx b/docs/get-started/test-drive/add-some-layout.mdx deleted file mode 100644 index b23407d43..000000000 --- a/docs/get-started/test-drive/add-some-layout.mdx +++ /dev/null @@ -1,85 +0,0 @@ ---- -id: add-some-layout -title: Add Some Layout ---- - -import useBaseUrl from '@docusaurus/useBaseUrl'; -import StackPanelZonesDiagram from '/img/get-started/test-drive/stackpanel-zones.png'; - -Avalonia provides a range of built-in controls to help you layout the visual elements of an application. On this page, -you will see how to use some of these layout controls. - -At this stage, your application has a single button located in the content zone of the main window. - -In fact, an Avalonia `Window` allows only one control in its content zone. To show multiple visual elements, you -must use a layout control that allows multiple controls within its content zone. - -## StackPanel - -The `StackPanel` control lays out a sequence of controls in the order they are defined in XAML. By default, it lays out -in a vertical stack but this can be changed to horizontal with its `Orientation` property. - - - -```xml - - 1 - 2 - -``` - -## TextBlock - -The `TextBlock` control allows extensive styling of its contained text. - -To take the example forward, add a `StackPanel` as follows (include the preexisting `Button` XAML): - -```xml - - - - - - - -// highlight-start - - - - - - - - - // highlight-end - - -``` - - - -:::info -You can explore the other layout controls in Avalonia using the reference [here](../../reference/controls/layout-controls.md). -::: - -On the next page, you will add some inputs to the middle of the window. \ No newline at end of file diff --git a/docs/get-started/test-drive/code-with-controls.md b/docs/get-started/test-drive/code-with-controls.md deleted file mode 100644 index 303a09a42..000000000 --- a/docs/get-started/test-drive/code-with-controls.md +++ /dev/null @@ -1,99 +0,0 @@ ---- -id: code-with-controls -title: Code With Controls ---- - -In this section, you will implement the core logic to update the Fahrenheit temperature based on the Celsius input. - -## Control Names - -Avalonia creates objects for each control defined in the XAML hierarchy. Your code can access these controls at runtime, -but should be named for easy access. - -To add control names, follow this procedure: - -- Stop the app if it is running. -- Locate the `TextBox` for Celsius. -- Add the `Name` attribute like this: - -```xml - -``` - -- Repeat the above for the Fahrenheit input: - -```xml - -``` - -## Get Control Values in Code-Behind - -To access the `Text` value of the `celsius` input, follow this procedure: - -- Switch to the **MainWindow.axaml.cs** code-behind file. -- Locate the `Button_OnClick` event handler. -- Alter the `Debug` statement to display the text property of the `Celsius` input, like this: - -```csharp -Debug.WriteLine($"Click! Celsius={Celsius.Text}"); -``` - -- Run the app again (in debug mode) to confirm that you can see the value in the Celsius appear in the debug window. - -## Set Control Values in Code-Behind - -To use the simple formula that converts Celsius temperature to Fahrenheit, you will first need to ensure that the -Celsius input text converts to a number. The formula is then: - -``` -Tf = Tc * (9/5) + 32 -``` - -To add the conversion formula, follow this procedure: - -- Locate the `Button_OnClick` event handler. -- Validate the Celsius input text as a number. -- Use the conversion formula. -- Update the `Text` in the Fahrenheit input. -- Run the app to check your work. - -One implementation of the above is as follows: - -```csharp -private void Button_OnClick(object? sender, RoutedEventArgs e) -{ - if (double.TryParse(Celsius.Text, out double C)) - { - var F = C * (9d / 5d) + 32; - Fahrenheit.Text = F.ToString("0.0"); - } - else - { - Celsius.Text = "0"; - Fahrenheit.Text = "0"; - } -} -``` - -You can check your work using the following conversion table: - -| Celsius | Fahrenheit | -|---------|------------| -| -10 | 14.0 | -| 0 | 32.0 | -| 10 | 50.0 | -| 21 | 69.8 | -| 25 | 77.0 | -| 32 | 89.6 | - -### Exercises - -You have now used an event handler to get and set control properties at runtime. Try some of these exercises: - -- Stop showing the gridlines (easy). -- Stop the user from changing the text in the Fahrenheit input by setting the `IsReadOnly` attribute (easy). -- Calculate the conversion as the user types into the Celsius input using the `TextChanged` event (moderate). - -:::info -For full information about the complete range of Avalonia built-in controls, events and attributes, see the controls reference section [here](../../reference/controls/). -::: diff --git a/docs/get-started/test-drive/create-a-project.mdx b/docs/get-started/test-drive/create-a-project.mdx deleted file mode 100644 index d58841a37..000000000 --- a/docs/get-started/test-drive/create-a-project.mdx +++ /dev/null @@ -1,142 +0,0 @@ ---- -id: create-a-project -title: Create and Run a Project ---- - -import useBaseUrl from '@docusaurus/useBaseUrl'; - -import VsFindAvaloniaTemplateScreenshot from '/img/get-started/test-drive/vs-find-avalonia-template-screenshot.png'; -import VsNewAvaloniaProjectScreenshot from '/img/get-started/test-drive/vs-new-avalonia-project-screenshot.png'; -import RiderRunScreenshot from '/img/get-started/test-drive/rider-toolbar-run.png'; -import InitialWindowScreenshot from '/img/get-started/test-drive/initial-window.png'; - -import vscode1 from '/img/get-started/test-drive/vscode-command-new-project.png'; -import vscode2 from '/img/get-started/test-drive/vscode-select-project-template.png'; -import vscode3 from '/img/get-started/test-drive/vscode-name-new-project.png'; -import vscode4 from '/img/get-started/test-drive/vscode-create-project.png'; -import vscode6 from '/img/get-started/test-drive/vscode-select-csharp.png'; -import vscode7 from '/img/get-started/test-drive/vscode-launch-app.png'; -import vscode8 from '/img/get-started/test-drive/vscode-app-running.png'; - - -## Create the Project - -To get started, we're going to use the MVVM Avalonia template: `Avalonia MVVM Application` (or `avalonia.mvvm` in the CLI). - - - - -1. On the Rider startup screen, select **New Solution** to bring up the `New Solution` Wizard. If you have installed the [Avalonia Templates](../install.md), then you will see three options. - * **Avalonia .NET App:** A template for desktop apps (Windows, macOS & Linux) that uses code-behind rather than MVVM. - * **Avalonia .NET MVVM App:** A template for desktop apps (Windows, macOS & Linux) that uses MVVM (by default with RxUI). - * **Avalonia Cross-Platform Application:** A template for all supported platforms (Windows, macOS, Linux, iOS, Android and WASM). This template requires additional workloads. - -2. In the sidebar, scroll down and select `Avalonia .NET MVVM App` - -3. Type `GetStartedApp` in the **Solution Name** field -4. Click **Create** - -The template will create a new solution and project. - - - - - -- In **Visual Studio**, click **Create a new project**. -- Type `Avalonia` in the search box. -- Click **Avalonia Application** then click **Next**. - - - -- Name the project `GetStartedApp`, and click **Create**. - -- The next screen allows selecting the platforms you wish to target: click **Desktop** then click **Next**. - -- The next screen allows selecting a design pattern: click **ReactiveUI** then click **Create**. - -The template will create a new solution and two new projects. `GetStartedApp` is the main project that is shared between each platform. `GetStartedApp.Desktop` is the platform-specific project for the desktop platform. - - - - - - * Bring up the Command Palette using `⇧ ⌘ P` and then type ".NET" and find and select the **.NET: New Project** command. - - * After selecting the command, you'll need to choose the project template. Choose **Avalonia MVVM app**. - - * Name the project `GetStartedApp`, and press enter. - - * You'll need to provide a path for where the project should be created. Do this, and then press **Create project** - - - -Run the command: - -```bash title='Bash' -dotnet new avalonia.mvvm -o GetStartedApp -``` - -This will create a new folder called `GetStartedApp` containing the new project. - - - -## Run the Project - -We're now ready to run the project! - - - - -Press the **Run** button in the Rider toolbar: - - - - - - - Right-click on the `GetStartedApp.Desktop` project and select **Set as Startup Project**. - - Hit `F5` to run the project. - - - - * Hit `F5` to run the project and Select `C#` as the debugger - - * Select **C#: GetStartedApp Demo** to launch the application with the debugger connected. - - - -Go into the `GetStartedApp` directory and run: - -```bash title='Bash' -dotnet run -``` - - - -The solution will build and run. - -You should now be running your first Avalonia application! - - diff --git a/docs/get-started/test-drive/index.md b/docs/get-started/test-drive/index.md deleted file mode 100644 index 1e0ad1c3c..000000000 --- a/docs/get-started/test-drive/index.md +++ /dev/null @@ -1,6 +0,0 @@ -# Test Drive - -import {DocsCardList} from '../../../src/components/DocsCard'; -import {useCurrentSidebarCategory} from '@docusaurus/theme-common'; - - diff --git a/docs/get-started/test-drive/input-controls.md b/docs/get-started/test-drive/input-controls.md deleted file mode 100644 index f605e5238..000000000 --- a/docs/get-started/test-drive/input-controls.md +++ /dev/null @@ -1,90 +0,0 @@ ---- -id: input-controls -title: Input Controls ---- -import useBaseUrl from '@docusaurus/useBaseUrl'; - -On this page, you will learn how to add input controls and arrange them in a neatly aligned layout. The aim is to add -numerical inputs with labels and an output control following in the row below. - -To achieve this layout, you will use the built-in `Grid` control to create cells and assign our controls to those cells. - -The following picture shows the finished application at runtime with the gridlines showing for layout visualization purposes. Normally, -these are invisible on a production UI. - - - -To create a layout using the `Grid` control with 2 columns and 3 rows, follow this procedure: - -- Stop the app if it is running. -- Locate the empty line in the XAML between `` and ` - -``` - -This assigns the number of rows and columns, their sizes, and makes the gridlines visible. Currently, it will show as a -straight line because the grid cells are empty. The `Auto` rows size to their content and will have zero height until -content is added. - -- Add `