かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

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

Silverlight2では、通常1つのアプリケーションが1つの*****.xapというzip形式で圧縮されたファイルとしてサーバ上に配備されます。
まだ、そこまで大きなアプリケーションを組んだことはないですが、本格的なアプリケーションを作るとxapが、ものすごいサイズになってしまって初回のダウンロードがとても大変になってしまいそうです。

そのため、業務単位とか適当な単位でSilverlight Applicationを分割してといった作りになりそうですが、それはそれでアプリケーション間での連携とかがちょっとめんどくさそうな気がしてます。
(よく調べないで言ってるので、いい方法もあるのかもしれません。知ってたらコメントください!!)

そんな時に、お手軽に使えそうないいものがComposite Application Guidance for WPF and Silverlight Feb2009の中にありました。
モジュールや関連するアセンブリをShellのあるxapとは別のxapに定義して、動的にそれを読み込ませることが割りと簡単に出来るっぽい。ということでHello world的に試してみました。

プロジェクトの作成

まず、プロジェクトを作成します。
作成するプロジェクトは以下の4つです。

  1. DynamicLoadApplication(Silverlightアプリケーション)
  2. DynamicLoadApplication.Web(1を作ったときに作られる)
  3. ModuleA(Silverlightクラスライブラリ)
  4. ModuleB(Silverlightアプリケーション):<テスト用ページは作成しない>

SilverlightのプロジェクトにはComposite Application Guidanceのライブラリを参照に追加しておきます。
ついでに、DynamicLoadApplicationでShellとBootstrapperを作成してApp.xaml.csでBootstrapperを起動するコードを書いておきます。

ここら辺はいつもどおりなので割愛。

Shellの作成

Shellを作っていきます。
といってもItemsControlを置いてRegionを定義するだけです(^^;

<UserControl x:Class="DynamicLoadApplication.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">
    <Grid x:Name="LayoutRoot" Background="White">
        <ItemsControl
            Regions:RegionManager.RegionName="MainRegion" />
    </Grid>
</UserControl>

ModuleAの作成

次に、通常のクラスライブラリで作ってModuleAを作成します。
ModuleAは、ボタンをクリックしたタイミングでModuleBを読み込むようにします。そのため、IModuleManagerをDIしています。

後は、普通のModuleと同じです。

ModuleAView.xaml

<UserControl x:Class="ModuleA.ModuleAView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Border BorderBrush="Blue" BorderThickness="1" Background="White">
        <StackPanel Margin="5">
            <TextBlock Text="ModuleA" />
            <Button Content="Load ModuleB"
                Click="ModuleBLoad" />
        </StackPanel>
    </Border>
</UserControl>

ModuleAView.xaml.cs

using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Unity;

namespace ModuleA
{
    public partial class ModuleAView : UserControl
    {
        // モジュール読み込みに必要なモジュールマネージャを
        // Unityからセットしてもらう。
        [Dependency]
        public IModuleManager ModuleManager { get; set; }

        public ModuleAView()
        {
            InitializeComponent();
        }

        private void ModuleBLoad(object sender, RoutedEventArgs e)
        {
            // ModuleBの読み込み
            this.ModuleManager.LoadModule("ModuleB");
        }
    }
}

Viewが出来たので、モジュールクラスを作成します。
モジュールクラスでは、MainRegionにModuleAViewを追加するだけです。

ModuleA.cs

using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;

namespace ModuleA
{
    public class ModuleA : IModule
    {
        [Dependency]
        public IUnityContainer Container { get; set; }

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        public void Initialize()
        {
            RegionManager.RegisterViewWithRegion("MainRegion",
                () => Container.Resolve<ModuleAView>());
        }
    }
}

ModuleAはこれで完成なので、DynamicLoadApplicationの参照にModuleAを追加して、Bootstrapperに以下GetModuleCatalogを以下のようにします。

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

この状態で実行すると、下のような画面が表示されます。
image

この時点でLoad ModuleBボタンを押しても、まだModuleBが無いのでModuleNotFoundExceptionが発生してしまいます。
ということで、今日の本題である別のxapにあるModuleを動的に読み込むということをしてみようと思います。

ModuleBの作成

ということで、ModuleBを作成していきます。まずは、見た目から作ります。
ModuleBViewという名前でユーザコントロールを作成して、以下のような見た目にします。

ModuleBView.xaml

<UserControl x:Class="ModuleB.ModuleBView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Border Background="White" BorderBrush="Red" BorderThickness="1">
        <TextBlock Text="ModuleB" Margin="5" />
    </Border>
</UserControl>

そして、モジュールクラスを作成します。
こちらも、ModuleBViewをMainRegionに追加するだけのシンプルなものです。

ModuleB.cs

using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;

namespace ModuleB
{
    public class ModuleB : IModule
    {
        [Dependency]
        public IUnityContainer Container { get; set; }
        [Dependency]
        public IRegionManager RegionManager { get; set; }

        public void Initialize()
        {
            RegionManager.RegisterViewWithRegion("MainRegion",
                () => Container.Resolve<ModuleBView>());
        }
    }
}

これでModuleB自体は完成です。
ModuleAと違うのは、DynamicLoadApplicationにModuleBの参照は追加しないということです。

ModuleB.xapから動的に色々読み込むようにModuleCatalogに追加する処理を追加します。

protected override IModuleCatalog GetModuleCatalog()
{
    return new ModuleCatalog()
        // ModuleAを追加
        .AddModule(typeof(ModuleA.ModuleA))
        // ModuleBを追加
        .AddModule(
            "ModuleB",
            "ModuleB.ModuleB, ModuleB, Version=1.0.0.0",
            "ModuleB.xap",
            InitializationMode.OnDemand);
}

最後に追加されたAddModuleでは、「ModuleB」という名前(ここは任意に決めれる)で、「ModuleB.ModuleB, ModuleB, Version1.0.0.0」という名前のクラスをモジュールとして読み込みます。
DLLとかはModuleB.xapにまとまって入っています。後は必要になったらDLしてきてね。

という感じのことが指定されています。

ということで実行してみます。

実行直後
image

Load ModuleBを押すとModuleBが読み込まれる
image

複数のXAPに分けて開発が出来て、それを割りと自然にくっつけることが出来るのはComposite Application Guidance for WPF and Silverlight Feb2009だけ!
(WebClientとか使ってごにょごにょやれば自作でも問題なく出来ます)

投稿日時 : 2009年3月18日 0:52

Feedback

# re: [Silverlight][C#]複数のxapを1つのアプリに 2009/07/19 10:04 お だ

お だ です。

こちらの記事を参考にさせていただきました。
トラックバックの仕方が分からなかったので、コメントで連絡しました。
宜しくお願いします。

# lancel 2012/10/17 22:21 http://www.saclancelpascher2013.com

You are my intake, I possess few blogs and occasionally run out from brand :). "Never mistake motion for action." by Ernest Hemingway.

