1
1

More than 3 years have passed since last update.

眺めて覚える C# Xamarin Forms(11) ListView Programmatically

Posted at

スマートフォンで一番使うリストビューを解説します。

前回に続いてC#バージョンです。Programmatically

空のプロジェクトを作成します。

image.png

モバイルアプリを選択します。

image.png

プロジェクト名を指定して作成

image.png

空白を選択してOK

image.png

MainPage.xaml.csを置き換えます。

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Xamarin_10
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            var list = new List<string> { "東京", "神田", "お茶の水", "四谷", "新宿", "中野", "荻窪" };
            var lv = new ListView() { ItemsSource = list };
            lv.ItemTapped += Lv_ItemTapped;
            Content = new Xamarin.Forms.ScrollView() { Orientation = ScrollOrientation.Vertical, Content = lv };
        }

        private void Lv_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            DisplayAlert("Tapped",$"{e.Item}", "OK");
        }
    }
}

実行結果
image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1