XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips Prism 4.5 &...

52
XAML /C# を使った Windows ストアアプリ(LOB)構築 TIPS - Prism 4.5 & Kona Project 等のご紹介 - 鈴木 章太郎 テクニカルエバンジェリスト MTC アーキテクト 日本マイクロソフト株式会社 http://blogs.msdn.com/b/shosuz/

Transcript of XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips Prism 4.5 &...

Page 1: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

XAML /C# を使ったWindows ストアアプリ(LOB)構築 TIPS - Prism 4.5 & Kona Project 等のご紹介 -

鈴木章太郎テクニカルエバンジェリスト兼 MTC アーキテクト日本マイクロソフト株式会社

http://blogs.msdn.com/b/shosuz/

Page 2: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

テクニカルエバンジェリストhttp://blogs.msdn.com/b/shosuz

MTC アーキテクトhttp://www.microsoft.com/ja-jp/business/mtc/ads.aspx

呟きネタは主に、Windows Azure, Windows、Windows Phone, RIA, HTML5, MVVM, iOS/Android x Windows Azure連携, Guitar … 等

ASPIC 執行役員 (‘05 ~)

Wipse モバイル x クラウド部会長(’11 ~)

2005-2012 早稲田大学大学院非常勤講師、2008-2010 中央大学非常勤講師

Microsoft 軽音楽部広報担当 (Gt./Key./DAW)

(2012 ~)

https://www.facebook.com/shosuz

Page 3: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

アジェンダ

Page 4: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 5: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Windowsストア LOB アプリ

LOB アプリケーションにも本格導入検討中の企業が多い

課題としては

既存のアプリケーションとの共存の方法

開発リソースの確保、技術研修等

既存アプリケーションの互換性確認の問題

Windows RT かWindows 8 Pro/Enterprise か

配布の方法(Windowsストア、SCCM、Active Directory、Intune、他)

Page 6: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 7: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

データソースとしての選択肢 ストアアプリケーションは基本的にデータベース直接接続不可

LOB アプリケーションはヘテロな環境に追加される

新規に構築する and/or 既存のリレーショナルデータベース

新規に公開する and/or 既存のWeb サービス

新規に構築する and/or 既存のコンテンツサーバー

マスターデータが置かれているサーバー, etc.

ASP.NET MVC4 Web API かWCF Data Services か

データベース(特に SQL Server )は Data Services

サービスの公開だとWeb API が有用だが、LINQ で制限あり

Data Servicesは RDBMS、Web APIはそれ以外のもの

組み合わせて使う(Windows Azureの場合はMobile Services もあり)

Page 8: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

百貨店の店舗用商品カタログのフロー例

SharePoint Site

http://sharepoint/url

se

ar

ch

Browse PageSite Actions username

Parent > Parent > Current PagePage Title

This Site: site search

Libraries

Site Pages

Shared

Documents

Drop Off

Library

Custom

library

Current PagePage One Page Two

7:37 AM マスター情報を取得

商品情報を検索

マスター情報を検索

画像 URL

を検索

画像 URL

を取得

画像 URLを含む商品情報を取得

画像情報をリクエスト

画像情報を返す

XML-RPC

REST

REST

Page 9: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 10: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 11: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Entity Framework + Code First クラスを作成してデータベースを作成

System.ComponentModel.DataAnnotations

スキーマ修正、マイグレーション可能

テストデータを入れられる

新規サービス立ち上げやマッシュアップサービスの構築ニーズに合致

ASP.NET MVC4 Web API でもWCF Data Services でも利用可能

ただし前者の場合は、使いやすいようにクラス設計をすべき

Page 12: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 13: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Web API によるサービスの公開

簡単に自動生成可能 コントローラ名決定

スキャフォールディング

オプションから選択 テンプレート

モデルクラス

データコンテキストクラス

クラスを適宜修正・追加 メソッドの追加、等

Page 14: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 15: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 16: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Kona プロジェクトとは?

Page 17: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Kona Projectとは?

CodePlexに公開されたMVVM

フレームワーク

Prism 4.5の一部を利用

C# / XAMLに特化

Windows ストア LOB アプリ開発のために最適化 設定・検索チャーム

各フレームへの遷移

バリデーション

その他順次追加予定

http://konaguidance.codeplex.com

Page 18: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

その前に Prism –元々は…

WPF, Silverlight, Windows Phone開発用

Page 19: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Prism はWindows ストアアプリ向きか?• Modularity

• UI Composition

• Region Navigation

• Decoupled

Communication

• Commands

• MVVM Support

Page 20: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 21: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介
Page 22: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

1. Windows Phone 開発の経験を十分に活用する

Windows Phone アプリ Windows ストアアプリ

Deactivate/Tombstoned/Reactiv

ate

Suspend/Terminate/Resume

Microsoft Push Notification

