Silverlight Workshop:複合アプリケーションライブラリ入門

この記事の対象者



次の場合、この投稿は興味深いものになります。

  1. 開発に集中している
  2. Silverlight(WPF)などのRIA環境で作業している
  3. Composite Application GuidanceComposite Application Libraryのような言葉を既に聞いたことがある場合
  4. Pavel Ivchenkovによる記事「 TDDの原理を使用したWPFでのアプリケーションの作成 」に興味があります
  5. Acervの記事「 ユースケースドリブン開発とコンポジットUIアプリケーションブロック 」を読むことに興味がありました


記事の目的



この記事では、Composite Application Guidanceに含まれるものの基本を学び、Composite Application Libraryに基づいて複合Silverlightアプリケーションを作成する方法(ロード可能なモジュール(Module)とコンソールに表示されるビュー(View)を作成する方法) )))

準備をしなさい



この記事では、Composite Application LibraryおよびUnity Application Blockバンドルの次のアセンブリが必要になります。



複合アプリケーションライブラリはソースコードの形式で配布されるため、次のアセンブリをコンパイルする必要があります。



これを行うには、キットから実行可能ファイルを実行します

デスクトップとSilverlight-Open Composite Application Library.bat

Visual Studioでプロジェクトをビルドすると、必要なアセンブリは次のディレクトリにあります。

CAL \ Silverlight \ Composite.UnityExtensions \ bin \ Debug



さあ始めましょう



1. Shellアプリケーションを使用してプロジェクトを作成します



最初に、 Silverlight アプリケーションアプリケーションを作成し、 HelloWorld.Silverlightと呼びます。また、ホストするASP.NET Webアプリケーションを追加することも忘れないでください。 結果は次のようになります。

Dd458947.1153f195-a8e1-43e4-a307-877d6823e346(en-us、MSDN.10).png

次に、必要なアセンブリへのリンクを追加します。



