かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[Silverlight][C#]Composite Application Guidance for SilverlightでHello world

Composite Application GuidanceのFeb-2009でSilverlight2に対応しています。
ということでHello worldを作ってどんなものか確認してみようと思います。

Composite Application Guidance for Silverlightをコンパイルしよう

ダウンロードしたファイルからDesktop & Silverlight - Open Composite Application Library.batをダブルクリックして、プロジェクトを開きます。
うちで使ってるのはVS2008 stdなので、テストプロジェクトに対応していないので、その旨をあらわすエラーが出るけど気にしないでOKを押してれば開けます。
職場でVisual Web Developer 2008を使っても開けたので、VWDでも大丈夫です。

んでソリューションをビルドすると、Composite.UnityExtensionsプロジェクトのBinフォルダの下にdllがいくつか作られます。
その中から、以下の5つのDLLだけ抜き出してとっておきます。

image

プロジェクトの作成

準備が出来たので、Hello world用のプロジェクトを作成します。
「CompositeSilverlight-HelloWorld」という名前でSilverlightアプリケーションを新規作成します。

ソリューションフォルダ直下にLibraryというフォルダを作って、そこに、さっき作ったDLLをコピーしておきます。
コピーしたDLLをプロジェクトの参照に追加します。
image

ShellとかBootstrapperとか

次にお約束の手順をふんでComposite Application Guidanceっぽいつくりにしていきます。
まず、Page.xamlをさくっと削除してShellという名前のユーザーコントロールを作成します。

次に、UnityBootstrapperを継承したBootstrapperクラスを作成します。ここらへんは、ほとんどWPFのときと同じ感覚で出来ます。

public class Bootstrapper : UnityBootstrapper
{

    protected override DependencyObject CreateShell()
    {
        // コンテナからシェルのインスタンスを取得
        var shell = Container.Resolve<Shell>();
        // SilverlightはApplicationのRootVisualに追加した
        // ユーザコントロールが表示される。
        Application.Current.RootVisual = shell;
        return shell;
    }

    protected override IModuleCatalog GetModuleCatalog()
    {
        // まだモジュールは無い
        return new ModuleCatalog();
    }
}

そして、App.xaml.csのStartupイベントでBootstrapperを起動するようにします。

private void Application_Startup(object sender, StartupEventArgs e)
{
    var bootstrapper = new Bootstrapper();
    bootstrapper.Run();
}

以上で、何も無いアプリケーションは起動します。
一応起動してコンパイルエラーや変なエラーが出ないか確認します。

何も表示されません。
image

最後に、ContentControlを追加してRegionNameを定義しておきます。
ここに、モジュールで追加したユーザコントロールを表示されるようにします。

<UserControl x:Class="CompositeSilverlight_HelloWorld.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Regions="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation">
    <ContentControl Regions:RegionManager.RegionName="MainRegion" />
</UserControl>

ここら辺もWPFのときと変わらない感じに出来る。


モジュールの作成

次にモジュールを作成します。
Silverlight用のクラスライブラリプロジェクトを作成して、Composite Application GuidanceのDLLを参照に追加します。

次に、「GreetView」という名前でユーザコントロールを作成します。TextBlockやTextBoxやButtonを使って画面を作ります。

<UserControl x:Class="CompositeSilverlight_Module.GreetView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBlock Text="名前:" />
        <TextBox Width="200" />
        <Button Content="挨拶" />
        <TextBlock />
    </StackPanel>
</UserControl>

image

とりあえず、動きはつけてない状態で、これをShellにあてこんでみます。
HelloWorldModuleという名前のクラスを作ってIModuleインターフェースを実装します。

IRegionManagerとIUnityContainerを使うのでDependency属性をつけたプロパティも定義しておきます。そして、InitializeメソッドでMainRegionにGreetViewを登録します。

public class HelloWorldModule : IModule
{
    // UnityContainerにインスタンスを登録してもらう
    [Dependency]
    public IUnityContainer Container { get; set; }
    [Dependency]
    public IRegionManager RegionManager { get; set; }
    
    public void Initialize()
    {
        // MainRegionにGreetViewを登録する
        RegionManager.RegisterViewWithRegion(
            "MainRegion",
            () => Container.Resolve<GreetView>());
    }
}

ここまで出来たら、「CompositeSilverlight-HelloWorld」プロジェクトに「CompositeSilverlight-Module」の参照を追加します。
そして、BootstrapperのGetModuleCatalogメソッドにHelloWorldModule登録のコードを追加します。

protected override IModuleCatalog GetModuleCatalog()
{
    // HelloWorldModuleを追加
    return new ModuleCatalog()
        .AddModule(typeof(HelloWorldModule));
}

これで、実行すると画面が表示されます。
image

動きは、まだ何もありません。

Commandを使ってみよう

とりあえず、このアプリに動きをつけてみようと思います。
まずは、普通と同じようにINotifyPropertyChangedを実装してNameをMessageプロパティを定義したGreetModelクラスを作成します。

public class GreetModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged メンバ

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var h = PropertyChanged;
        if (h != null) h(this, new PropertyChangedEventArgs(name));
    }

    #endregion

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    private string _message;
    public string Message
    {
        get { return _message; }
        set
        {
            if (_message == value) return;
            _message = value;
            OnPropertyChanged("Message");
        }
    }
}

