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

相关推荐
hong1616881 小时前
PhpStorm中配置调试功能
android·ide·phpstorm
bitter️4 小时前
vue2+ueditor集成秀米编辑器
编辑器
daxiongdedlam5 小时前
免费的文章生成器有哪些?盘点5款为你自动生成文章
人工智能·编辑器·ai写作·媒体
什么都没学会6 小时前
GO环境安装
开发语言·vscode·golang·安装
MuseLss7 小时前
解决Idea 配置文件无法提醒的功能
java·ide·intellij-idea
界面开发小八哥8 小时前
「Java开发指南」如何用MyEclipse搭建Adobe和Spring Flex?(一)
java·ide·spring·myeclipse·java开发
Python资讯站11 小时前
【PyCharm使用教程】PyCharm的基本使用教程,适合完全零基础,小白快速上手!(Python+PyCharm安装包)
开发语言·数据结构·ide·python·学习·pycharm·python学习
Days205012 小时前
ueditorplus百度编辑器集成秀米及135编辑器
编辑器
BY—-组态13 小时前
国内web组态推荐
前端·物联网·数学建模·编辑器·html·工业互联网
vvvae123415 小时前
Arduino IDE安装操作指南
ide