Public Class TouristInfo
Property name As String
Property title As String
Property description As String
Property url As String
End Class
Public NotInheritable Class MainPage
Inherits Page<
TouristInfoクラスのnameプロパティに要素の値を、titleプロパティに要素の値を、descriptionプロパティに<description>要素の値を、urlプロパティに<url>要素の値を指定して、AddメソッドでmyTouristInfoオブジェクトに追加していきます。<br> GridViewのItemsSourceプロパティに変数myTouristInfoオブジェクト値を指定します。これでタイルの中にname、title、descriptionの内容が表示されます。TitleTextBlockにComboBoxより選択した地域名を「の観光地」という文字列と連結して表示します。</p>
<pre class="brush: plain; " type="syntaxhighlighter"> Private Async Sub ComboBox1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged Dim areaName = DirectCast(ComboBox1.SelectedItem, ComboBoxItem).Content.ToString Select Case areaName Case "アジア" countryName = "AAS" Case "ビーチリゾート" countryName = "BCH" Case "ハワイ" countryName = "HWI" Case "ヨーロッパ" countryName = "EUR" Case "アメリカ" countryName = "DUS" Case "オセアニア" countryName = "FOC" Case "アフリカ・その他" countryName = "CAF" End Select Dim myUrl = String.Format("http://webservice.recruit.co.jp/ab-road/spot/v1/?key={0}&area={1}&count=100&format=xml", ApiKey, countryName) Dim myHttpClient As New HttpClient Dim myResult As String = Await myHttpClient.GetStringAsync(myUrl) myResult = myResult.Replace("<results xmlns=" &ChrW(34) & "http://webservice.recruit.co.jp/ab-road/" &ChrW(34) & ">", "<results>") Dim xmldoc As XElement = XElement.Parse(myResult) Dim myTouristInfo As New List(Of TouristInfo) For Each result In From c In xmldoc.Descendants("spot") Select c With myTouristInfo .Add(New TouristInfo With {.name = result.Element("name").Value, .title = result.Element("title").Value, .description = result.Element("description").Value, .url = result.Element("url").Value}) End With Next GridView1.ItemsSource = myTouristInfo TitleTextBlock.Text = areaName& "の観光地" End Sub
</pre>
<h4>GridViewの項目が選択された時の処理</h4>
<p>Frameを表示状態にします。<br> ComboBoxを選択不可とします。<br> 変数myUrlにGridViewから選択された項目を、TouristInfoクラスにキャストして、そのurlプロパティの値を取得し格納します。</p>
<p>変数myUrlの値を引数にWebBrowserPageに遷移します。</p>
<p>例外が発生した場合は、ComboBoxの選択を可能にし、Frameを非表示とします。</p>
<pre class="brush: plain; " type="syntaxhighlighter"> Private Sub GridView1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles GridView1.SelectionChanged Try myFrame.Visibility = Windows.UI.Xaml.Visibility.Visible ComboBox1.IsEnabled = False Dim myUrl As String = DirectCast(GridView1.SelectedItem, TouristInfo).url myFrame.Navigate(GetType(WebBrowserPage), myUrl) Catch ComboBox1.IsEnabled = True myFrame.Visibility = Windows.UI.Xaml.Visibility.Collapsed End Try End Sub
</pre>
<h4>戻る(←)アイコンがタップされた時の処理</h4>
<p>ComboBoxの使用を可能とし、Frameを非表示にします。</p>
<pre class="brush: plain; " type="syntaxhighlighter"> Private Sub backButton_Click(sender As Object, e As RoutedEventArgs) Handles backButton.Click ComboBox1.IsEnabled = True myFrame.Visibility = Windows.UI.Xaml.Visibility.Collapsed End Sub
End Class
</pre>
<p>次に、ソリューションエクスプローラー内のWebBrowserPage.xamlを展開して表示される、WebBrowserPage.xaml.vbをダブルクリックしてリスト4のコードを記述します。</p>
<h3>ロジックコードを記述する</h3>
<h4>リスト4 (WebBrowserPage.xaml.vb)</h4>
<pre class="brush: plain; " type="syntaxhighlighter">Option Strict On
Public NotInheritable Class WebBrowserPage Inherits Page
</pre>
<h4>ページがアクティブになった時の処理</h4>
<p>MainPage.xamlから渡された引数(myUrl)は、e.Parameterで取得できます。これはObject型であるため、DirectCastでString型にキャストして、変数myUriに格納します。</p>
<p>WebBrowserのSourceプロパティにmyUriの値を指定します。これでWebBrowser内に、該当する観光地の情報が表示されます。</p>
<pre class="brush: plain; " type="syntaxhighlighter"> Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs) Dim myUri As String = DirectCast(e.Parameter, String) WebBrowser1.Source = New Uri(myUri, UriKind.Absolute) End Sub
End Class
</pre>
<p>今回でこの連載は終了です。長い間おつきあいありがとうました。<br> 次回の連載も、ぜひ楽しみに待っていてくだされば嬉しいです。</p>
<h3>筆者からのお知らせ</h3>
<p>筆者はWindowsストアでアプリを公開しています。チャームの検索からWindowsストアを選択して、検索欄に、kuniyasuまたはYakushijiKuniyasuと入力すると、公開されているアプリの一覧が表示されます。上記はどちらも私のアカウントですので、興味のある方は是非ダウンロードして使ってみてください。</p>
</body></html>