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

相关推荐
界面开发小八哥6 小时前
通用CI/CD软件平台TeamCity v2025.3全新发布——主要界面交互体验升级
ide·ci/cd·开发平台·teamcity
张乔2410 小时前
mybatisX的自定义模板生成
java·ide·intellij-idea
taoismimortal11 小时前
vscode目录,右键菜单加入用VSCode打开文件和文件夹(快速解决)(含删除)(脚本)
ide·vscode·编辑器
猫咪的白手套12 小时前
解决VSCode中“#include错误,请更新includePath“问题
ide·vscode·编辑器
抠头专注python环境配置15 小时前
Pycharm、Python安装及配置小白教程
ide·python·pycharm
天安彩15 小时前
mac下 vscode 运行 c++无法弹出窗口
c++·vscode·macos·clang
Ann's Blog16 小时前
pycharm配conda环境
ide·pycharm·conda
白日与明月17 小时前
Hive-vscode-snippets
hive·hadoop·vscode
橘子味的茶二19 小时前
vsCode如何远程服务器不需要每次输入密码
服务器·ide·vscode
鑫宇吖21 小时前
IAR编辑器如何让左侧的工具栏显示出来?
编辑器·嵌入式·c·iar