かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

XAMLって画面構築用ってわけじゃないんだ

XAMLって今まで画面専用マークアップ言語だと思ってたけど大きな勘違いをしてたっぽい。
とても汎用的につかえるオブジェクトのマークアップ言語だったのね。

ってことで実験。
XAMLFamilyって名前でコンソールアプリのプロジェクトを作って、WindowsBaseとPresentationFrameworkとPresentationCoreを参照に追加。

XAMLFamilyにFamilyクラスを作ってみた。
中身はNameプロパティを持つだけのシンプルなものです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XAMLFamily
{
    public class Family
    {
        public string Name { get; set; }
    }
}

んで、Family.xamlっていうファイルをひとつ追加。
XMLとして作ってしまおう。(csファイルはいらないから)
ビルドアクションは、埋め込まれたリソースにします。
作ったら下のようなXMLを書きます。

<f:Family
    xmlns:f="clr-namespace:XAMLFamily;assembly=XAMLFamily"
    Name="孫悟空一家">
</f:Family>

Mainに下のようなプログラムを書くと、コンソールに「孫悟空一家」と表示されます。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Reflection;
using System.IO;

namespace XAMLFamily
{
    class Program
    {
        static void Main(string[] args)
        {
            Stream xaml = Assembly.GetExecutingAssembly().GetManifestResourceStream("XAMLFamily.Family.xaml");
            Family family = (Family) XamlReader.Load(xaml);
            Console.WriteLine(family.Name);
        }
    }
}

XAMLを読み込んでFamilyクラスのインスタンスを作ってます。
XAMLに定義した内容は、

Family family = new Family() { Name = "孫悟空一家" };

とまったく同じです。
さて、Familyには誰かしらが所属してるのでそういう風にしていきます。
Personクラスを作って、FamilyにICollection<Person>型のMembersプロパティを作ります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XAMLFamily
{
    public class Family
    {
        public Family()
        {
            Members = new List<Person>();
        }

        public string Name { get; set; }
        public List<Person> Members { get; set; }
    }
    public class Person
    {
        public string Name { get; set; }
    }
}

XAMLのほうにガシガシ定義を追加していきます。
Family.MembersにPersonを追加して家族っぽくしていきます。

<f:Family
    xmlns:f="clr-namespace:XAMLFamily;assembly=XAMLFamily"
    Name="孫悟空一家">
    <f:Family.Members>
        <f:Person Name="孫悟空" />
        <!-- チチって苗字孫なの? -->
        <f:Person Name="孫チチ" />
        <f:Person Name="孫悟飯" />
        <f:Person Name="孫悟天" />
    </f:Family.Members>
</f:Family>

Mainの方もFamilyクラスのMembersの中身を出力するようにします。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Reflection;
using System.IO;

namespace XAMLFamily
{
    class Program
    {
        static void Main(string[] args)
        {
            Stream xaml = Assembly.GetExecutingAssembly().GetManifestResourceStream("XAMLFamily.Family.xaml");
            Family family = (Family) XamlReader.Load(xaml);
            Console.WriteLine(family.Name);
            // 家族のメンバーも表示するよ
            family.Members.ForEach(p => Console.WriteLine("\t{0}", p.Name));
        }
    }
}

実行してみると、XAMLに定義した家族のメンバーが表示されます。

孫悟空一家
       孫悟空
       孫チチ
       孫悟飯
       孫悟天

そっかぁ今までWPFで画面作るのにやってたのは、System.Windows.Windowクラスのインスタンスを組み立ててただけなんだぁと納得。
んじゃ、Grid.Rowとかみたいなのは、どういうことになってるの?ってのを見て見ると、staticメソッドのSet~って形で作るみたい。
へぇ~。なるほろ。

工夫次第でほかのものにも使えそうな感じがしてきた。

投稿日時 : 2007年10月14日 1:41

Feedback

# re: XAMLって画面構築用ってわけじゃないんだ 2007/10/15 9:39 渋木宏明(ひどり)