そして、こんにちは[入力された名前]さん!とメッセージを作るGreetメソッドを定義して、それを実行するDelegateCommand<T>を定義します。

// 挨拶を作成する
public void Greet()
{
    this.Message = string.Format("こんにちは{0}さん", this.Name);
}

// メッセージ作成用コマンド
public ICommand CreateMessage { get; private set; }

public GreetModel()
{
    // コマンド初期化
    CreateMessage = new DelegateCommand<object>(
        param => Greet());
}

後はGreetViewでBindingをします。

<UserControl x:Class="CompositeSilverlight_Module.GreetView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:Commands="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBlock Text="名前:" />
        <TextBox Width="200" Text="{Binding Name, Mode=TwoWay}"/>
        <Button Content="挨拶" Commands:Click.Command="{Binding CreateMessage}" />
        <TextBlock Text="{Binding Message, Mode=TwoWay}"/>
    </StackPanel>
</UserControl>

このXAMLのポイントは、Commandを使用している部分です。
これがあれば、Silverlightでもコマンドが使えます。

 

そして、最後にGreetViewとGreetModelを紐付ける処理をModuleに書きます。

public class HelloWorldModule : IModule
{
    // UnityContainerにインスタンスを登録してもらう
    [Dependency]
    public IUnityContainer Container { get; set; }
    [Dependency]
    public IRegionManager RegionManager { get; set; }
    
    public void Initialize()
    {
        // MainRegionにGreetViewを登録する
        Container.RegisterType<GreetModel>();
        RegionManager.RegisterViewWithRegion(
            "MainRegion",
            () => Container.Resolve<GreetView>());
    }
}


これで実行すると、HelloWorldが恐らく動きます。
image

WPFのComposite Application Guidanと、ほぼ同じのりで作れる。
すばらしい。

投稿日時 : 2009年3月11日 1:11

Feedback

# [Silverlight][C#]複数のxapを1つのアプリに 2009/03/18 0:52 かずきのBlog

