첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.15MB

▶ MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:TestProject">
    <ContentPage.Resources>
        <local:ImageSourceConverter x:Key="ImageSourceConverterKey" />
    </ContentPage.Resources>
    <Grid
        Margin="10"
        RowDefinitions="*,10,Auto">
        <CollectionView x:Name="collectionView" Grid.Row="0"
            SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid
                        Padding="10"
                        RowDefinitions="Auto"
                        ColumnDefinitions="Auto,10,*">
                        <Image Grid.Column="0"
                            WidthRequest="60"
                            HeightRequest="60"
                            Aspect="AspectFill"
                            Source="{Binding ImageURL, Converter={StaticResource ImageSourceConverterKey}}" />
                        <StackLayout Grid.Column="2">
                            <Label
                                FontAttributes="Bold"
                                FontSize="16"
                                Text="{Binding Name}" />
                            <Label
                                VerticalOptions="End"
                                FontAttributes="Italic"
                                Text="{Binding Location}" />
                        </StackLayout>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
        <Label x:Name="label" Grid.Row="2"
            HorizontalOptions="StartAndExpand"
            Text="선택 항목 : (없음)" />
    </Grid>
</ContentPage>

 

728x90

 

▶ MainPage.xaml.cs

namespace TestProject;

/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainPage()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainPage()
    {
        InitializeComponent();

        List<Monkey> list = new List<Monkey>();

        for(int i = 0; i < 10; i++)
        {
            list.Add
            (
                new Monkey
                {
                    Name     = "Baboon",
                    Location = "Africa and Asia",
                    ImageURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg"
                }
            );

            list.Add
            (
                new Monkey
                {
                    Name     = "Capuchin Monkey",
                    Location = "Central and South America",
                    ImageURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg"
                }
            );

            list.Add
            (
                new Monkey
                {
                    Name     = "Blue Monkey",
                    Location = "Central and East Africa",
                    ImageURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/BlueMonkey.jpg/220px-BlueMonkey.jpg"
                }
            );
        }

        this.collectionView.ItemsSource = list;

        this.collectionView.SelectionChanged += collectionView_SelectionChanged;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 컬렉션 뷰 선택 변경시 처리하기 - collectionView_SelectionChanged(sender, e)

    /// <summary>
    /// 컬렉션 뷰 선택 변경시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void collectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string previousName = (e.PreviousSelection.FirstOrDefault() as Monkey)?.Name;
        string currentName  = (e.CurrentSelection.FirstOrDefault()  as Monkey)?.Name;

        this.label.Text = $"이전 원숭이명 : {previousName}\n현재 원숭이명 : {currentName}";
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요