当初は画面専用的な扱いでしたが、途中から汎用的に使われるようになりました>xaml

WF (Windows WorkFlow) のワークフローも xaml で記述します。

んが、最終的には画面専用的な扱いでリリースされることになりました>xaml

# re: XAMLって画面構築用ってわけじゃないんだ 2007/10/15 10:23 シャノン

ありゃ、XOMLってのはなかったっけ?

# re: XAMLって画面構築用ってわけじゃないんだ 2007/10/15 14:14 渋木宏明(ひどり)

最終的に「xaml は画面用」な扱いをされる過程で分化したんだったと思います>xoml

# re: XAMLって画面構築用ってわけじゃないんだ 2007/10/15 18:35 かずき

名前が違うだけで同じものなんですか。
WFもやらないとなぁ…

# [C#][WPF]DependencyObjectって その4 2008/02/08 0:14 かずきのBlog

[C#][WPF]DependencyObjectって その4

# longchamps 2012/12/14 22:37 http://www.soldesacslongchamp.info/category/sac-lo

make them red by using a yellow indy!!

# longchamp bag outlet 2012/12/15 15:37 http://www.longchampbagoutlet.info/category/longch

Very effective info. Hope to find more content soon!

# burberry online 2012/12/16 3:45 http://www.burberryuksale.info/category/burberry-c

Very useful info. Hope to check out more posts soon!

# PrbMerpeqtauLOgpWW 2014/08/28 15:53 http://crorkz.com/

YsW7ol Useful information. Fortunate me I discovered your web site by accident, and I'm stunned why this accident did not came about earlier! I bookmarked it.

# ivermectin rx 2021/09/28 19:25 MarvinLic

stromectol ivermectin 3 mg https://stromectolfive.com/# ivermectin 1 cream

# ivermectin buy australia 2021/11/01 22:12 DelbertBup

ivermectin where to buy http://stromectolivermectin19.com/# ivermectin cream uk
ivermectin brand name

# ivermectin 18mg 2021/11/03 1:59 DelbertBup

ivermectin 1 http://stromectolivermectin19.online# ivermectin usa price
ivermectin 1 topical cream

# buying prednisone without prescription https://prednisonesnw.com/#
generic prednisone 10mg 2021/11/13 9:44 Prednisone

buying prednisone without prescription https://prednisonesnw.com/#
generic prednisone 10mg

# buy careprost in the usa free shipping 2021/12/12 8:22 Travislyday

http://stromectols.com/ buy stromectol pills

# bimatoprost generic 2021/12/13 3:55 Travislyday

http://bimatoprostrx.online/ careprost bimatoprost for sale

# careprost for sale 2021/12/14 19:04 Travislyday

https://plaquenils.com/ plaquenil cheapest price

# ivermectin 5 mg price 2022/02/18 9:53 AlfonzoShump

http://stromectolst.com/# ivermectin price comparison
stromectol in canada

# generic ivermectin for humans 2022/02/22 1:49 Geraldtaw

https://stromectolis.com/# stromectol usa

# doxycycline generic https://doxycyline1st.com/
100mg doxycycline 2022/02/26 8:26 Jusidkid

doxycycline generic https://doxycyline1st.com/
100mg doxycycline

# doors2.txt;1 2023/03/14 14:51 nMpAkZDDukvsRVhQ

doors2.txt;1

# doors2.txt;1 2023/03/14 16:26 AxcRXADaLBfcSuFv

doors2.txt;1

# overseas pharmacies online https://pillswithoutprescription.pro/# 2023/05/16 11:34 PillsPro

overseas pharmacies online https://pillswithoutprescription.pro/#

# abella danger filmleri https://abelladanger.online/ abella danger izle
2024/03/06 11:18 Adella

abella danger filmleri https://abelladanger.online/ abella danger izle

# gates of olympus demo - https://gatesofolympus.auction/ gates of olympus giri&#351; 2024/03/27 21:12 Olympic

gates of olympus demo - https://gatesofolympus.auction/ gates of olympus giri&#351;

タイトル
名前
Url
コメント