[Silverlight][C#]複数のxapを1つのアプリに

# re: [Silverlight][C#]Composite Application Guidance for SilverlightでHello world 2010/01/18 14:27 通りすがり

うーん、最後のGreetViewとGreetModelの紐付けが出来てないのかボタンをクリックしても動作せず…。
どこか見落としてるのかなぁ?

# re: [Silverlight][C#]Composite Application Guidance for SilverlightでHello world 2010/01/18 16:12 かずき

すいません(^^;
紐付けのコードがすっぽりと抜け落ちてます・・・。

今手元に試せる環境がないので、これで動く確証はないのですが、GreetViewに以下のプロパティを追加するとどうなりますか?
[Dependency]
public GreetModel Model
{
get { return DataContext as GreetModel; }
set { DataContext = value; }
}
この記事を書いた当初にやっていた方法と同じかどうかはわかりませんが、多分動くようになると思います。
もし、動かなかったらまたコメントでお知らせ頂ければ、家に帰って試してみようと思います。

# ShBwubcWlJfs 2011/09/29 1:19 http://oemfinder.com

QfsCTp Yet, much is unclear. Could you describe in more details!...

# PJEEqvLpEvqoq 2011/11/02 5:26 http://www.pharmaciecambier.com/

Good day! I do not see the conditions of using the information. May I copy the text from here on my site if you leave a link to this page?!...

# UpJTpHxPjitIJdEXibm 2011/11/02 6:20 http://optclinic.com/

Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!...

# JunAGCPJhPn 2011/11/09 6:21 http://roaccutaneprix.net/

Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!...

# dUZhxdlnxAOQXmntdtR 2011/11/09 6:41 http://www.farmaciaunica.com/

Yeah? I read and I understand that I do not understand anything what it is about:D

# VgwJaPvPpeDTVA 2011/11/16 2:57 http://circalighting.com/details.aspx?pid=721&

The topic is pretty complicated for a beginner!...

# twdMobdNpTFjCGwE 2011/11/16 3:36 http://www.catalinabiosolutions.com/index.php/pond

Read, of course, far from my topic. But still, we can work together. How do you feel about trust management?!...

# HWfGiyxpToI 2011/11/16 4:41 http://www.hooksandlattice.com/barnlights.html

Thanks for all the answers:) In fact, learned a lot of new information. Dut I just didn`t figure out what is what till the end!...

# full welded ball valve 2012/10/18 23:06 http://www.dwkvalve.com/product_cat_list/Full-Weld

Only wanna remark on few general things, The website layout is perfect, the articles is very fantastic. "The way you treat yourself sets the standard for others." by Sonya Friedman.

# sacs lancel 2012/10/19 13:29 http://www.saclancelpascher2013.com

Simply a smiling visitant here to share the love (:, btw great style .

# womens shirts 2012/10/26 3:03 http://www.burberryoutletscarfsale.com/burberry-wo

I really enjoy reading through on this internet site , it has fantastic articles . "Those who complain most are most to be complained of." by Matthew Henry.
womens shirts http://www.burberryoutletscarfsale.com/burberry-womens-shirts.html

# wallet 2012/10/28 16:47 http://www.burberryoutletscarfsale.com/accessories

Perfectly pent subject matter, thanks for information .
wallet http://www.burberryoutletscarfsale.com/accessories/burberry-wallets-2012.html

# mens shirts 2012/10/28 16:47 http://www.burberryoutletscarfsale.com/burberry-me

Just wanna remark that you have a very decent website , I love the pattern it actually stands out.
mens shirts http://www.burberryoutletscarfsale.com/burberry-men-shirts.html

# burberry bags 2012/10/28 16:47 http://www.burberryoutletscarfsale.com/burberry-ba

Simply wanna input that you have a very decent website , I love the style and design it actually stands out.
burberry bags http://www.burberryoutletscarfsale.com/burberry-bags.html

# cheap tie 2012/10/28 16:47 http://www.burberryoutletscarfsale.com/accessories

of course like your website but you have to check the spelling on quite a few of your posts. Several of them are rife with spelling problems and I in finding it very bothersome to tell the truth however I'll surely come again again.
cheap tie http://www.burberryoutletscarfsale.com/accessories/burberry-ties.html

# Nike Schehe 2012/10/30 21:10 http://www.nikefree3runschuhe.com/

Do not ever bring your main felicity to at least one substantially less rosy more than all by yourself.
Nike Schehe http://www.nikefree3runschuhe.com/

# t shirt scarf 2012/10/31 19:43 http://www.burberrysalehandbags.com/burberry-scarf

Great write-up, I'm regular visitor of one's blog, maintain up the excellent operate, and It is going to be a regular visitor for a long time.
t shirt scarf http://www.burberrysalehandbags.com/burberry-scarf.html

# wallet 2012/11/01 9:14 http://www.burberryoutletlocations.com/burberry-wa

Some genuinely superb information, Gladiolus I found this. "Carthago delenda est. (Carthage must be destroyed.)" by Marcius Porcius Cato.
wallet http://www.burberryoutletlocations.com/burberry-wallets-2012.html

# burberry watches on sale 2012/11/01 9:15 http://www.burberryoutletlocations.com/burberry-wa

I the efforts you have put in this, regards for all the great blog posts.
burberry watches on sale http://www.burberryoutletlocations.com/burberry-watches.html

# cheap tie 2012/11/01 9:15 http://www.burberryoutletlocations.com/burberry-ti

Some truly great blog posts on this website , thanks for contribution.
cheap tie http://www.burberryoutletlocations.com/burberry-ties.html

# burberry mens shirts 2012/11/01 9:15 http://www.burberryoutletlocations.com/burberry-me

I like this post, enjoyed this one thanks for posting. "It is well to give when asked but it is better to give unasked, through understanding." by Kahlil Gibran.
burberry mens shirts http://www.burberryoutletlocations.com/burberry-men-shirts.html

# burberry bag 2012/11/01 9:16 http://www.burberryoutletlocations.com/burberry-wo

I the efforts you have put in this, regards for all the great articles.
burberry bag http://www.burberryoutletlocations.com/burberry-women-bags.html

# mens shirts 2012/11/03 1:28 http://www.burberrysalehandbags.com/burberry-men-s

Merely wanna admit that this is handy , Thanks for taking your time to write this.
mens shirts http://www.burberrysalehandbags.com/burberry-men-shirts.html

# Burberry Ties 2012/11/03 1:28 http://www.burberrysalehandbags.com/burberry-ties.

I've been surfing online more than three hours today, but I never discovered any attention-grabbing article like yours. It is beautiful worth enough for me. Personally, if all webmasters and bloggers made excellent content material as you probably did, the internet shall be a lot more useful than ever before. "It's all right to have butterflies in your stomach. Just get them to fly in formation." by Dr. Rob Gilbert.
Burberry Ties http://www.burberrysalehandbags.com/burberry-ties.html

# scarf 2012/11/03 1:33 http://www.burberryoutletscarfsale.com/accessories

It's truly a great and useful piece of information. I am satisfied that you simply shared this useful information with us. Please stay us informed like this. Thanks for sharing.
scarf http://www.burberryoutletscarfsale.com/accessories/burberry-scarf.html

# Men's Canada Goose Como Parka 2012/11/03 4:29 http://www.supercoatsale.com/mens-canada-goose-com

I like this post, enjoyed this one thanks for putting up.
Men's Canada Goose Como Parka http://www.supercoatsale.com/mens-canada-goose-como-parka-c-1_8.html

# Men's Duvetica Jackets 2012/11/03 4:29 http://www.supercoatsale.com/canada-goose-duvetica

Some genuinely superb information, Sword lily I noticed this. "If you find it in your heart to care for somebody else, you will have succeeded." by Maya Angelou.
Men's Duvetica Jackets http://www.supercoatsale.com/canada-goose-duvetica-mens-duvetica-jackets-c-13_14.html

# Women's Canada Goose Jackets 2012/11/03 4:29 http://www.supercoatsale.com/womens-canada-goose-j

great points altogether, you just won brand new|a new} reader. What may you suggest in regards to your publish that you just made some days ago? Any certain?
Women's Canada Goose Jackets http://www.supercoatsale.com/womens-canada-goose-jackets-c-12.html

# Adidas Climacool Ride 2012/11/03 4:30 http://www.adidasoutle.com/adidas-shoes-adidas-cli

Thanks, I have just been looking for info about this topic for a long time and yours is the best I have discovered so far. However, what about the bottom line? Are you sure about the supply?
Adidas Climacool Ride http://www.adidasoutle.com/adidas-shoes-adidas-climacool-ride-c-1_3.html

# Women's Duvetica Coats 2012/11/03 4:30 http://www.supercoatsale.com/canada-goose-duvetica

I like this post, enjoyed this one thanks for posting .
Women's Duvetica Coats http://www.supercoatsale.com/canada-goose-duvetica-womens-duvetica-coats-c-13_16.html

# ehChiNtqfKmZ 2014/08/27 1:56 http://crorkz.com/

W9R86z Heya i am for the primary time here. I came across this board and I to find It really useful & it helped me out much. I hope to provide something back and help others such as you aided me.

# RHavJrudrBZuGc 2014/09/03 19:58 https://www.youtube.com/watch?v=ietNoQGnfas

I will immediately seize your rss as I can't find your e-mail subscription link or e-newsletter service. Do you have any? Please permit me recognize in order that I may just subscribe. Thanks.

# DDaftXkXcLiQ 2014/09/08 20:38 http://janetnevins.wordpress.com/

I've learn a few excellent stuff here. Definitely price bookmarking for revisiting. I surprise how a lot attempt you place to create the sort of fantastic informative site.

# WTwWUNpBjyfULLD 2014/09/17 16:31 http://www.1818-888.com

This website online is known as a stroll-by for all the information you needed about this and didn't know who to ask. Glimpse here, and you'll undoubtedly discover it.

# Where I can find Ramsey Campbell 22 ebooks Collection mobi&epub To Download? To http://4.shopfor.pw/89u 2014/12/27 8:00 NXbmTXkico

Where I can find Holly Black 16 ebooks Collection PDF MOBI EPUB To Download? To http://at.tvv.pw/yf

# I used to be able to find good info from your content. 2019/04/16 15:35 I used to be able to find good info from your cont

I used to be able to find good info from your content.

# I am not sure where you're getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for excellent information I was looking for this info for my mission. 2019/06/15 10:15 I am not sure where you're getting your informatio

I am not sure where you're getting your information, but good topic.
I needs to spend some time learning more or understanding
more. Thanks for excellent information I was looking for this info for my mission.

# You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this web site. 2019/08/01 17:28 You have made some decent points there. I checked

You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this
web site.

# You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this web site. 2019/08/01 17:29 You have made some decent points there. I checked

You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this
web site.

# You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this web site. 2019/08/01 17:30 You have made some decent points there. I checked

You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this
web site.

# You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this web site. 2019/08/01 17:31 You have made some decent points there. I checked

You have made some decent points there. I checked on the web to learn more about the issue and found most people will go along with your views on this
web site.

タイトル
名前
Url
コメント