LoginSignup
1

More than 3 years have passed since last update.

xamarin TabbedPageでタブ移動時にListViewの更新

Posted at

TabbedPageにて、複数のListViewを用意し、ステータス(既読/読書中)によってリストを切り替えるインターフェースを作成した時に、タブを選択したタイミングでリストの表示内容を更新したい時がある。
そんな時は、該当タブのContentPageのAppearingイベントを利用することで実現可能だ。

sample.xaml
<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:controls="clr-namespace:sample;assembly=books2" 
            x:Class="sample">

    <TabbedPage.Children>
        <ContentPage Title="既読" Padding="30" Appearing="Handle_Appearing0">
        <ListView x:Name="_listView0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1" />    
                            <RowDefinition Height="20" />
                            <RowDefinition Height="40" />
                            <RowDefinition Height="1" /> 
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1" />
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="1" />
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding ImageL}"
                               Grid.Row="1" Grid.Column="1" Grid.RowSpan="2"
                               VerticalOptions="Start"
                               Aspect="AspectFit"
                               />
                        <Label Grid.Row="1" Grid.Column="2"
                               Text="{Binding Title}" 
                               FontAttributes="Bold"                                
                               />
                        <Label Grid.Row="2" Grid.Column="2"
                               Text="{Binding Author}"
                               FontSize="12"
                               />
                    </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </ContentPage>    
        <ContentPage Title="読書中" Padding="30" Appearing="Handle_Appearing1">
        <ListView x:Name="_listView1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1" />    
                            <RowDefinition Height="20" />
                            <RowDefinition Height="40" />
                            <RowDefinition Height="1" /> 
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1" />
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="1" />
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding ImageL}"
                               Grid.Row="1" Grid.Column="1" Grid.RowSpan="2"
                               VerticalOptions="Start"
                               Aspect="AspectFit"
                               />
                        <Label Grid.Row="1" Grid.Column="2"
                               Text="{Binding Title}" 
                               FontAttributes="Bold"                                
                               />
                        <Label Grid.Row="2" Grid.Column="2"
                               Text="{Binding Author}"
                               FontSize="12"
                               />
                    </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </ContentPage>    
    </TabbedPage.Children>
</TabbedPage>

sample.xaml.cs
        void Handle_Appearing0(object sender, System.EventArgs e)
        {
            // リストビューのデータを更新
            _listView0.ItemsSource =
                wSqliteControl.GetItems(0);
        }

引っ張り更新は実現できていたものの、Androidだとリストにデータが表示されていないと引っ張れないやんけ!!
となって悶々としてましたが、ようやく解決できました。

同じような悩みを抱えている方の助けに少しでもなれば幸いです。

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