본문 바로가기

C#

C# 교육 5일차

WPF

xaml

사용자 정의 컨트롤 UserControl

 

Binding

code나 tag로 함

source, path 지정해야 함

 

<Window.Resources>
        <local:Coffee x:Key="c2" Name="StarBucks" Bean="Arabica"/>
    </Window.Resources>

태그에서 접근 가능

 

DataContext

 <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="93,236,0,0" 
                       TextWrapping="Wrap" Text="{Binding Source={StaticResource c2}, Path=Bean}" VerticalAlignment="Top"/>
 <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="93,307,0,0" 
                       TextWrapping="Wrap" Text="{Binding Source={StaticResource c2}, Path=Name}" VerticalAlignment="Top"/>

<StackPanel>
            <StackPanel.DataContext>
                <Binding Source="{StaticResource c2}"/>
            </StackPanel.DataContext>
            <TextBlock x:Name="textBlock" HorizontalAlignment="Left" 
                       TextWrapping="Wrap" Text="{Binding Path=Bean}" VerticalAlignment="Top"/>
            <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" 
                       TextWrapping="Wrap" Text="{Binding Path=Name}" VerticalAlignment="Top"/>
</StackPanel>

ListBox Item -> ToString 보여줌 ToString  재정의 필요

 

재사용할 것으로 생각되는 것은 Resouce에 정의

ResouceDictionary

 

style

 

trigger

 

<DoubleAnimation> 속성 값이 숫자인 속성 설정 가능

 

Multitask

 

Single Thread

Multi-Thread

1. Thread 생성

2. Task <-- Thread Pool에서 가져다가 씀 (권장)

반환값이 있으면 Thread로 안되고 Task로 사용

람다식

 delegate void MyDelegate(string s);
    class Program
    {
        public static void Hello(string s)
        {
            Console.WriteLine("Hello," + s);
        }
        public static int Add(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            Hello("A");

            //1.delegate 선언해서 사용
            MyDelegate d1 = new MyDelegate(Hello);
            d1("A");
            //2.Anonymous Method 익명 메서드 -delegate   3.* 
            MyDelegate d2 = delegate (string s)
            {
                Console.WriteLine("Hello," + s);
            };
            d2("A");
            //3.Lambda Expression 람다식  C++11   =>
            MyDelegate d3 = (s) => { Console.WriteLine("Hello," + s); };
            //Action 대리자 -반환값이 없는 메서드 호출 :16개
            Action<string> a1 = new Action<string>(Hello);
            a1("A");

            Action<string> a2 = (s) => { Console.WriteLine("Hello," + s); };

            //Func 대리자-반환값이 있는 메서드 호출       
            //                 --- 마지막 타입이 반환값
            Func<int,int,int> f1 = new Func<int,int,int>(Add);
            Console.WriteLine(f1(10,20));

            Func<int, int, int> f2 = (a, b) => { return a + b; };
            Console.WriteLine(f2(20,30));

            Func<int, int> f3 = (a) => { return a * a; };
            Console.WriteLine(f3(5));
        }
    }

async - await :비동기 프로그래밍

 

Running Tasks in Parallel

Using the Parallel.Invoke Method

Parallel.Invoke( () => MethodForFirstTask(),
() => MethodForSecondTask(),
() => MethodForThirdTask() );

Using a Parallel.For Loop

int from = 0;
int to = 500000;
double[] array = new double[capacity];
// This is a sequential implementation:
for(int index = 0; index < 500000; index++)
{
array[index] = Math.Sqrt(index);
}
// This is the equivalent parallel implementation:
Parallel.For(from, to, index =>
{
array[index] = Math.Sqrt(index);
});

 

 

Synchronizing

lock - 동기화

 

COM Component <- ActiveX Control (. ocx,. dll)

                                 Automation

regedit

 

 

dynamic - 컴파일 타임이 아닌 런타임에 타입체크 인텔리센스(자동완성)가 안 먹힘

Unmanaged Code

Excel

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace Mod11UseExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application app = new Excel.Application();
            app.Visible = true;
            Excel.Workbook wb = app.Workbooks.Add();
            Excel.Worksheet sheet = wb.Worksheets.get_Item(1);
            sheet.Cells[1, 1] = "Hello";
            sheet.Cells[2, 1] = "World";
            sheet.SaveAs("test.xls");
            app.Quit();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

 

[DllImport]

 [DllImport("user32.dll")] // FindWindow()현재 떠있는 창 찾을 때 사용
        public static extern int MessageBox(int hWnd, string lpText, string lpCaption, uint uType);

        static void Main(string[] args)
        {
            // MessageBox.Show("Hello");
            MessageBox(0, "Good Afternoon", "Hello", 1);
        }

 

 

 

Dispose - 라이프타임 제어

try-finally = using - 빠져나올 때 Dispose가 불러짐

 

gac

2.Shared Assembly
- GAC에 등록
   Global Assembly Cache
  c:\windows\assembly  .NET Fx2.X
  C:\Windows\Microsoft.NET\assembly   .NEt Fx 4.*
-strong name 강력한 이름
  -프로젝트 속성 - 서명탭
     -어셈블리 서명  : 강력한 이름 키 파일 선태 :새로 만들기
                   .snk
                   .pfx   -암호화
c>gacutil -i filename
c>gacutil -u filename

tips

http://soen.kr/

 

SoEn:소프트웨어 공학 연구소

 

soen.kr

ctrl + } 짝인 괄호 찾기

배포 시 아래 두개 파일 배포
Mod1Console(프로젝트 이름).exe ->Properties에서 변경 가능
Mod1Console.exe.config

'C#' 카테고리의 다른 글

C# 리스트 내의 모든 값이 참이면 조건 실행  (0) 2023.11.03
enum 길이  (0) 2023.11.01
C# 교육 4일차  (0) 2023.10.19
C# 교육 3일차  (0) 2023.10.18
C# 교육 2일차  (0) 2023.10.17