zoukankan      html  css  js  c++  java
  • c#学习笔记

    笔记是给我自己看的可能有些地方会描述错误。不定期更新!本来想设为私有的不公开,貌似没得设置



    1.对一段代码折叠(即代码开头多了个小+号,可以折叠,展开)

    #region "折叠后显示的名字" //放在需要折叠代码头部
    #endregion //放在需要折叠代码的末尾


    把一个字符串转换成枚举类型:
    (枚举类型)(Enum.Parse(typeof(枚举类型),"待转换的字符串")); //所有括号也要复制进去


    2.c#中函数返回多个值方法:


    1)在函数参数类型前加out,函数往外传值。(out注重传出,单向传值)
    例如:
    static Bool Max(out int m) //这里m其实会接收num的值变为0,但由于m加了out要进行赋值才能使用所以这里的传参没有意义
    {
    m=10; //这里的m必须进行初始化或者赋值才能使用,不能直接返回或者进行使用。
    return 0;
    }


    int num=0;
    Max(out num); //注意这里out不能漏掉。表示num不是用来传入值的。这里不是num传值给函数,而是函数m返回值给num


    2)在函数参数类型前加ref,地址传值。(ref可传入传出,双向传值)
    用法和out类似,只是上例的m不用赋值或初始化才能用,而是可以直接接收num的值使用。m改变num也随之改变




    out,ref相当于快捷方式和原文件
    普通函数传值,相当于文件和文件的复制本






    3.声明数组并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:


    (1)一维数组:


    int[] numbers = new int[5];


    (2)多维数组:


    string[,] names = new string[5,4];


    (3)数组的数组(交错的):


    byte[][] scores = new byte[5][];
    for (int x = 0; x < scores.Length; x++) 
    {
       scores[x] = new byte[4];
    }










    3.面向对象--属性
    class Person 

    private int age; 
    public int Age //这里Age不是用来存值,值都存在age中。

    set //赋值
    {
    if(value<0)
    {
    return;
    }
    this.age=value;

    get //取值
    {
    return this.age;






    4.代码发生异常处理
    try
    {
    //这里填写可能出错代码。例如:
    int a = Convert.ToInt32(Console.ReadLine()); //这里接收的字符串若不是纯数字程序则会出现错误,若出现错误之后的代码不执行。跳到catch中执行代码
    }
    catch //这里catch可以加一个参数Exception ex,ex可以用来接收错误信息。ex.Message存放错误信息。ex.StackTrace存放异常堆栈
    {
    //这里填写出现错误后执行的代码
    }




    函数的异常信息可以自己定义,即ex.Message
    throw new Exception("自定义异常信息"); //throw扔,catch抓。出现异常的时候已经new好了一个Exception对象扔出来,我们catch抓住即可














    5.c#中的链表---集合
    定义方法
    List<数据类型> 集合名字 = new List<数据类型>();
    例如:
    List<int> num = new List<int>();


    往里面添加元素:
    集合名字.Add(添加的内容); //添加的内容数据类型一定要相同


    例如:
    num.Add(1234);


    查看元素个数:
    集合名字.Count();


    移除某个元素
    集合名字.Remove(移除的内容);


    清空集合
    集合名字.Clear();




    6.foreach遍历数组,集合
    foreach(数据类型 接收变量名字 in 数组或集合名字)
    {
    //内容自己写,举个例子
    Console.WriteLine(接收变量名字.ToString);
    }


    7.可空数据类型和不可空数据类型
    可空(null)数据类型:引用类型,自定义类型,string
    不可空:值类型。int,bool,decimal,datetime等。


    例如:
    string s = ""; //字符串空,但不是null。有长度,为0
    string s = null //null表示没有指向,没有长度
    int i = null; //错误,int的值不能为null
    int? i = null;  //正确,在不可空数据类型后加?可以为null


    int?转换为int需要强制转换
    int? i=1;
    int i1 = (int)i;






    8.控件通用属性:
    Visibility控件是否可见----Visible表示可见,Collapsed不可见
    IsEnabled控件是否可用:bool类型
    Background背景色
    FontSize字体大小


    TextBox:MaxLength可输入的最大字符数;TextWrapping是否自动换行(Wrap自动换行,NoWrap不自动换行)IsReadOnly="True"("False")只读不能写入
    PasswordBox密码框,Password属性为密码
    CheckBox复选框:IsChecked:是否选中,bool? //True选中,False没选,空
    RadioButton单选框,同组只能选一个:GroupName="组名";//同一组RadioButton,只能选一个
    DatePicker日期选择:DateTime? value = datePicker1.SelectedDate; //SelectedDate选定的日期,可能为null
    DateTime.Now当前时间(含小时分秒部分) DateTime.Today今天时间
    image图片控件:Source属性为图片地址,相对路径
    ProgressBar进度条:IsIndeterminate是否不确定模式("true","false");Maximum最大值;Minimum最小值,Value当前值
    if(value==null)
    {
    MessageBox.Show("请选择日期");
    }
    else
    {
    MessageBox.Show(value.ToString);
    }










    9.基本布局
    类型:System.Windows.VerticalAlignment一个垂直对齐设置
    类型:System.Windows.HorizontalAlignment一个水平对齐设置




    Grid:
    跨越行使用Grid.RowSpan属性,跨越列使用Grid.ColumnSpan属性
    Row行,Column列
       <Grid.RowDefinitions>  
           <RowDefinition Height="25"></RowDefinition>  
           <RowDefinition></RowDefinition>  
        </Grid.RowDefinitions> 


        <Grid.ColumnDefinitions>  
           <ColumnDefinition Width="150"></ColumnDefinition>  
           <ColumnDefinition Width="Auto"></ColumnDefinition>  
           <ColumnDefinition></ColumnDefinition>  
        </Grid.ColumnDefinitions> 


    控件布局:
    1. <Grid>  
    2.    <Grid.RowDefinitions>  
    3.        <RowDefinition Height="25"></RowDefinition>  
    4.        <RowDefinition></RowDefinition>  
    5.    </Grid.RowDefinitions>  
    6.    <Grid.ColumnDefinitions>  
    7.        <ColumnDefinition Width="150"></ColumnDefinition>  
    8.        <ColumnDefinition Width="Auto"></ColumnDefinition>  
    9.        <ColumnDefinition></ColumnDefinition>  
    10.    </Grid.ColumnDefinitions>  
    11.    <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="Black" />  
    12.    <TextBox Grid.Row="1" Grid.Column="0" BorderBrush="Black" />  
    13.    <GridSplitter Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="5" Background="Gray" ShowsPreview="True"></GridSplitter>  
    14.    <TextBox Grid.Row="1" Grid.Column="2" BorderBrush="Black" />  
    15. </Grid>  






    DockPanel布局:(通常用于置顶,置底,置左,置右)
    举例:菜单置顶
    <DockPanel>
    <Menu DockPanel.Dock="Top">
    <MenuItem Header="File">
    <MenuItem Header="Open">
    </MenuItem>
    </MenuItem>
    </Menu>
    </DockPanel>










    10.窗口的属性(Xaml)
    1.修改标题:修改windows的title属性
    2.不想修够窗口的大小:ResizeMode="NoReSize"
    3.窗口打开的时候在屏幕正中央: WindowStartupLocation="CenterScreen"
    4.窗口打开时的状态(最大化):WindowState="Maxmized"
    5.去除窗口本身自带的最大化,最小化,关闭:WindowStyle:None
    6.获取或设置一个值窗口的工作区是否支持透明度。:AllowsTransparency
    7.任务栏不显示图标:ShowInTaskbar=false;
    8.总在最前 Topmost=true




    11.多窗口
    右键解决方案-添加-窗口
    在需要调用窗口的地方加上代码:
    这里假设新建一个窗口叫Window1,其实就是新建了一个窗口类Window1
    Window1 w1 = new Window1();
    w1.ShowDialog();






    12.打开,保存对话框
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "文本文件|*.txt|图片|*.jpg"; //打开过滤器
    ofd..Multiselect=true; //允许同时选择多个文件
    if(ofd.ShowDialog()==true)
    {
    //调出打开对话框,打开后执行
    //打开文件的路径存放在ofd.FileName
    }
    else
    {
    //调出打开对话框,选中取消后执行
    }




    打开多个文件实例:
    private void Form1_Load(object sender, System.EventArgs e)
      {
       openFileDialog1.Multiselect=true;//允许同时选择多个文件
      }
            string[] picArr;//选择多张图片路径保存到数组
      private void button1_Click(object sender, System.EventArgs e)
      {
       if(openFileDialog1.ShowDialog()==DialogResult.OK)
       {
        picArr=openFileDialog1.FileNames;
        //pictureBox1.Image=Image.FromFile(picArr[0]);
       }
      }






    保存对话框
    SaveFileDialog sfd = new SaveFileDialog();










    12.定时器
    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromMilliseconds(200); //获取或设置计时器刻度之间的时间段。 
    timer.Tick += timer_Tick; //超过计时器间隔时发生。
    timer.Start();
    void timer_Tick(object sender,object e)
    {

    }






    13.绘制圆角边框
    Border 类:
    CornerRadius 获取或设置边框的角的半径。 
    BorderBrush 获取或设置用于创建边框的颜色。 
    BorderThickness 获取或设置边框的粗细。 
    System.Windows.Media.Effects.DropShadowEffect 一种用于在目标纹理周围绘制投影的位图效果。 
    例如:
    <Border CornerRadius="5" BorderBrush="#FF226C8A" BorderThickness="0">
            <Border CornerRadius="5" BorderBrush="#FF56C5F3" BorderThickness="0">
                <Border Background="#FF1EB2EF">
                    <Grid>
                 
                    </Grid>
                </Border>
            </Border>
        </Border>








    <Border.Effect>
             <DropShadowEffect ShadowDepth="0" Color="#FF7A7F80"/>
    </Border.Effect>






    组合后:
            <Border CornerRadius="5" BorderBrush="#FF226C8A" BorderThickness="0" Height="530" Width="630">
                <Border.Effect>
                    <DropShadowEffect ShadowDepth="0" Color="#FF7A7F80"/>
                </Border.Effect>
                <Border CornerRadius="5" BorderBrush="#FF56C5F3" BorderThickness="0">    
                        <Image Source="Images/background.png" Height="520" Width="620"></Image>    
                </Border>
            </Border>












    14.去除按钮边框(把默认按钮样式隐藏)
    在<Button></Button>中加入
    <Button.Template>
    <ControlTemplate TargetType="Button">
    <ContentPresenter Content="{TemplateBinding Content}"/>
    </ControlTemplate>
    </Button.Template>














    15.textbox禁止输入法
    在xaml里面修改
    window里面加:
    xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"


    textbox属性加上input:InputMethod.IsInputMethodEnabled="False"




    实例:
    <Window x:Class="WpfModelViewApplication1.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
        Title="Main Window" Height="400" Width="800">
        <DockPanel>
            <Grid x:Name="grid1">
                <TextBox x:Name="tb" Width="100" HorizontalAlignment="Right" Margin="0,164,122,128" input:InputMethod.IsInputMethodEnabled="False"/>
            </Grid>
        </DockPanel>
    </Window>












    16.延时不假死
    Thread.Sleep(5);




    winform 下直接使用:
    static public void WaitFor(int ms)  
    {  
        DateTime time = DateTime.Now;  
        while (true)  
        {  
            Application.DoEvents();  //wpf 直接调用Doevents,定义在下面
            TimeSpan span = DateTime.Now - time;  
            if (span.TotalMilliseconds > ms) break;  
        }  
    }








    wpf编写Doevents():


    static void DoEvents()
            {
                DispatcherFrame frame = new DispatcherFrame(true);
                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate(object arg)
                {
                    DispatcherFrame fr = arg as DispatcherFrame;
                    fr.Continue = false;
                }, frame);
                Dispatcher.PushFrame(frame);
            }  












    17.窗口嵌于桌面,win+D不会隐藏
    涉及的api
    [DllImport("user32")]
    private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
    //获取窗口句柄传递键盘消息
            [DllImport("USER32.dll")]
            public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);




    IntPtr hDeskTop = FindWindow("Progman", "Program Manager");
    SetParent(this.Handle, hDeskTop); //windows+D不会隐藏


    wpf中this.Handle改为new WindowInteropHelper(this).Handle
  • 相关阅读:
    React在componentDidMount里面发送请求
    React 术语词汇表
    React里受控与非受控组件
    React和Vue等框架什么时候操作DOM
    【LeetCode】79. Word Search
    【LeetCode】91. Decode Ways
    【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
    【LeetCode】1. Two Sum
    【LeetCode】141. Linked List Cycle (2 solutions)
    【LeetCode】120. Triangle (3 solutions)
  • 原文地址:https://www.cnblogs.com/fornet/p/2976173.html
Copyright © 2011-2022 走看看