2월, 2014의 게시물 표시

Resource를 위한 별도의 Assembly를 만든다면 URI는 절대경로를 사용해라.

런타임에 다른 Application에서 사용되므로, 상대경로로 정의된 파일을 찾지 못한다. Resource assembly는 다음처럼 만들어라. 클래스라이브러리 프로젝트라면 assemblyinfo.cs 에 어트리뷰트를 추가 하고 [ assembly : ThemeInfo ( ResourceDictionaryLocation . None , //where theme specific resource dictionaries are located //(used if a resource is not found in the page, // or application resource dictionaries) ResourceDictionaryLocation . SourceAssembly //where the generic resource dictionary is located //(used if a resource is not found in the page, // app, or any theme specific resource dictionaries) )] 용도별로 따로 Resource Dictionary를 만들어서, /Themes/generic.xaml 에서 파일을 절대경로로 포함시켜서 머지하고, 더미클래스를 하나 정의하고, 다른 어셈블리에서 참조될 수 있도록,  ComponentResourceKey 를 이용해 키를 부여한다. DynamicResource 를 이용해 필요한 리소스를 참조해서 이용한다.

Pack URIs in WPF

WPF에서는 파일 위치를 정의하기 위해 uniform resource identifiers(URI)를  사용한다. Specifying the user interface (UI) to show when an application first starts. Loading images. Navigating to pages. Loading non-executable data files. 그리고 URI는 다양한 위치를 정의할 수 있다. The current assembly. A referenced assembly. A location relative to an assembly. The application's site of origin. 더 자세한 설명은 생략한다. 복잡하다.

StaticResource vs DynamicResource

At this point, you’re probably wondering what the difference is between a static and dynamic resource. The difference is that a static resource grabs the object from the resources collection once . Depending on the type of object (and the way it’s used), any changes you make to that object may be noticed right away. However, the dynamic resource looks the object up in the resources collection every time it’s needed . That means you could place an entirely new object under the same key, and the dynamic resource would pick up your change. As a general guideline, use dynamic properties only when you have these conditions: Your resource has properties that depend on system settings (such as the current Windows colors or fonts). You plan to replace your resource objects programmatically.

Bind to objects that aren't Elements

How to specify the source : Source  : An existing object. To refer to an existing object, use the StaticResource Markup Extension RelativeSource : Gets or sets the binding source by specifying its location relative to the position of the binding target. RelativeSource MarkupExtension DataContext : The binding source for multiple elements is defined once.

The Binding Class

Binding  원다풀~~~. 윈도우 UI 프로그래밍은 윈폼/WPF 모두 바인딩을 얼마나 잘  쓰느냐가 관건이다. 생산성 떨어지는 날코딩은 이제 그만하자. 참고로 일단 소스-타겥이 바인딩으로 연결된 후에, 타겥의 바인딩된 프라퍼티를 직접 설정하면 바인딩이 해제된다. 흔히 하는 실수. 일단 바인딩이 이루어졌으면 타겥의 프라퍼티는 항상 소스를 통해서 업데이트 해야 한다. 바인딩을 지우거나 바인딩 정보를 얻으려면  BindingOperations 을 이용한다. ClearBinding GetBinding 타겥은 항상 소스가 변하면 바로 업데이트 되지만(단 INotifyPropertyChanged  를 구현한 경우에만), 소스는 바로 업데이트 안 된다. UpdateSourceTrigger  ; PropertyChanged, LostFocus, Explicit, Default

Resources in WPF

StreamResourceInfo  가 아니더라도, Image의 Source 프라퍼티는 리소스 경로를 해석할 수 있다. 리소스 경로는 상대 경로를 사용하거나, 절대 경로를 사용한다. pack://application:,,,/[ AssemblyName ;component/] ResourceName 그리고  Content file 을 이용해 파일 자체를 포함시켜도 리소스의 경우와 같은 문법을 사용한다.

Button의 재밌는 두 속성

IsCancel When IsCancel is true, this button is designated as the cancel button for a window. If you press the Esc key while positioned anywhere on the current window, this button is triggered. IsDefault When IsDefault is true, this button is designated as the default button (also known as the accept button). Its behavior depends on your current location in the window. If you’re positioned on a non-Button control (such as a TextBox, RadioButton, CheckBox, and so on), the default button is given a blue shading, almost as though it has focus. If you press Enter, this button is triggered. However, if you’re positioned on another Button control, the current button gets the blue shading, and pressing Enter triggers that button, not the default button.

The hierarchy of content controls

이미지

How can you fix WPF’s small-text display problem?

So how can you fix WPF’s small-text display problem? The best solution is to scale up your text (on a 96 dpi monitor, the effect should disappear at a text size of about 15 device-independent units) or use a highdpi monitor that has enough resolution to show sharp text at any size. But because these options often aren’t practical, WPF also has the ability to selectively use GDI-like text rendering. To use GDI-style text rendering, you add the extOptions.TextFormattingMode attached property to a text-displaying element such as the TextBlock or Label, and set it to Display (rather than the standard value, Ideal). Here’s an example: <textblock fontsize="12" margin="5"> This is a Test. Ideal text is blurry at small sizes. </textblock> It’s important to remember that the TextFormattingMode property is a solution for small text only. If you use it on larger text (text above 15 points), the text will not be as clear, the spacing will not be as even, and the ...

The EventArgs classes for input events

이미지

항상 Loaded 이벤트에서 추가적인 초기화 작업을 수행하자.

You can also use the window constructor to perform your initialization (just add your code immediately after the InitializeComponent() call). However, it’s always better to use the Loaded event. That’s because if an exception occurs in the constructor of the Window, it’s thrown while the XAML parser is parsing the page. As a result, your  exception is wrapped in an unhelpful XamlParseException object (with the original exception in the InnerException property).

IoC(혹은 DI) Container 성능비교

.NET에서 사용되는 여러 IoC 컨테이너 성능비교 추천 Injector : Simple Injector

데이타로우로 C# 객체 생성하기

AutoMapper http://www.geekytidbits.com/automapper-with-datatables/ http://stackoverflow.com/questions/16222313/how-to-use-automapper-to-map-a-datarow-to-an-object-in-a-wcf-service DataRow로부터 POCO를 생성할 때(Projection), AutoMapper를 이용하자. 깔끔하다.

DataRow의 컬럼 값을 Generic(strongly-typed)한 방법으로 읽기

잘 몰랐을 때는 ToString()을 통해 문자열로 바꿔서, 다시 원하는 타입으로 파싱을 했는데 솔직히 좀 구리다. DataRowExtensions.Field Method  를 이용해야 한다. 타입 컨버전을 하지 않기 때문에 빠르다. 게다가 Nullable 타입도 지원한다.  http://stackoverflow.com/a/4593758 DataTable dt = new DataTable(); var someValue = dt.Rows[0].Field ("aaa");

WPF Events usually fall into one of five categories

Lifetime events : These events occur when the element is initialized, loaded, or unloaded. Mouse events : These events are the result of mouse actions. Keyboard events : These events are the result of keyboard actions (such as key presses). Stylus events : These events are the result of using the pen-like stylus, which takes the place of a mouse on a Tablet PC. Multitouch events : These events are the result of touching down with one or more fingers on a multitouch screen. They’re supported only in Windows 7 and Windows 8.

Routed events come in the following three flavors

이미지
Direct events These are like ordinary .NET events. They originate in one element and don’t pass to any other. For example, MouseEnter (which fires when the mouse pointer moves over an element) is a direct event. Bubbling events These events travel up the containment hierarchy. For example, MouseDown is a bubbling event. It’s raised first by the element that is clicked. Next, it’s raised by that element’s parent, then by thatelement’s parent, and so on, until WPF reaches the top of the element tree. Tunneling events These events travel down the containment hierarchy. They give you the chance to preview (and possibly stop) an event before it reaches the appropriate control. For example, PreviewKeyDown allows you to intercept a key press, first at the window level and then in increasingly more-specific containers until you reach the element that had focus when the key was pressed.  Tunneling events are useful if you need to perform some preprocessing that acts on ...