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

相关推荐
编程猪猪侠14 分钟前
VSCode如何修改默认扩展路径和用户文件夹目录到其他盘以及微信开发工具如何修改扩展路径到其他盘
ide·vscode·编辑器
哈哈幸运2 小时前
Linux Sed 深度解析:从日志清洗到 K8s 等12个高频场景
linux·运维·编辑器·sed
姜太小白2 小时前
【VSCode】VS Code自动换行设置方法
ide·vscode·编辑器
无心水4 小时前
基础服务系列-Jupyter Notebook 支持Java
ide·python·jupyter
周杰伦_Jay5 小时前
continue插件实现IDEA接入本地离线部署的deepseek等大模型
java·数据结构·ide·人工智能·算法·数据挖掘·intellij-idea
●^●6 小时前
Linux 命令行与 vi/vim 编辑器完全指南
linux·编辑器·vim
未来之窗软件服务7 小时前
声音分离人声和配乐-从头设计数字生命第5课, demucs——仙盟创梦IDE
ide·macos·xcode·仙盟创梦ide·数字生命
Kusunoki_D8 小时前
使用 VSCode 编写 Markdown 文件
vscode·编辑器·markdown
綦枫Maple8 小时前
Vue实战(08)解决 Vue 项目中路径别名 `@` 在 IDE 中报错无法识别的问题
前端·ide·vue.js
隐-梵10 小时前
Android studio进阶开发(四)--okhttp的网络通信的使用
android·ide·okhttp·android studio