Service (MPN)

Windows Push Notification

Service (WNS)

Windows Phone Marketplace

certification

Windows Store app certification

& Application Excellence Review

(AER)

App manifest declares

capabilities

App manifest declares

capabilities

Page 23: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

2. AttachedBehavior にフォーカスする

• AttachedBehavior の経験を活かし、それにフォーカス 後からコントロールに動作を追加できる

• Blend のBehavior<T>

→ 使わない

• Expressionsのバインディング

→ 使わない

Page 24: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

3. Push通知には要Windows ストア開発者登録

• Windows ストアに、開発したアプリを申請し、正しいクレデンシャルを取得する

(SID 及びシークレットキー)

• 完全にサイドローディングで配布する前提で設計・開発を行ったアプリからは、WNS

(Windows Notification Service)を使ったプッシュ配信はできない→ 通知ハブ?

Page 25: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

async Task<int> AccessTheWebAsync()

{

HttpClient client = new HttpClient();

Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

DoIndependentWork();

string urlContents = await getStringTask;

return urlContents.Length;

}

4. .NET 4.5 の asyncと await を活用する

Page 26: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

ページ遷移とナビゲーション

Page 27: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Navigation support

protected virtual void GoHome(object sender, RoutedEventArgs e)

protected virtual void GoBack(object sender, RoutedEventArgs e)

protected virtual void GoForward(object sender, RoutedEventArgs e)

Visual state switching

public void StartLayoutUpdates(object sender, RoutedEventArgs e)

public void StopLayoutUpdates(object sender, RoutedEventArgs e)

Process lifetime management

protected override void OnNavigatedTo(NavigationEventArgs e)

protected override void OnNavigatedFrom(NavigationEventArgs e)

protected virtual void LoadState(Object navigationParameter,

Dictionary<String, Object> pageState)

protected virtual void SaveState(Dictionary<String, Object> pageState)

5. LayoutAwarePageクラスの使用ページ遷移、状態管理、および Visual State の管理のため

Page 28: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

XAML:

<Button Click="GoBack"

IsEnabled="{Binding Frame.CanGoBack,

ElementName=pageRoot}

"Style="{StaticResource BackButtonStyle}“

/>

<UserControl

Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates“

>

Navigation と Visual State のサポート

Page 29: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

protected override void SaveState(System.Collections.Generic.Dictionary<string, object> pageState){

var virtualizingStackPanel = VisualTreeUtilities.GetVisualChild<VirtualizingStackPanel>(itemGridView);

if (virtualizingStackPanel != null && pageState != null){

pageState["virtualizingStackPanelHorizontalOffset"] = virtualizingStackPanel.HorizontalOffset;}

}

protected override void LoadState(object navigationParameter, System.Collections.Generic.Dictionary<string, object> pageState)

{if (pageState != null && pageState.ContainsKey("virtualizingStackPanelHorizontalOffset")){

double.TryParse(pageState["virtualizingStackPanelHorizontalOffset"].ToString(),out virtualizingStackPanelHorizontalOffset);

}}

LoadStateと SaveState : SuspensionManager

Page 30: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

<VisualStateManager.VisualStateGroups>

<VisualStateGroup x:Name="ApplicationViewStates">

<VisualState x:Name="FullScreenLandscape"/>

<VisualState x:Name="Filled"/>

<VisualState x:Name="FullScreenPortrait">

<Storyboard> ... </Storyboard>

</VisualState>

<VisualState x:Name="Snapped">

<Storyboard> ... </Storyboard>

</VisualState>

</VisualStateGroup>

</VisualStateManager.VisualStateGroups>

6. Visual State のサポートによるlandscape, portrait, fill, and snap

Page 31: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

グローバル対応

Page 32: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

7.グローバル対応 -

ロケールごとに分離されたリソース

<ToolTip x:Uid=“PreviewCartoonizeAppBarButtonToolTip”Content=“Preview Cartoonization”… />

Page 33: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Model-View-ViewModel Pattern

Page 34: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

View First:this.Frame.Navigate(typeof(ItemDetailPage), itemId);

ViewModel First:

Var itemDetailPageViewModel = new ItemDetailPageViewModel(…){ ItemId = itemId };

navigationService.Navigate(itemDetailPageViewModel);

8. ページ遷移:

View First かViewModel Firstか

Page 35: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

public abstract class BindableBase : INotifyPropertyChanged{

public event PropertyChangedEventHandler PropertyChanged;

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null){

if (object.Equals(storage, value)) return false;storage = value;this.OnPropertyChanged(propertyName);return true;

}

protected void OnPropertyChanged([CallerMemberName] string propertyName = null){

var eventHandler = this.PropertyChanged;if (eventHandler != null){

eventHandler(this, new PropertyChangedEventArgs(propertyName));}

}}

