[WPF] xml 로드하기

C#/WPF 2020. 7. 8. 11:59
728x90
728x170

xml string 을 load 하여 화면에 보여주는 코드입니다.

(내용은 찰스패졸드 책 내용 인용)

 

using System.IO;

using System.Windows;

using System.Windows.Markup;

using System.Xml;

 

namespace WpfApp

{

    /// <summary>

    /// MainWindow.xaml에 대한 상호 작용 논리

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

 

            string strXml = @"

<FlowDocument xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'

              xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'

              TextAlignment='Left'>

    <Paragraph TextAlignment='Center' FontSize='32' FontStyle='Italic'

               LineHeight='24'>

        KJUN.KR

    </Paragraph>

    <Paragraph TextAlignment='Center'>

        &#x00A9; 2020 by kjun

    </Paragraph>

    <Paragraph FontSize='16pt' FontWeight='Bold' LineHeight='16'>

        Introduction

    </Paragraph>

    <Paragraph>

        KJUN.KR is a sample program from kjun

        <Italic>

            Applications = Code + Markup:

            A Guide to the Microsoft Windows Presentation Foundation

        </Italic>

        published by Microsoft Press in 2020.

        XAML Cruncher provides a convenient way to learn about and experiment

        with XAML, the Extensible Application Markup Language.

    </Paragraph>

    <Paragraph>

        XAML Cruncher consists of an Edit section (in which you enter and edit

        a XAML document) and a Display section that shows the object created

        from the XAML. If the XAML document has errors, the text is displayed

        in red and the status bar indicates the problem.

    </Paragraph>

    <Paragraph>

        Most of the interface and functionality of the edit section of

        XAML Cruncher is based on Windows Notepad.

        The

        <Bold>Xaml</Bold> menu provides additional features.

    </Paragraph>

    <Paragraph FontSize='16pt' FontWeight='Bold' LineHeight='16'>

        Xaml Menu

    </Paragraph>

    <Paragraph>

        The

        <Bold>Orientation</Bold> menu item lets you choose whether you

        want the Edit and Display sections of XAML Cruncher arranged

        horizontally or vertically.

    </Paragraph>

    <Paragraph>

        The

        <Bold>Tab Spaces</Bold> menu item displays a dialog box that lets

        you choose the number of spaces you want inserted when you press the

        Tab key. Changing this item does not change any indentation

        already in the current document.

    </Paragraph>

    <Paragraph>

        There are times when your XAML document will be so complex that it

        takes a little while to convert it into an object. You may want to

        <Bold>Suspend Parsing</Bold> by checking this item on the

        <Bold>Xaml</Bold> menu.

    </Paragraph>

    <Paragraph>

        If you've suspended parsing, or if you want to reparse the XAML file,

        select

        <Bold>Reparse</Bold> from the menu or press F6.

    </Paragraph>

    <Paragraph>

        If the root element of your XAML is

        <Italic>Window</Italic> ,

        XAML Cruncher will not be able to display the

        <Italic>Window</Italic>

        object in its own window.

        Select the

        <Bold>Show Window</Bold> menu item or press F7 to view

        the window.

    </Paragraph>

    <Paragraph>

        When you start up XAML Cruncher (and whenever you select

        <Bold>New</Bold> from the

        <Bold>File</Bold> menu), the Edit window

        displays a simple startup document.

        If you want to use the current document as the startup document,

        select the

        <Bold>Save as Startup Document</Bold> item.

    </Paragraph>

</FlowDocument>

";

 

            StringReader strreader = new StringReader(strXml);

            XmlTextReader xmlreader = new XmlTextReader(strreader);

            object obj = XamlReader.Load(xmlreader);

 

            Content = obj;

        }

 

    }

}

 

 

 

 

 

아래처럼 XML 에 frame 를 이용해 xml 파일을 지정할수도 있다.

 

<Window x:Class="WpfApp.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"

        xmlns:local="clr-namespace:WpfApp"

        mc:Ignorable="d"

        Title="MainWindow" Height="400" Width="600">

    <Frame Source="kjunkr.xaml" />

</Window>

 

 

 

728x90
그리드형

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

[WPF] static member 사용하기  (0) 2020.07.09
[WPF] ContextMenu  (0) 2020.07.08
[WPF] xml 16진수 값 0x0C은(는) 잘못된 문자입니다.  (0) 2020.07.08
[WPF] Hyperlink  (0) 2020.07.08
[WPF] Custom RoutedUICommand 만들기  (0) 2020.07.07
Posted by kjun
,