{"id":345,"date":"2012-08-20T23:36:03","date_gmt":"2012-08-20T14:36:03","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2012\/08\/20\/silverlight_prism_mvvm_3_web\/"},"modified":"2012-08-20T23:36:03","modified_gmt":"2012-08-20T14:36:03","slug":"silverlight_prism_mvvm_3_web","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2012\/08\/20\/silverlight_prism_mvvm_3_web\/","title":{"rendered":"Silverlight Prism \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5229\u7528\u3057\u3066MVVM\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b \u7b2c3\u56de Web\u30b5\u30fc\u30d3\u30b9\u3068\u306e\u9023\u643a"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2012\/08\/18\/silverlight_prism_mvvm_1\">Silverlight Prism \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5229\u7528\u3057\u3066MVVM\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b \u7b2c1\u56de<\/a> <\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2012\/08\/19\/silverlight_prism_mvvm_2_mvvm\">Silverlight Prism \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5229\u7528\u3057\u3066MVVM\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b \u7b2c2\u56de MVVM\u30d1\u30bf\u30fc\u30f3\u306e\u4f5c\u6210<\/a> <\/li>\n<\/ul>\n<p>\u7b2c1\u56de\u3067\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u5206\u5272\u3068\u3001UI\u5408\u6210\u306e\u52d5\u4f5c\u3092\u78ba\u8a8d\u3001\u7b2c2\u56de\u3067\u3001MVVM\u30d1\u30bf\u30fc\u30f3\u306e\u9aa8\u683c\u304c\u3067\u304d\u305f\u306e\u3067\u3001\u7b2c2\u56de\u306b\u30c0\u30df\u30fc\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u3044\u305f\u500b\u6240\u3092\u3001Web\u30b5\u30fc\u30d3\u30b9\u304b\u3089\u53d6\u5f97\u3059\u308b\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3001\u975e\u540c\u671f\u51e6\u7406\u306b\u5909\u66f4\u3057\u3066\u3044\u304f\u3002<\/p>\n<h2>\u975e\u540c\u671f\u901a\u4fe1\u306e\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0<\/h2>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/gg405484(v=pandp.40).aspx\">Implementing the MVVM Pattern<\/a><\/p>\n<p>View Model \u306f\u3001\u975e\u540c\u671f\u3067\u30b5\u30fc\u30d3\u30b9\u3084\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3068\u901a\u4fe1\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u304c\u6709\u308b\u3002\u30e6\u30fc\u30b6\u30fc\u304c\u3001\u975e\u540c\u671f\u30ea\u30af\u30a8\u30b9\u30c8\u3082\u3057\u304f\u306f\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u30bf\u30b9\u30af\u3092\u958b\u59cb\u3059\u308b\u5834\u5408\u3001\u5fdc\u7b54\u304c\u3044\u3064\u5230\u7740\u3059\u308b\u306e\u304b\u3092\u4e88\u6e2c\u3059\u308b\u306e\u306f\u96e3\u3057\u3044\u3002UI\u306f\u3001UI\u30b9\u30ec\u30c3\u30c9\u304b\u3089\u306e\u307f\u66f4\u65b0\u3055\u308c\u308b\u306e\u3067\u3001UI\u30b9\u30ec\u30c3\u30c9\u304b\u3089\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3055\u308c\u305f\u30ea\u30af\u30a8\u30b9\u30c8\u3067UI\u3092\u66f4\u65b0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<h3>\u30c7\u30fc\u30bf\u306e\u53c2\u7167\u3068Web\u30b5\u30fc\u30d3\u30b9\u3068\u306e\u901a\u4fe1<\/h3>\n<p>Web\u30b5\u30fc\u30d3\u30b9\u3084\u305d\u306e\u4ed6\u306e\u30ea\u30e2\u30fc\u30c8\u30a2\u30af\u30bb\u30b9\u901a\u4fe1\u3092\u884c\u3046\u5834\u5408\u3001IAsyncResult \u30d1\u30bf\u30fc\u30f3\u306b\u305f\u3069\u308a\u7740\u304f\u3060\u308d\u3046\u3002\u3053\u306e\u30d1\u30bf\u30fc\u30f3\u306f\u3001<strong>GetXxxxx<\/strong> \u3068\u3044\u3063\u305f\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001\u30e1\u30bd\u30c3\u30c9\u306e\u30da\u30a2\u3001<strong>BeginGetXxxxx<\/strong>\u304a\u3088\u3073 <strong>EndGetXxxxx<\/strong>\u3092\u5229\u7528\u3059\u308b\u3002\u975e\u540c\u671f\u547c\u3073\u51fa\u3057\u3092\u958b\u59cb\u3059\u308b\u306b\u306f\u3001<strong>BeginGetXxxxx<\/strong>\u3092\u547c\u3073\u51fa\u3057\u3001\u7d50\u679c\u3092\u53d6\u5f97\u3057\u305f\u308a\u3001\u30e1\u30bd\u30c3\u30c9\u547c\u3073\u51fa\u3057\u306e\u4f8b\u5916\u3092\u78ba\u8a8d\u3059\u308b\u306b\u306f\u3001\u547c\u3073\u51fa\u3057\u304c\u5b8c\u4e86\u3057\u305f\u3068\u304d\u306b\u3001<strong>EndGetXxxxx<\/strong>\u3092\u547c\u3073\u51fa\u3059\u3002<\/p>\n<p><strong>EndGetXXXXX <\/strong>\u3044\u3064\u547c\u3073\u51fa\u3059\u304b\u6c7a\u5b9a\u3059\u308b\u306b\u306f\u3001\u5b8c\u4e86\u3092\u30dd\u30fc\u30ea\u30f3\u30b0\u3059\u308b\u304b\u3001\u3067\u304d\u308c\u3070\u3001<strong>BeginGetXxxxx <\/strong>\u3092\u547c\u3073\u51fa\u3059\u3068\u304d\u306b\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u8a18\u8ff0\u3059\u308b\u306e\u304c\u671b\u307e\u3057\u3044\u3002<\/p>\n<pre class=\"prettyprint linenums\">IAsyncResult asyncResult = this.service.BeginGetQuestionnaire(GetQuestionnaireCompleted, null);\r\n\r\nprivate void GetQuestionnaireCompleted(IAsyncResult result)\r\n{\r\n   try\r\n   {\r\n     questionnaire = this.service.EndGetQuestionnaire(ar);\r\n   }\r\n   catch (Exception ex)\r\n   {\r\n     \/\/ Do something to report the error.\r\n   }\r\n}<\/pre>\n<p><strong>EndGetXxxxx<\/strong>\u30e1\u30bd\u30c3\u30c9\u306e\u547c\u3073\u51fa\u3057\u3067\u306f\u3001\u3069\u306e\u3088\u3046\u306a\u4f8b\u5916\u3082\u8d77\u3053\u308a\u3046\u308b\u305f\u3081\u3001\u3053\u308c\u3089\u3092\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3059\u308b\u5fc5\u8981\u304c\u6709\u308a\u3001\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u306a\u65b9\u6cd5\u3067UI\u3092\u901a\u3057\u3066\u30ec\u30dd\u30fc\u30c8\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u306f\u3001\u901a\u5e38UI\u30b9\u30ec\u30c3\u30c9\u4e0a\u306b\u7121\u3044\u305f\u3081\u3001UI\u306e\u72b6\u614b\u306b\u4f55\u3089\u304b\u5909\u66f4\u3092\u52a0\u3048\u305f\u3044\u5834\u5408\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3092UI\u30b9\u30ec\u30c3\u30c9\u306b\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3053\u306e\u305f\u3081\u306b\u3001\u30b9\u30ec\u30c3\u30c9 <strong>Dispatcher <\/strong>\u3082\u3057\u304f\u306f\u3001<strong>SynchronizationContext <\/strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5229\u7528\u3059\u308b\u3002 Silverlight \u3067\u306f\u901a\u5e38\u3001Dispatcer \u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001<strong>Questionnaire <\/strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u975e\u540c\u671f\u306b\u53c2\u7167\u3055\u308c\u3001<strong>QuestionnaireView <\/strong>\u306e\u30c7\u30fc\u30bf\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306b\u8a2d\u5b9a\u3055\u308c\u308b\u3002 Silverlight \u3067\u306f\u3001Dispatcher\u306e <strong>CheckAccess <\/strong>\u30e1\u30bd\u30c3\u30c9 \u3092 UI\u30b9\u30ec\u30c3\u30c9\u306b\u5c5e\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3059\u308b\u305f\u3081\u306b\u5229\u7528\u3067\u304d\u308b\u3002UI\u30b9\u30ec\u30c3\u30c9\u306b\u7121\u3044\u5834\u5408\u3001<strong>BeginInvoke <\/strong>\u30e1\u30bd\u30c3\u30c9 \u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">var dispatcher = System.Windows.Deployment.Current.Dispatcher;\r\nif (dispatcher.CheckAccess())\r\n{\r\n    QuestionnaireView.DataContext = questionnaire;\r\n}\r\nelse\r\n{\r\n    dispatcher.BeginInvoke(\r\n          () =&gt; { Questionnaire.DataContext = questionnaire; });\r\n}<\/pre>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2012\/08\/05\/prism_visual_web_developer_2010_express\">Prism \u306b\u542b\u307e\u308c\u308b<\/a>\u3001Model-VIew-ViewModel \u53c2\u7167\u5b9f\u88c5(MVVM RI)\u3067\u306f\u3001\u3069\u306e\u3088\u3046\u306b\u3001IAsyncResult \u30d9\u30fc\u30b9\u306e\u30b5\u30fc\u30d3\u30b9\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5229\u7528\u3059\u308b\u304b\u306e\u30b5\u30f3\u30d7\u30eb\u304c\u3042\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">this.questionnaireRepository.GetQuestionnaireAsync(\r\n    (result) =&gt;\r\n    {\r\n        this.Questionnaire = result.Result;\r\n    });<\/pre>\n<p><strong>Result <\/strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u767a\u751f\u3057\u305f\u30a8\u30e9\u30fc\u3082\u30e9\u30c3\u30d7\u3057\u3066\u3044\u308b\u3002\u30a8\u30e9\u30fc\u3092\u8a55\u4fa1\u3059\u308b\u4f8b<\/p>\n<pre class=\"prettyprint linenums\">this.questionnaireRepository.GetQuestionnaireAsync(\r\n    (result) =&gt;\r\n    {\r\n        if (result.Error == null) {\r\n          this.Questionnaire = result.Result;\r\n          ...\r\n        }\r\n        else\r\n        {  \r\n          \/\/ Handle error. \r\n        }\r\n    })<\/pre>\n<h2>\u975e\u540c\u671f\u51e6\u7406\u306e\u5b9f\u88c5<\/h2>\n<h3>AsyncResult \u30af\u30e9\u30b9\u306e\u4f5c\u6210<\/h3>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/ms228969(v=vs.80).aspx\">\u975e\u540c\u671f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u30c7\u30b6\u30a4\u30f3 \u30d1\u30bf\u30fc\u30f3<\/a> <\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/magazine\/cc163467.aspx\">CLR \u306e\u975e\u540c\u671f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 \u30e2\u30c7\u30eb\u3092\u5b9f\u88c5\u3059\u308b<\/a> <\/li>\n<\/ul>\n<p>BeginXxxxx \u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u969b\u306b\u3001\u305d\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u5185\u90e8\u7684\u306bIAsyncResult\u3092\u5b9f\u88c5\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u69cb\u7bc9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002IAsyncResult \u3092\u5b9f\u88c5\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u306f\u3001\u4ee5\u4e0b\u306e4\u3064\u306e\u8aad\u307f\u53d6\u308a\u5c02\u7528\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u5b9f\u88c5\u3059\u308b\u3002<\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/query\/dev10.query?appId=Dev10IDEF1&amp;l=JA-JP&amp;k=k(SYSTEM.IASYNCRESULT);k(TargetFrameworkMoniker-%22SILVERLIGHT%2cVERSION%3dV5.0%22);k(DevLang-CSHARP)&amp;rd=true\">IAsyncResult<\/a><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"535\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"200\">\u540d\u524d<\/td>\n<td valign=\"top\" width=\"333\">\u8aac\u660e<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"200\">AsyncState<\/td>\n<td valign=\"top\" width=\"333\">\u975e\u540c\u671f\u64cd\u4f5c\u306b\u3064\u3044\u3066\u306e\u60c5\u5831\u3092\u9650\u5b9a\u307e\u305f\u306f\u683c\u7d0d\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u5b9a\u7fa9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"200\">AsyncWaitHandle<\/td>\n<td valign=\"top\" width=\"333\">\u975e\u540c\u671f\u64cd\u4f5c\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u5f85\u6a5f\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3059\u308b <a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/system.threading.waithandle(v=vs.95)\">WaitHandle<\/a> \u3092\u53d6\u5f97<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"200\">CompletedSynchronously<\/td>\n<td valign=\"top\" width=\"333\">\u975e\u540c\u671f\u64cd\u4f5c\u304c\u540c\u671f\u7684\u306b\u5b8c\u4e86\u3057\u305f\u304b\u3069\u3046\u304b\u3092\u793a\u3059\u5024\u3092\u53d6\u5f97<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"200\">IsCompleted<\/td>\n<td valign=\"top\" width=\"333\">\u975e\u540c\u671f\u64cd\u4f5c\u304c\u5b8c\u4e86\u3057\u305f\u304b\u3069\u3046\u304b\u3092\u793a\u3059\u5024\u3092\u53d6\u5f97<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#160;<\/p>\n<p>\u5b9f\u88c5\u306e\u5185\u5bb9\u306f\u3001MVVM RI \u3092\u53c2\u8003\u306b\u3002<\/p>\n<p><strong>AsyncResult.cs<\/strong><\/p>\n<pre class=\"prettyprint linenums\">using System;\r\nusing System.Threading;\r\n\r\nnamespace ProjectModule.Services\r\n{\r\n    public class AsyncResult<t> : IAsyncResult\r\n    {\r\n        private readonly object lockObject;\r\n        private readonly AsyncCallback asyncCallback;\r\n        private readonly object asyncState;\r\n        private ManualResetEvent waitHandle;\r\n        private bool completedSynchronously;\r\n        private bool isCompleted;\r\n        private T result;\r\n        private bool endCalled;\r\n        private Exception exception;\r\n\r\n        public AsyncResult(AsyncCallback asyncCallback, object asyncState)\r\n        {\r\n            this.lockObject = new object();\r\n            this.asyncCallback = asyncCallback;\r\n            this.asyncState = asyncState;\r\n        }\r\n\r\n        public T Result\r\n        {\r\n            get { return this.result; }\r\n        }\r\n\r\n        public static AsyncResult<t> End(IAsyncResult asyncResult)\r\n        {\r\n            var localResult = asyncResult as AsyncResult<t>;\r\n            if (localResult == null)\r\n            {\r\n                throw new ArgumentNullException(&quot;asyncResult&quot;);\r\n            }\r\n\r\n            lock (localResult.lockObject)\r\n            {\r\n                if (localResult.endCalled)\r\n                {\r\n                    throw new InvalidOperationException(&quot;End method already called&quot;);\r\n                }\r\n\r\n                localResult.endCalled = true;\r\n            }\r\n\r\n            if (!localResult.IsCompleted)\r\n            {\r\n                localResult.AsyncWaitHandle.WaitOne();\r\n            }\r\n\r\n            if (localResult.waitHandle != null)\r\n            {\r\n                localResult.waitHandle.Close();\r\n            }\r\n\r\n            if (localResult.exception != null)\r\n            {\r\n                throw localResult.exception;\r\n            }\r\n\r\n            return localResult;\r\n        }\r\n\r\n        public void SetComplete(T result, bool completedSynchronously)\r\n        {\r\n            this.result = result;\r\n\r\n            this.DoSetComplete(completedSynchronously);\r\n        }\r\n\r\n        public void SetComplete(Exception e, bool completedSynchronously)\r\n        {\r\n            this.exception = e;\r\n\r\n            this.DoSetComplete(completedSynchronously);\r\n        }\r\n\r\n        private void DoSetComplete(bool completedSynchronously)\r\n        {\r\n            if (completedSynchronously)\r\n            {\r\n                this.completedSynchronously = true;\r\n                this.isCompleted = true;\r\n            }\r\n            else\r\n            {\r\n                lock (this.lockObject)\r\n                {\r\n                    this.isCompleted = true;\r\n                    if (this.waitHandle != null)\r\n                    {\r\n                        this.waitHandle.Set();\r\n                    }\r\n                }\r\n            }\r\n\r\n            if (this.asyncCallback != null)\r\n            {\r\n                this.asyncCallback(this);\r\n            }\r\n        }\r\n\r\n        #region Implementation of IAsyncResult\r\n        \r\n        public object AsyncState\r\n        {\r\n            get { return this.asyncState; }\r\n        }\r\n\r\n        public WaitHandle AsyncWaitHandle\r\n        {\r\n            get\r\n            {\r\n                lock (this.lockObject)\r\n                {\r\n                    if (this.waitHandle == null)\r\n                    {\r\n                        this.waitHandle = new ManualResetEvent(this.IsCompleted);\r\n                    }\r\n                }\r\n\r\n                return this.waitHandle;\r\n            }\r\n        }\r\n\r\n        public bool CompletedSynchronously\r\n        {\r\n            get { return this.completedSynchronously; }\r\n        }\r\n\r\n        public bool IsCompleted\r\n        {\r\n            get { return this.isCompleted; }\r\n        }\r\n\r\n        #endregion\r\n    }\r\n}<\/pre>\n<h3>\u30c7\u30fc\u30bf\u30b5\u30fc\u30d3\u30b9\u306e\u4f5c\u6210<\/h3>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/gg405484(v=pandp.40).aspx\">Implementing the MVVM Pattern<\/a> \u306e\u8a18\u8ff0\u3092\u53c2\u8003\u306b\u3001\u30c7\u30fc\u30bf\u53d6\u5f97\u30b5\u30fc\u30d3\u30b9\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u3001BeginGetXxxxx \u30e1\u30bd\u30c3\u30c9\u3068\u3001EndGetXxxxx \u3092\u8ffd\u52a0\u3059\u308b(\u5143\u3005\u306eGetXxxxx \u306f\u524a\u9664)<\/p>\n<p><strong>IProjectDataService.cs<\/strong><\/p>\n<pre class=\"prettyprint linenums\">using System;\r\nusing ProjectModule.Models;\r\n\r\nnamespace ProjectModule.Services\r\n{\r\n    public interface IProjectDataService\r\n    {\r\n        IAsyncResult BeginGetProjects(AsyncCallback callback, object userState);\r\n        Projects EndGetProjects(IAsyncResult asyncResult);\r\n    }\r\n}<\/pre>\n<p>\u4e0a\u8a18\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u305f\u30af\u30e9\u30b9\u3092\u4f5c\u6210<\/p>\n<ul>\n<li>BeginGetProjects \u30e1\u30bd\u30c3\u30c9\u306e\u306a\u304b\u3067\u64ec\u4f3c\u7684\u306b\u9045\u5ef6\u3092\u3055\u305b\u308b\u305f\u3081\u306b\u3001Thread.Sleep \u3092\u5b9f\u65bd\u3057\u305f\u5f8c\u3001AsyncResult\u306eSetComplete\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3002SetComplete\u306b\u6e21\u3059\u7d50\u679c(CreateProjects\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024)\u304c\u3001Result \u3068\u3057\u3066\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b <\/li>\n<li>CreateProjects\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2012\/08\/19\/silverlight_prism_mvvm_2_mvvm\">\u7b2c2\u56de<\/a>\u306e\u307e\u307e\u3001\u30c0\u30df\u30fc\u306e\u30c7\u30fc\u30bf\u3092\u8fd4\u3059\u307e\u307e\u3068\u3057\u3066\u304a\u304f\u3002 <\/li>\n<li>EndGetProjects\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u3001\u7d50\u679c\u3092\u53d6\u308a\u51fa\u3057\u8fd4\u5374 <\/li>\n<\/ul>\n<p><strong>ProjectDataService.cs<\/strong><\/p>\n<pre class=\"prettyprint linenums\">using System;\r\nusing System.ComponentModel.Composition;\r\nusing ProjectModule.Models;\r\nusing System.Collections.Generic;\r\nusing System.Threading;\r\nusing System.Net;\r\n\r\nnamespace ProjectModule.Services\r\n{\r\n\r\n    [Export(typeof(IProjectDataService))]\r\n    public class ProjectDataService : IProjectDataService\r\n    {\r\n        private Projects projects;\r\n\r\n        public IAsyncResult BeginGetProjects(AsyncCallback callback, object userState)\r\n        {\r\n            var asyncResult = new AsyncResult<projects>(callback, userState);\r\n            ThreadPool.QueueUserWorkItem(\r\n                o =&gt;\r\n                {\r\n                    Thread.Sleep(1500);\r\n                    asyncResult.SetComplete(CreateProjects(), false);\r\n                });\r\n\r\n            return asyncResult;\r\n        }\r\n\r\n        public Projects CreateProjects()\r\n        {\r\n            if (this.projects == null)\r\n            {\r\n                this.projects = new Projects { \r\n                    new Project(){\r\n                        Key = &quot;001&quot;,\r\n                        SystemName = &quot;Sys2&quot;,\r\n                        ApplicationName=&quot;Test App1&quot;\r\n                    },\r\n                    new Project(){\r\n                        Key = &quot;002&quot;,\r\n                        SystemName = &quot;Sys2&quot;,\r\n                        ApplicationName=&quot;Test App2&quot;\r\n                    },\r\n                    new Project(){\r\n                        Key = &quot;003&quot;,\r\n                        SystemName = &quot;Sys2&quot;,\r\n                        ApplicationName=&quot;Test App3&quot;\r\n                    },\r\n                };\r\n            }\r\n            return this.projects;\r\n        }\r\n\r\n\r\n        Projects IProjectDataService.EndGetProjects(IAsyncResult asyncResult)\r\n        {\r\n            var localAsyncResult = AsyncResult<projects>.End(asyncResult);\r\n\r\n            return localAsyncResult.Result;\r\n        }\r\n    }\r\n}<\/pre>\n<h3>ViewModel\u306e\u5909\u66f4<\/h3>\n<ul>\n<li>\u4eca\u307e\u3067\u3001\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u3001\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3068\u8868\u793a\u3092\u884c\u3063\u3066\u3044\u305f\u306e\u3092\u3001\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u306f\u3001\u4e0a\u8a18\u3067\u4f5c\u6210\u3057\u305f\u3001BeginGetProjects \u30e1\u30bd\u30c3\u30c9\u3092\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af GetProjectCompleted \u3092\u6307\u5b9a\u3057\u3066\u975e\u540c\u671f\u3067\u547c\u3073\u51fa\u3059 <\/li>\n<li>GetProjectCompleted \u306f\u3001\u51e6\u7406\u304c\u7d42\u4e86\u3057\u305f\u3089\u3001AsyncResult \u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u308b <\/li>\n<li>\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u3001GetProjectCompleted \u304c\u547c\u3073\u51fa\u3055\u308c\u305f\u3089\u3001EndGetProjects \u3092\u547c\u3073\u51fa\u3057\u3066\u3001\u7d50\u679c\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3002 <\/li>\n<li>\u3053\u306e\u307e\u307e\u3060\u3068\u3001\u300c\u30ec\u30b9\u30dd\u30f3\u30b9\u306f\u3001\u901a\u5e38UI\u30b9\u30ec\u30c3\u30c9\u4e0a\u306b\u7121\u3044\u305f\u3081\u3001UI\u306e\u72b6\u614b\u306b\u4f55\u3089\u304b\u5909\u66f4\u3092\u52a0\u3048\u305f\u3044\u5834\u5408\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u3092UI\u30b9\u30ec\u30c3\u30c9\u306b\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u3059\u308b\u5fc5\u8981\u300d\u304c\u3042\u308b\u305f\u3081\u3001\n<p>var dispatcher = System.Windows.Deployment.Current.Dispatcher; \u4ee5\u4e0b\u306e\u5224\u5b9a\u3067\u3001UI\u30b9\u30ec\u30c3\u30c9\u304b\u5426\u304b\u306b\u3088\u3063\u3066\u3001\u30d7\u30ed\u30d1\u30c6\u30a3\u5909\u66f4\u901a\u77e5\u3092 NotifyPropertyChanged \u30e1\u30bd\u30c3\u30c9\u3092\u76f4\u63a5\u547c\u3076\u304b\u3001\u30c7\u30a3\u30b9\u30d1\u30c3\u30c1\u30e3\u306b\u547c\u3073\u51fa\u3057\u3092\u4efb\u305b\u308b\u304b\u5224\u5b9a\u3057\u3066\u3001\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u5909\u66f4\u3092View \u306b\u901a\u77e5<\/p>\n<\/li>\n<\/ul>\n<pre class=\"prettyprint linenums\">using System;\r\nusing System.ComponentModel;\r\nusing System.ComponentModel.Composition;\r\nusing System.Windows.Data;\r\nusing Microsoft.Practices.Prism.Events;\r\nusing ProjectModule.Events;\r\nusing ProjectModule.Models;\r\nusing ProjectModule.Services;\r\n\r\nnamespace ProjectModule.ViewModels\r\n{\r\n    \r\n    [Export]\r\n    public class ProjectListViewModel : INotifyPropertyChanged\r\n    {\r\n\r\n        private readonly IEventAggregator eventAggregator;\r\n        private IProjectDataService dataService;\r\n        public ICollectionView Projects { get; set;}\r\n\r\n        [ImportingConstructor]\r\n        public ProjectListViewModel(IProjectDataService dataService, IEventAggregator eventAggregator)\r\n        {\r\n            if (dataService == null) throw new ArgumentNullException(&quot;dataService&quot;);\r\n            if (eventAggregator == null) throw new ArgumentNullException(&quot;eventAggregator&quot;);\r\n\r\n            this.eventAggregator = eventAggregator;\r\n            this.dataService = dataService;\r\n\r\n            IAsyncResult asyncResult = this.dataService.BeginGetProjects(GetProjectsCompleted, null);\r\n        }\r\n\r\n        private void GetProjectsCompleted(IAsyncResult result)\r\n        {\r\n            try\r\n            {\r\n                this.Projects = new PagedCollectionView(this.dataService.EndGetProjects(result));\r\n                \r\n                if (this.Projects != null)\r\n                {\r\n                    this.Projects.CurrentChanged += new EventHandler(this.SelectedProjectChanged);\r\n                }\r\n                var dispatcher = System.Windows.Deployment.Current.Dispatcher;\r\n                if (dispatcher.CheckAccess())\r\n                {\r\n                    this.NotifyPropertyChanged(&quot;Projects&quot;);\r\n                }\r\n                else\r\n                {\r\n                    dispatcher.BeginInvoke(\r\n                          () =&gt; { this.NotifyPropertyChanged(&quot;Projects&quot;); });\r\n                }\r\n  \r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                \/\/ TODO\r\n            }\r\n        }\r\n\r\n\r\n        private void SelectedProjectChanged(object sender, EventArgs e)\r\n        {\r\n            Project project = this.Projects.CurrentItem as Project;\r\n            if (project != null)\r\n            {\r\n                this.eventAggregator.GetEvent<projectselectedevent>().Publish(project.Key);\r\n            }\r\n        }\r\n        \r\n        #region INotifyPropertyChanged Members\r\n\r\n        public event PropertyChangedEventHandler PropertyChanged;\r\n\r\n        private void NotifyPropertyChanged(string propertyName)\r\n        {\r\n            if (this.PropertyChanged != null)\r\n            {\r\n                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));\r\n            }\r\n        }\r\n        \r\n        #endregion\r\n\r\n    }\r\n}<\/pre>\n<h3>\u5b9f\u884c<\/h3>\n<p>\u8868\u793a\u7d50\u679c\u306f\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2012\/08\/19\/silverlight_prism_mvvm_2_mvvm\">\u7b2c2\u56de<\/a> \u3068\u5909\u308f\u3089\u306a\u3044\u304c\u3001\u5185\u90e8\u7684\u306b\u306f\u3001\u30c7\u30fc\u30bf\u3092\u975e\u540c\u671f\u3067\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/SilverlightPrismMVVM2_9DF0\/prism_handson16.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"prism_handson16\" border=\"0\" alt=\"prism_handson16\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/SilverlightPrismMVVM2_9DF0\/prism_handson16_thumb.png\" width=\"344\" height=\"207\" \/><\/a><\/p>\n<p><iframe style=\"width: 120px; height: 240px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea-pm-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4798113468\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><iframe style=\"width: 120px; height: 240px\" marginheight=\"0\" src=\"http:\/\/rcm-jp.amazon.co.jp\/e\/cm?lt1=_blank&amp;bc1=FFFFFF&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=typea-pm-22&amp;o=9&amp;p=8&amp;l=as4&amp;m=amazon&amp;f=ifr&amp;ref=ss_til&amp;asins=4873115655\" frameborder=\"0\" marginwidth=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Silverlight Prism \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5229\u7528\u3057\u3066MVVM\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b \u7b2c1\u56de Silverlight Prism \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u5229\u7528\u3057\u3066MVVM\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3066\u307f\u308b \u7b2c2\u56de MV [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[55,54],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-prism","category-silverlight"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}