この記事の対象者
次の場合、この投稿は興味深いものになります。
- 開発に集中している
- Silverlight(WPF)などのRIA環境で作業している
- Composite Application Guidance 、 Composite Application Libraryのような言葉を既に聞いたことがある場合
- Pavel Ivchenkovによる記事「 TDDの原理を使用したWPFでのアプリケーションの作成 」に興味があります 。
- Acervの記事「 ユースケースドリブン開発とコンポジットUIアプリケーションブロック 」を読むことに興味がありました
記事の目的
この記事では、Composite Application Guidanceに含まれるものの基本を学び、Composite Application Libraryに基づいて複合Silverlightアプリケーションを作成する方法(ロード可能なモジュール(Module)とコンソールに表示されるビュー(View)を作成する方法) )))
準備をしなさい
この記事では、Composite Application Libraryおよび
Unity Application Blockバンドルの次のアセンブリが必要になります。
- Microsoft.Practices.Composite.dll
- Microsoft.Practices.Composite。 プレゼンテーション dll
- Microsoft.Practices.Composite.UnityExtensions dll
- Microsoft.Practices.ServiceLocation.dll
- Microsoft.Practices.Unity.dll
複合アプリケーションライブラリはソースコードの形式で配布されるため、次のアセンブリをコンパイルする必要があります。
- Microsoft.Practices.Composite.dll
- Microsoft.Practices.Composite.Presentation.dll
- Microsoft.Practices.Composite.UnityExtensions.dll
これを行うには、キットから実行可能ファイルを実行します
デスクトップとSilverlight-Open Composite Application Library.batVisual Studioでプロジェクトをビルドすると、必要なアセンブリは次のディレクトリにあります。
CAL \ Silverlight \ Composite.UnityExtensions \ bin \ Debugさあ始めましょう
1. Shellアプリケーションを使用してプロジェクトを作成します
最初に、
Silverlight アプリケーションアプリケーションを作成し、
HelloWorld.Silverlightと呼びます。また、ホストするASP.NET Webアプリケーションを追加することも忘れないでください。 結果は次のようになります。
.png)
次に、必要なアセンブリへのリンクを追加します。
- Microsoft.Practices.Composite .dll
- Microsoft.Practices.Composite。 プレゼンテーション .dll
- Microsoft.Practices.Composite.UnityExtensions.dll
- Microsoft.Practices.Unity.dll
- Microsoft.Practices.ServiceLocation.dll
次に、アプリケーションのコンソール(
Shell )を作成する必要があります。このため、Page.xamlの名前をShell.xamlに変更し、Shell.xaml.csのクラス名をPageからShellに変更します。
また、Shell.xamlファイルを変更する必要があります。
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
< UserControl x:Class ="HelloWorld.Silverlight.Shell" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Width ="400" Height ="300" > < Grid x:Name ="LayoutRoot" Background ="White" > </ Grid > </ UserControl >
* This source code was highlighted with Source Code Highlighter .
次に、リージョンとは何か、どのように使用するかを知る必要があります。
リージョンは、特定のコントロール位置にビューを表示したり、ビューを動的に置き換えたりするために開発者が使用する概念的なメカニズムです。
次のコントロールは、リージョンのホスティングとして使用できます。
- System.Windows.Controls.ContentControl
- System.Windows.Controls.ItemsControl
- System.Windows.Controls.Primitives.Selector
- System.Windows.Controls.TabControl
地域(地域)の詳細については、
http://msdn.microsoft.com/en-us/library/dd458944 (printer)
.aspxを参照して
ください。コンソールにリージョンを追加するには、ItemsControlを使用します。 Shell.xamlで、名前空間へのリンクを追加します
- xmlns:Regions = "clr-namespace:Microsoft.Practices.Composite.Presentation.Regions; assembly = Microsoft.Practices.Composite.Presentation"
*このソースコードは、 ソースコードハイライターで強調表示されました。
また、itemsControlコントロールを追加します
- < UserControl x:Class = "HelloWorld.Silverlight.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"
- 幅 = "400" 高さ = "300" >
- < ItemsControl Name = "MainRegion" />
- </ UserControl >
*このソースコードは、 ソースコードハイライターで強調表示されました。
また、属性を追加し、RegionManagerを「MainRegion」という名前の現在の領域に示します
- < ItemsControl Name = "MainRegion" Regions:RegionManager 。 RegionName = "MainRegion" />
*このソースコードは、 ソースコードハイライターで強調表示されました。
ブートストラップBootstrapperは、アプリケーションのコンポーネントを構成するためのメカニズムです。
コンポジットアプリケーションライブラリにあるUnityBootstrapper抽象クラスに基づいて独自のブートストラップクラスを作成し、Unityコンテナを使用してコンポーネントを初期化します。これを行うには、プロジェクトにBootstrapper.csファイルを追加し、継承を定義します。
- クラス Bootstrapper:UnityBootstrapper
- {
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
CreateShellコンソールの作成方法も再定義します。
- Protected Override DependencyObject CreateShell()
- {
- シェルshell = Container.Resolve <Shell>();
- Application.Current.RootVisual = shell;
- シェルを返します。
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
およびモジュールディレクトリの定義:
- 保護された オーバーライド IModuleCatalog GetModuleCatalog()
- {
- ModuleCatalogカタログ= 新しい ModuleCatalog();
- 返品カタログ。
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
ブートストラップを初期化することも必要です。
- private void Application_Startup( オブジェクト送信者、StartupEventArgs e)
- {
- Bootstrapper bootstrapper = new Bootstrapper();
- bootstrapper.Run();
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
プロジェクトを実行して確認できます。
2.モジュールを追加する
モジュールは、アプリケーションでの組み合わせ用に設計された機能的で疎結合のユニットです。
これを行うには、Silverlightクラスライブラリタイプのプロジェクトを追加し、HelloWorldModuleという名前を付ける必要があります。
.png)
アセンブリリンクを追加します。
- Microsoft.Practices.Composite.dll
- Microsoft.Practices.Composite.Presentation.dll
クラスClass1の名前をHelloWorldModuleに変更し、Microsoft.Practices.Composite.Modularity.IModuleインターフェイスから継承します。
- パブリック クラス HelloWorldModule:IModule
- {
- public void Initialize(){}
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
次に、GetModuleCatalogメソッドをわずかに変更して、Bootstrapperクラスのディレクトリにモジュールを登録する必要があります。
- 保護された オーバーライド IModuleCatalog GetModuleCatalog()
- {
- ModuleCatalogカタログ= 新しい ModuleCatalog()
- .AddModule( typeof (HelloWorldModule.HelloWorldModule));
- 返品カタログ。
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
3.ビューを追加する
HelloWorldModuleモジュールのプロジェクトにViewsというフォルダーを作成し、それにHelloWorldViewというSilverlightユーザーコントロールを追加します。これがビューになります。 HelloWorldView.xamlを少し調整します。
- < UserControl x:Class = "HelloWorldModule.Views.HelloWorldView"
- xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" >
- < グリッド x:名前 = "LayoutRoot" 背景 = "白" >
- < TextBlock Text = "Hello World" Foreground = "Green" HorizontalAlignment = "Center" VerticalAlignment = "Center" FontFamily = "Calibri" FontSize = "24" FontWeight = "Bold" > </ TextBlock >
- </ グリッド >
- </ UserControl >
*このソースコードは、 ソースコードハイライターで強調表示されました。
領域マネージャー -ビューと領域の無関係なマッピング(名前を指定することによってのみ)のサービス。
依存性注入を介してHelloWorldModuleモジュールのクラスコンストラクターに追加し、regionManagerフィールドに保存します。
- public HelloWorldModule(IRegionManager regionManager)
- {
- this .regionManager = regionManager;
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
地域のパフォーマンスも
登録します 。
- public void Initialize()
- {
- regionManager.RegisterViewWithRegion( "MainRegion" 、 typeof (Views.HelloWorldView));
- }
*このソースコードは、 ソースコードハイライターで強調表示されました。
まあ、それだけです。 これで、アプリケーションを実行できます。
.png)
結論として
そのため、この記事では、Composite Application Guidanceに含まれるものの基本を学び、Composite Application Library(ロード可能なモジュール(Module)とコンソールに表示されるビュー(View)を作成)に基づいて複合Silverlightアプリケーションを作成する方法も学びました。 (シェル))。 約束どおりすべて。
ソース
- Patterns&Practicesの記事「WPFおよびSilverlightの複合アプリケーションガイダンス-2009年2月」の翻訳が少しゆるく不完全だった前
Silverlightハンズオンラボ:複合アプリケーションライブラリ入門は、 http://msdn.microsoft.com/en-us/library/dd458947(printer).aspxにあります。 - マニュアルとソートは、 http://www.codeplex.com/CompositeWPF/からダウンロードできます。