9. BindableBaseクラスの利用によるINotifyPropertyChangedの提供

Page 36: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

10. Kona ViewModelLocatorの利用• Convention ベースのルックアップ

• MyNamespace.MyPage -> MyNamespace.MyPageViewModel

• ルールに基づく例外により Conventionのオーバーライドが可能

• ViewModelをインスタンス化するのにコンテナを活用可能

Page 37: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

WPF/Silverlight における典型的なバリデーション

• Implement INotifyDataErrorInfo

• UI コントロールでエラーのディクショナリにバインド

• NotifyOnValidationError=True

<TextBox Text="{Binding Id, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>

Page 38: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

View:

<TextBoxText="{Binding Address.FirstName, Mode=TwoWay}" behaviors:HighlightFormFieldOnErrors.PropertyErrors=

"{Binding Errors[FirstName]}" />

-----------------------------------------------------------------------------

ViewModel:

_bindableValidator = new BindableValidator(_address);

public BindableValidator Errors{

get { return _bindableValidator; }}

11. Kona BindableValidatorの利用

Page 39: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Decoupled Eventing• “ハリウッド式ペアレンタルスタイルの UI コンポジション”

(ユーザーコントロール)

• 子コントロールは、長時間持続するサービスにより発生したイベントをリッスンする必要があるも、自らそれを、取り外すことはできない

• Prism の EventAggregator部分をポーティング

Page 40: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

12. 必要に応じて EventAggregator使用public SubscriberViewModel(IEventAggregator eventAggregator){

eventAggregator.GetEvent<ShoppingCartUpdatedEvent>()

.Subscribe(s => UpdateItemCountAsync());}

public PublisherViewModel(IEventAggregator eventAggregator){

_eventAggregator = eventAggregator;}

_eventAggregator.GetEvent<ShoppingCartUpdatedEvent>()

.Publish(string.Empty);

Page 41: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Commanding と ViewModel Method Invocation

ICommand:

void Execute(object)

bool CanExecute(object)

event EventHandler CanExecuteChanged

Command Invoker:

ButtonBase

-----------------------------------------------------

Event -> Action

Page 42: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

13. コントロールにICommandをサポートする DelegateCommandを使用

View:

<Button Content=“Go to shopping cart”Command="{Binding ShoppingCartNavigationCommand}" />

---------------------------------------------------------------------

ViewModel:

ShoppingCartNavigationCommand = new DelegateCommand(NavigateToShoppingCartPage,

CanNavigateToShoppingCartPage);

ShoppingCartNavigationCommand.RaiseCanExecuteChanged();

Page 43: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

14. AttachedBehavior や Action を使用する

View:

<GridView x:Name="itemGridView“

ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"

ItemTemplate="{StaticResource KonaRI250x250ItemTemplate}"

SelectionMode="None“ IsItemClickEnabled="True"

behaviors:ListViewItemClickedToAction.Action=

"{Binding CategoryNavigationAction}">

----------------------------------------------------------------------------------

ViewModel:

CategoryNavigationAction = NavigateToCategory;

Page 44: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Suspend, Resume, 及びTerminate

Page 45: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

15. Kona RestorableStateAttributeとMVVM フレームワークの利用

public class MyViewModel : ViewModel, INavigationAware{

private string _name;

[RestorableState]public string Name

{get { return _name; }set { SetProperty(ref _name, value); }

}}

Page 46: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

WindowsAzure Store

WindowsPhone Store

16. Visual Studio との統合

Page 47: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

ALM ソリューションの提供

Page 48: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

ALM ソリューションの提供

Page 49: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Visual Studio Ultimate 2012アプリケーション ライフサイクル全体を包括的にカバー

クライアント

サーバー

Visual Studio Team Foundation Server 2012アプリケーション開発プロジェクトの統合開発基盤

Visual Studio Premium 2012統合開発環境とテスト機能を搭載

Visual Studio Professional 2012開発機能を提供する統合開発環境

Visual Studio Test Professional 2012包括的なテストの実施と管理を支援

Page 50: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

参考情報 Visual Studio 2012 導入事例

http://aka.ms/VS2012-Case

Visual Studio 2012 が提供する統合ソリューション

http://aka.ms/VS2012-Sol

Visual Studio 2012 製品ガイド(PDF, 16MB)

http://aka.ms/VS2012-Prod

Visual Studio 2012 試用版

http://aka.ms/VS2012-Try

Visual Studio 2012 ライセンスホワイトペーパー

http://aka.ms/VS2012-Lic

Page 51: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介

Resource

http://konaguidance.codeplex.com

英語版 OS でないと .exe は解凍できない場合あり

Source Code → Download で .zip を落としてください

Page 52: XAML と C# を使った Windows ストアアプリ(LOB)構築のためのtips   Prism 4.5 & Kona project 等のご紹介