C#Csharp演示代码编译

第一部分:基础程序

Csharp演示代码编译
一、新建一个txt文件直接修改后缀

image

二、写一个基础代码

"hello.cs"和所有代码一样,首先需要一个函数入口:

using System;

namespace MySpace
{ 
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello w");
            Console.ReadKey();
        }
    }
}

三、C#编译器

编译C#代码需要一个编译器,最基本C#编译器即是这个csc.exe。

image

双击打开这个执行文件是不行的,我们需要使用dot窗口结合需要被编译的代码去使用才行。
指令如下:
按顺序输入分别为“编译器”+“编译文件路径”+“关键字/out”+“编译输出路径”

(注意转义字符需要+/)

image

最后双击打开你的程序即可,完美运行
image

第二部分:visual studio

学习C#的时候我们使用一些集成工具,初次打开需要勾选下载一些组件
image
创建一个WPF窗口
image
此时我们可以观察我们的文件目录,其中的“WpfApp_solution.sln”文件指向的是“WpfApp1.csproj”,通过这个文件完成了我们的项目是由什么格式打开。
image
image
(划重点)回到我们的基础界面,可以看到右边的资源管理器中只有一个MainWindow.xaml,当项目运行是固定是由APP.xaml去控制主要打开窗口的。
image

第三部分:XAML基本布局

WPF使用的页面和网页制作有异曲同工之妙,也是需要分割一层层的盒子去套起来(示例)

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        Title="MainWindow" Height="450" Width="800">
    
    <Grid ShowGridLines="True"><!--定义分割线-->

        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition><!--上下分割线-->
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
            <Button Height="20" Width="80" Content="TEST1" />
            <Button Grid.Row="1" Grid.Column="1" Content="TEST2" />
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition><!--左右分割线-->
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
    </Grid>
</Window>

image
全局样式设置方法

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources><!--全局样式-->
        <Style TargetType="Button">
            <Setter Property="Background"  Value="Red"/>
            <Setter Property="FontSize"  Value="25"/>
        </Style>
    </Window.Resources>
    
    <Grid ShowGridLines="True"><!--定义分割线-->

        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition><!--上下分割线-->
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
            <Button Height="20" Width="80" Content="TEST1" />
            <Button Grid.Row="1" Grid.Column="1" Content="TEST2" />
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition><!--左右分割线-->
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
    </Grid>
</Window>

image

第三部分:XAML基本布局二

颜色静态资源使用方法如下:
image

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources><!--全局样式-->
        <Style x:Key="LoginStyle" TargetType="Button">
            <Setter Property="Background"  Value="Green"/>
            <Setter Property="FontSize"  Value="25"/>
            <Setter Property="Height"  Value="50"/>
            <Setter Property="Width"  Value="500"/>
        </Style>
    </Window.Resources>
    
    <StackPanel>
        <Button Style="{StaticResource LoginStyle}" Content="TEST" />
    </StackPanel>

</Window>

posted @ 2024-11-26 11:04  周半仙  阅读(444)  评论(0)    收藏  举报