次に、アプリケーションのコンソール( 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 .
  1. < 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 .
  2. < 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 .
  3. < 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 .
  4. < 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 .
  5. < 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 .
  6. < 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 .
  7. < 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 .


次に、リージョンとは何か、どのように使用するかを知る必要があります。

リージョンは、特定のコントロール位置にビューを表示したり、ビューを動的に置き換えたりするために開発者が使用する概念的なメカニズムです。

次のコントロールは、リージョンのホスティングとして使用できます。



地域(地域)の詳細については、 http://msdn.microsoft.com/en-us/library/dd458944 (printer) .aspxを参照してください。

コンソールにリージョンを追加するには、ItemsControlを使用します。 Shell.xamlで、名前空間へのリンクを追加します

  1. xmlns:Regions = "clr-namespace:Microsoft.Practices.Composite.Presentation.Regions; assembly = Microsoft.Practices.Composite.Presentation"

*このソースコードは、 ソースコードハイライターで強調表示されました。


また、itemsControlコントロールを追加します

  1. < UserControl x:Class = "HelloWorld.Silverlight.Shell"
  2. xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:Regions = "clr-namespace:Microsoft.Practices.Composite.Presentation.Regions; assembly = Microsoft.Practices.Composite.Presentation"
  5. = "400" 高さ = "300" >
  6. < ItemsControl Name = "MainRegion" />
  7. </ UserControl >

*このソースコードは、 ソースコードハイライターで強調表示されました。


また、属性を追加し、RegionManagerを「MainRegion」という名前の現在の領域に示します

  1. < ItemsControl Name = "MainRegion" Regions:RegionManagerRegionName = "MainRegion" />

*このソースコードは、 ソースコードハイライターで強調表示されました。


ブートストラップ

Bootstrapperは、アプリケーションのコンポーネントを構成するためのメカニズムです。

コンポジットアプリケーションライブラリにあるUnityBootstrapper抽象クラスに基づいて独自のブートストラップクラスを作成し、Unityコンテナを使用してコンポーネントを初期化します。これを行うには、プロジェクトにBootstrapper.csファイルを追加し、継承を定義します。

  1. クラス Bootstrapper:UnityBootstrapper
  2. {
  3. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


CreateShellコンソールの作成方法も再定義します。

  1. Protected Override DependencyObject CreateShell()
  2. {
  3. シェルshell = Container.Resolve <Shell>();
  4. Application.Current.RootVisual = shell;
  5. シェルを返します。
  6. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


およびモジュールディレクトリの定義:

  1. 保護された オーバーライド IModuleCatalog GetModuleCatalog()
  2. {
  3. ModuleCatalogカタログ= 新しい ModuleCatalog();
  4. 返品カタログ。
  5. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


ブートストラップを初期化することも必要です。

  1. private void Application_Startup( オブジェクト送信者、StartupEventArgs e)
  2. {
  3. Bootstrapper bootstrapper = new Bootstrapper();
  4. bootstrapper.Run();
  5. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


プロジェクトを実行して確認できます。

2.モジュールを追加する



モジュールは、アプリケーションでの組み合わせ用に設計された機能的で疎結合のユニットです。

これを行うには、Silverlightクラスライブラリタイプのプロジェクトを追加し、HelloWorldModuleという名前を付ける必要があります。

image

アセンブリリンクを追加します。



クラスClass1の名前をHelloWorldModuleに変更し、Microsoft.Practices.Composite.Modularity.IModuleインターフェイスから継承します。

  1. パブリック クラス HelloWorldModule:IModule
  2. {
  3. public void Initialize(){}
  4. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


次に、GetModuleCatalogメソッドをわずかに変更して、Bootstrapperクラスのディレクトリにモジュールを登録する必要があります。

  1. 保護された オーバーライド IModuleCatalog GetModuleCatalog()
  2. {
  3. ModuleCatalogカタログ= 新しい ModuleCatalog()
  4. .AddModule( typeof (HelloWorldModule.HelloWorldModule));
  5. 返品カタログ。
  6. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


3.ビューを追加する



HelloWorldModuleモジュールのプロジェクトにViewsというフォルダーを作成し、それにHelloWorldViewというSilverlightユーザーコントロールを追加します。これがビューになります。 HelloWorldView.xamlを少し調整します。

  1. < UserControl x:Class = "HelloWorldModule.Views.HelloWorldView"
  2. xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" >
  4. < グリッド x:名前 = "LayoutRoot" 背景 = "白" >
  5. < TextBlock Text = "Hello World" Foreground = "Green" Horizo​​ntalAlignment = "Center" VerticalAlignment = "Center" FontFamily = "Calibri" FontSize = "24" FontWeight = "Bold" > </ TextBlock >
  6. </ グリッド >
  7. </ UserControl >

*このソースコードは、 ソースコードハイライターで強調表示されました。


領域マネージャー -ビューと領域の無関係なマッピング(名前を指定することによってのみ)のサービス。

依存性注入を介してHelloWorldModuleモジュールのクラスコンストラクターに追加し、regionManagerフィールドに保存します。

  1. public HelloWorldModule(IRegionManager regionManager)
  2. {
  3. this .regionManager = regionManager;
  4. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


地域のパフォーマンス登録します

  1. public void Initialize()
  2. {
  3. regionManager.RegisterViewWithRegion( "MainRegion"typeof (Views.HelloWorldView));
  4. }

*このソースコードは、 ソースコードハイライターで強調表示されました。


まあ、それだけです。 これで、アプリケーションを実行できます。

image

結論として



そのため、この記事では、Composite Application Guidanceに含まれるものの基本を学び、Composite Application Library(ロード可能なモジュール(Module)とコンソールに表示されるビュー(View)を作成)に基づいて複合Silverlightアプリケーションを作成する方法も学びました。 (シェル))。 約束どおりすべて。

ソース



  1. Patterns&Practicesの記事「WPFおよびSilverlightの複合アプリケーションガイダンス-2009年2月」の翻訳が少しゆるく不完全だった前


    Silverlightハンズオンラボ:複合アプリケーションライブラリ入門は、 http://msdn.microsoft.com/en-us/library/dd458947(printer).aspxにあります。
  2. マニュアルとソートは、 http://www.codeplex.com/CompositeWPF/からダウンロードできます

Source: https://habr.com/ru/post/J54485/


All Articles