# ugg sale 2012/10/19 13:28 http://www.superbootonline.com

of course like your website however you have to check the spelling on several of your posts. A number of them are rife with spelling problems and I find it very troublesome to inform the truth nevertheless I will surely come back again.

# Supra Skytop III 2012/12/07 20:21 http://supratkstore.webs.com/

I got what you intend, thanks for putting up.Woh I am delighted to find this website through google. "Success is dependent on effort." by Sophocles.

# Christian Louboutin Platforms 2012/12/08 15:12 http://myclsale1.webs.com/

Enjoyed reading through this, very good stuff, appreciate it. "While thou livest keep a good tongue in thy head." by William Shakespeare.

# burberry solde 2012/12/15 22:50 http://www.sacburberryecharpe.fr/category/foulard-

Its ability are thus fantastic and additionally working type so quick.

# mickeal kors sac pas cher 2012/12/18 2:02 http://sac2012femmes.wordpress.com/category/sacs-m

If a photostream has photos that - irrespective of whether good or even not - triggered any spirited comments¡ä twine.

# michael kors 2012/12/18 14:19 http://www.sacmichaelkors.net/sac-michael-kors-cla

Very practical info. Hope to find out more content articles soon!

# sacs burberry 2012/12/18 14:26 http://www.sacburberryecharpe.fr/category/sac-burb

I work with earbuds abroad because with their portability, even though I favor over that ear.

# burberry uk 2012/12/18 20:10 http://www.burberryoutlet2012.info/category/burber

gripping avenues of feed-back bursting in the photos.

# michael kors paris 2012/12/19 13:15 http://sac-michael-kors.webnode.fr/sur-michael-kor

Keep in the excellent deliver the results.

# burberry bags 2012/12/21 1:20 http://burberryukoutlets.wordpress.com/category/bu

