vscode开发avalonia

安装

安装.net 8

安装avalonia模板

bash 复制代码
dotnet new install Avalonia.Templates

创建项目

bash 复制代码
dotnet new avalonia.app -o GetStartedApp

安装c# dev kit插件和Avalonia for VSCode Community




dotnet run运行

修改代码

MainWindow.axaml

xml 复制代码
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450"
        x:Class="GetStartedApp.MainWindow"
        Title="GetStartedApp">
    <TextBlock Text="My Text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Window>

继续修改代码

xml 复制代码
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450"
        x:Class="GetStartedApp.MainWindow"
        Title="GetStartedApp">
     <Button HorizontalAlignment="Center">Calculate</Button>
</Window>

增加布局

xml 复制代码
<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450"
        x:Class="GetStartedApp.MainWindow"
        Title="GetStartedApp">
  <StackPanel>
    <Border Margin="5" 
        CornerRadius="10"
        Background="LightBlue">
        <TextBlock Margin="5"
          FontSize="24" 
          HorizontalAlignment="Center"
          Text="温度转换器">
        </TextBlock>
    </Border>
    <Grid ShowGridLines="True"  Margin="5" 
      ColumnDefinitions="120, 100" 
      RowDefinitions="Auto, Auto, Auto">
        <Label Grid.Row="0" Grid.Column="0" Margin="10">Celsius</Label>
        <TextBox Name="celsius" Grid.Row="0" Grid.Column="1" Margin="0 5" Text="0"/>
        <Label Grid.Row="1" Grid.Column="0" Margin="10">Fahrenheit</Label>
        <TextBox Name="fahrenheit" Grid.Row="1"  Grid.Column="1" Margin="0 5" Text="0"/>
        <Button Grid.Row="2" Grid.Column="1" Margin="0 5" Click="ButtonClicked">Calculate</Button>
    </Grid>  
  </StackPanel>
</Window>

绑定点击事件,修改MainWindow.axaml.cs

cs 复制代码
public void ButtonClicked(object source, RoutedEventArgs args)
{
   Debug.WriteLine("Click!");
   Debug.WriteLine($"Click! Celsius={celsius.Text}");
   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";
   }
}

参考

https://docs.avaloniaui.net/zh-Hans/docs/welcome

相关推荐
❀͜͡傀儡师4 分钟前
GitHub Copilot for VS Code 中文使用完整教程
vscode·github·copilot
白头小黄30 分钟前
ESP32+VScode+PIO实现基础的自带USB接口的JTAG调试
ide·vscode·编辑器
数字供应链安全产品选型1 小时前
2026 AI安全左移再进化:从IDE插件到CI门禁,悬镜灵境AIDR的全流程集成实践
ide·人工智能·安全
Fleshy数模2 小时前
解决 PaddleOCR 库冲突:PyCharm 虚拟环境搭建 + 完整 OCR 实战教程
ide·pycharm·ocr
凤年徐2 小时前
Vim编辑器使用详解:多模式、常用命令与配置技巧
linux·编辑器·vim
ILYT NCTR2 小时前
vscode配置django环境并创建django项目(全图文操作)
vscode·django·sqlite
2501_915918412 小时前
快蝎iOS开发IDE:免Xcode开发,支持Swift/Flutter项目
ide·vscode·ios·个人开发·xcode·swift·敏捷流程
Navicat中国2 小时前
Navicat 视频简介 | 数据管理之数据编辑器
数据库·编辑器·navicat·数据管理
三品吉他手会点灯17 小时前
STM32 VSCode 开发-C/C++的环境配置中,找不到C/C++: Edit Configurations选项
c语言·c++·vscode·stm32·单片机·嵌入式硬件·编辑器
小堃学编程21 小时前
【项目实战】基于protobuf的发布订阅式消息队列(4)—— 服务端
c语言·c++·vscode·消息队列·gtest·protobuf·muduo