Microsoft MVP성태의 닷넷 이야기
.NET Framework: 166. WPF - XAML 요소의 네임스페이스와 CLR 타입 매핑 [링크 복사], [링크+제목 복사],
조회: 22099
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

WPF - XAML 요소의 네임스페이스와 CLR 타입 매핑


말로 표현하기보다는, 간단한 예를 들어 설명하는 것이 더 빠를 것 같습니다.

대개의 경우, 사용자 컨트롤을 만들어서 XAML 디자인에 올려놓으면 다음과 같은 식으로 구성이 됩니다.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" 
    xmlns:mycontrol="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1">
    <Grid>
        <mycontrol:UserControl1 />
    </Grid>
</Window>

이렇게 되는 경우, 각 컨트롤의 네임스페이스 및 어셈블리에 따라서 xmlns 항목이 별도로 추가되어야 합니다. 또는, 간혹 컨트롤을 다른 네임스페이스나 어셈블리로 옮길려고 하면 위의 구문이 들어있는 모든 XAML 코드를 수정해야 하는 불편함이 있습니다.

다른 3rd-party 컨트롤 업체에서는 이를 어떻게 해결하고 있을까요? 가령 Infragistics 컨트롤을 WPF 디자이너 화면에 올려놓으면 다음과 같이 XAML이 구성됩니다.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" 
    xmlns:igDP="http://infragistics.com/DataPresenter">
    <Grid>
        <igDP:XamDataGrid Name="xamDataGrid1" />
    </Grid>
</Window>

보는 것처럼, 해당 컨트롤이 정의된 .NET CLR Namespace와 Assembly 정보를 표시하지 않고 단순히 XML Namespace로 연결하는 것을 볼 수 있습니다. 이렇게 함으로써 향후 유지보수가 간소화되는 장점이 있습니다.

자!!!
문제는 이걸 어떻게 구현하느냐는 것입니다.

먼저, 해당 CLR 네임스페이스를 사용하지 않고 XML 네임스페이스로 연결하는 것은 다음과 같이 XmlnsDefinition 특성을 Assembly 수준에서 정의하는 것으로 해결할 수 있습니다.

[assembly: XmlnsDefinition("http://schemas.test.co.kr/wpf/", "WpfControlLibrary1")]

그럼, 위의 첫 번째 코드 예제를 다음과 같이 변경하는 것이 가능합니다.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" 
    xmlns:mycontrol="http://infragistics.com/DataPresenter">
    <Grid>
        <mycontrol:UserControl1 />
    </Grid>
</Window>

그런데, 디자인 화면에서 위에서 정의된 UserControl1을 끌어다 놓으면 xmlns Prefix가 "Custom" 같은 것으로 임의의 문자열로 정해지는 것을 확인할 수 있습니다. 즉, Infragistics처럼 "igDP"라는 특정 문자열을 사용하려고 하면 다음과 같은 추가적인 특성을 더 정의해 주어야 합니다.

[assembly: XmlnsPrefix("hhttp://schemas.test.co.kr/wpf/", "mycontrol")]

저처럼 후회하지 마시고 ^^ 미리미리 XmlnsPrefix / XmlnsDefinition 2가지 특성을 이용해서 컨트롤을 정의하시기 바랍니다.



[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/26/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 31  32  33  34  35  36  37  38  [39]  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12667정성태6/10/20219577.NET Framework: 1068. COM+ 서버 응용 프로그램을 이용해 CoInitializeSecurity 제약 해결파일 다운로드1
12666정성태6/10/20218122.NET Framework: 1067. 별도 DLL에 포함된 타입을 STAThread Main 메서드에서 사용하는 경우 CoInitializeSecurity 자동 호출파일 다운로드1
12665정성태6/9/20219440.NET Framework: 1066. Wslhub.Sdk 사용으로 알아보는 CoInitializeSecurity 사용 제약파일 다운로드1
12664정성태6/9/20217793오류 유형: 723. COM+ PIA 참조 시 "This operation failed because the QueryInterface call on the COM component" 오류
12663정성태6/9/20219337.NET Framework: 1065. Windows Forms - 속성 창의 디자인 설정 지원: 문자열 목록 내에서 항목을 선택하는 TypeConverter 제작파일 다운로드1
12662정성태6/8/20218416.NET Framework: 1064. C# COM 개체를 PIA(Primary Interop Assembly)로써 "Embed Interop Types" 참조하는 방법파일 다운로드1
12661정성태6/4/202119100.NET Framework: 1063. C# - MQTT를 이용한 클라이언트/서버(Broker) 통신 예제 [4]파일 다운로드1
12660정성태6/3/202110171.NET Framework: 1062. Windows Forms - 폼 내에서 발생하는 마우스 이벤트를 자식 컨트롤 영역에 상관없이 수신하는 방법 [1]파일 다운로드1
12659정성태6/2/202111414Linux: 40. 우분투 설치 후 MBR 디스크 드라이브 여유 공간이 인식되지 않은 경우 - Logical Volume Management
12658정성태6/2/20218856Windows: 194. Microsoft Store에 있는 구글의 공식 Youtube App
12657정성태6/2/202110121Windows: 193. 윈도우 패키지 관리자 - winget 설치
12656정성태6/1/20218366.NET Framework: 1061. 서버 유형의 COM+에 적용할 수 없는 Server GC
12655정성태6/1/20217816오류 유형: 722. windbg/sos - savemodule - Fail to read memory
12654정성태5/31/20217840오류 유형: 721. Hyper-V - Saved 상태의 VM을 시작 시 오류 발생
12653정성태5/31/202110530.NET Framework: 1060. 닷넷 GC에 새롭게 구현되는 DPAD(Dynamic Promotion And Demotion for GC)
12652정성태5/31/20218666VS.NET IDE: 164. Visual Studio - Web Deploy로 Publish 시 암호창이 매번 뜨는 문제
12651정성태5/31/20218916오류 유형: 720. PostgreSQL - ERROR: 22P02: malformed array literal: "..."
12650정성태5/17/20218228기타: 82. OpenTabletDriver의 버튼에 더블 클릭을 매핑 및 게임에서의 지원 방법
12649정성태5/16/20219556.NET Framework: 1059. 세대 별 GC(Garbage Collection) 방식에서 Card table의 사용 의미 [1]
12648정성태5/16/20218201사물인터넷: 66. PC -> FTDI -> NodeMCU v1 ESP8266 기기를 UART 핀을 연결해 직렬 통신하는 방법파일 다운로드1
12647정성태5/15/20219441.NET Framework: 1058. C# - C++과의 연동을 위한 구조체의 fixed 배열 필드 사용파일 다운로드1
12646정성태5/15/20218572사물인터넷: 65. C# - Arduino IDE의 Serial Monitor 기능 구현파일 다운로드1
12645정성태5/14/20218268사물인터넷: 64. NodeMCU v1 ESP8266 - LittleFS를 이용한 와이파이 접속 정보 업데이트파일 다운로드1
12644정성태5/14/20219430오류 유형: 719. 윈도우 - 제어판의 "프로그램 및 기능" / "Windows 기능 켜기/끄기" 오류 0x800736B3
12643정성태5/14/20218629오류 유형: 718. 서버 유형의 COM+ 사용 시 0x80080005(Server execution failed) 오류 발생
12642정성태5/14/20219554오류 유형: 717. The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
... 31  32  33  34  35  36  37  38  [39]  40  41  42  43  44  45  ...