Hey bless you!

# michael kors paris sac 2012/12/22 16:58 http://michael-kors-canada.webnode.fr/blog/

Its ability are hence fantastic not to mention working form so immediate.

# Hi, I do believe this is an excellent site. I stumbledupon it ;) I am going to come back yet again since I saved as a favorite it. Money and freedom is the greatest way to change, may you be rich and continue to guide other people. 2018/09/23 18:10 Hi, I do believe this is an excellent site. I stum

Hi, I do believe this is an excellent site. I stumbledupon it ;) I am going to
come back yet again since I saved as a favorite it.

Money and freedom is the greatest way to change, may you be rich and continue to guide other people.

# Woah! I'm really loving the template/theme of this website. It's simple, yet effective. A lot of times it's tough to get that "perfect balance" between usability and appearance. I must say you have done a awesome job with this. Also, the blog lo 2018/10/01 20:13 Woah! I'm really loving the template/theme of this

Woah! I'm really loving the template/theme of this website.
It's simple, yet effective. A lot of times it's tough to get that "perfect balance" between usability and appearance.
I must say you have done a awesome job with this. Also,
the blog loads super fast for me on Safari. Exceptional Blog!

# My partner and I stumbled over here coming from a different web page and thought I may as well check things out. I like what I see so i am just following you. Look forward to checking out your web page repeatedly. 2018/10/02 2:12 My partner and I stumbled over here coming from a

My partner and I stumbled over here coming from a different web page and thought I may as well check things out.
I like what I see so i am just following you. Look forward to checking out your web
page repeatedly.

# Hi there! Someone in my Myspace group shared this website with us so I came to take a look. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Excellent blog and amazing design and style. 2018/10/02 7:31 Hi there! Someone in my Myspace group shared this

Hi there! Someone in my Myspace group shared this
website with us so I came to take a look. I'm definitely loving the information. I'm bookmarking and will be tweeting this
to my followers! Excellent blog and amazing design and style.

# Hi there, I enjoy reading through your article post. I wanted to write a little comment to support you. 2018/11/02 20:38 Hi there, I enjoy reading through your article pos

Hi there, I enjoy reading through your article post.
I wanted to write a little comment to support you.

# Good article. I will be going through many of these issues as well.. 2018/11/12 15:04 Good article. I will be going through many of thes

Good article. I will be going through many of these
issues as well..

# My brother suggested I might like this web site. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks! 2018/11/12 22:53 My brother suggested I might like this web site.

My brother suggested I might like this web site. He was entirely right.
This post truly made my day. You can not imagine simply
how much time I had spent for this info! Thanks!

# Sling tv coupons and promo codes for november 2018 I was suggested this website by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my problem. You're amazing! Thanks! Sling tv coupons and promo codes f 2018/11/18 0:30 Sling tv coupons and promo codes for november 2018

Sling tv coupons and promo codes for november 2018
I was suggested this website by my cousin. I'm not sure whether this post is
written by him as no one else know such detailed
about my problem. You're amazing! Thanks! Sling tv
coupons and promo codes for november 2018

# Hello to every body, it's my first go to see of this webpage; this blog carries remarkable and actually fine data for readers. 2018/11/19 3:32 Hello to every body, it's my first go to see of t

Hello to every body, it's my first go to see of this webpage; this blog carries
remarkable and actually fine data for readers.

# Illikebuisse cnktb 2021/07/04 23:12 pharmacepticacom

quinoline sulfate https://www.pharmaceptica.com/

# re: [Silverlight][C#]???xap?1?????? 2021/07/17 9:13 hydroxychloroquine 200 mg high

used to treat malaria chloro https://chloroquineorigin.com/# plaquenil 200 mg twice a day

# lykfiertvdez 2021/11/27 21:26 dwedayqhqx

https://hydrochloroquinefil.com/ hydroxychloroquine sulfate

# wgonezchsinq 2022/05/07 0:34 uhuuhz

hydroxychloroquine risk https://keys-chloroquineclinique.com/

# plaquenil 200 mg cost 2022/12/26 14:58 MorrisReaks

http://hydroxychloroquinex.com/ where to buy hydroxychloroquine

# where can i buy hydroxychloroquine 2022/12/30 0:54 MorrisReaks

http://hydroxychloroquinex.com/ 200mg hydroxychloroquine

タイトル  
名前  
Url
コメント