Skip to content

Instantly share code, notes, and snippets.

@3t14
Last active January 21, 2017 01:05
Show Gist options
  • Save 3t14/4047910874d1e998a439c6cd3f3d8cfe to your computer and use it in GitHub Desktop.
Save 3t14/4047910874d1e998a439c6cd3f3d8cfe to your computer and use it in GitHub Desktop.
ListView + Navigation + async + awaitの例
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace HelloListView
{
public class App : Application
{
public App()
{
// ListViewの表示内容の生成
var observableCollection = new ObservableCollection<String> ();
// 0,1...99の整数値をリストを生成し、項目名に利用
foreach (var i in Enumerable.Range(0, 100))
{
observableCollection.Add(string.Format("項目-{0}", i));
}
// 生成いた文字列のコレクションをListViewの表示内容として
// ListViewを生成
var listView = new ListView
{
ItemsSource = observableCollection, // 表示内容の割り当て
RowHeight = 64 // 表示の共通の高さの指定
};
// The root page of your application
var content = new ContentPage
{
Title = "HelloListView",
Content = new StackLayout
{
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0),
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "Welcome to Xamarin Forms!"
},
listView
}
}
};
// 選択処理の組み込み
//listView.ItemSelected += (sender, e) =>
//{
// // senderはViewとして扱われるため、SelectedItemプロパティに
// // 参照できるようにListViewとしてキャスティングした上で参照する
// var selectedItem = ((ListView)sender).SelectedItem;
// // アラート表示
// // DisplayAlertを用いてアラート表示するには
// // ContentPageを参照しなければならない
// // content生成後に割り当てる必要あり
// content.DisplayAlert(
// "選択した項目",
// selectedItem.ToString(),
// "OK"
// );
//};
// ItemSelectedのDisplayAlertメソッドを用いていると
// PushModalAsyncが無効に
// メソッド内にawait記述がある場合は、async宣言は必須
listView.ItemTapped += async(sender, e) =>
{
var selectedItem = ((ListView)sender).SelectedItem;
// 画面遷移
// 取得した項目を元に次の画面に遷移
var detailContent = new ContentPage
{
Title = "詳細ページ",
Content = new StackLayout
{
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0),
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = String.Format("選択された項目: {0}", selectedItem)
}
}
}
};
Debug.WriteLine("Step1");
// Task<T>とした場合、その処理は待ち状態とはならない。
var test = await TestTaskAsync();
// この場所は通常スレッドでは実行されない
Debug.WriteLine("Step2");
// 結果を出力
Debug.WriteLine("Result = " + test);
// awaitの記述をしなければ画面が表示されない
await MainPage.Navigation.PushAsync(detailContent);
};
MainPage = new NavigationPage(content);
}
async Task<string> TestTaskAsync()
{
Debug.WriteLine("TestTaskAsync Step1");
// 1秒待つ
await Task.Delay(1000);
Debug.WriteLine("TestTaskAsync Step2");
return "test";
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment