프로그래밍

자마린 ListView를 통한 버튼 동적생성 질문!

WPF에서 ItemsControl을 이용해 ObservableCollection<string>으로 버튼을 동적생성하게 했는데

자마린에는 ItemsControl이 없었다...

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.Views.AboutPage"
             xmlns:vm="clr-namespace:App1.ViewModels"
             Title="{Binding Title}">
    
    <ContentPage.BindingContext>
        <vm:AboutViewModel />
    </ContentPage.BindingContext>
    
    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:Key="Accent">#96d1ff</Color>
        </ResourceDictionary>
    </ContentPage.Resources>
 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackLayout BackgroundColor="{StaticResource Accent}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill">
            <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center">
                <ContentView Padding="0,40,0,40" VerticalOptions="FillAndExpand">
                    <Image Source="xamarin_logo.png" VerticalOptions="Center" HeightRequest="64" />
                </ContentView>
            </StackLayout>
        </StackLayout>
        <ScrollView Grid.Row="1">
            <StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10">
                <ListView x:Name="MyList" ItemsSource="{Binding Path=this.ButtonCollection}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout Orientation="Vertical">
                                <Button Margin="0,10,0,0" Text="{Binding Path=., Mode=TwoWay, UpdateSourceEventName=PropertyChanged}"
                                Command="{Binding Path=BindingConetxt.SubButtonCommand, Source={x:Reference Name=MyList}}"
                                BackgroundColor="{StaticResource Primary}"
                                TextColor="White">
                                </Button>
                            </StackLayout>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                
            </StackLayout>
        </ScrollView>
    </Grid>
 
</ContentPage>
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
 
namespace App1.ViewModels
{
    public class AboutViewModel : BaseViewModel
    {
        public ObservableCollection<string> ButtonCollection { get; set; }
 
        public AboutViewModel()
        {
            Title = "About";
            OpenWebCommand = new Command(async () => await Browser.OpenAsync("https://aka.ms/xamarin-quickstart"));
 
            ButtonCollection = new ObservableCollection<string>();
 
            Init();
        }
 
        public void Init()
        {
            ButtonCollection.Add("1번");
            ButtonCollection.Add("2번");
            ButtonCollection.Add("3번");
            ButtonCollection.Add("4번");
        }
 
        public ICommand OpenWebCommand { get; }
    }
}
cs

 

그래서 ListView를 이용해 위와 같이 각각 View부분과 ViewModel부분을 구현했는데

내가 원했던 모습은

캡처.PNG

이것처럼 ButtonCollection의 있는 String들을 Text로 갖는 버튼들이 생기게 하려고 했지만 버튼들이 전혀 생성 되질 않는다.

 

WPF하고 비슷하면서도 다른게 아주 엿같네... 이유가 뭐지?

10개의 댓글

2023.07.19
0
ye
2023.07.20

CollectionView? ListView? 이중에 쓰면 되는거같은데

0
2023.07.20

콜렉션에는 데이터가 들어갔지만 xamarin ui thread에서는 버튼이 추가된 걸 모르고 있는 듯...

propertychanged나 collectionchanged, notify 뭐 이런 Interface 찾아보셈

0
2023.07.20

버튼콜렉션에 하나하나 넣지말고 따로 변수 만들어서 다 넣은다음에 콜렉션에 뭉태기로 넣어조보삼

0
2023.07.20

아니 애초에 string타입만 받게 해놨는데... 어케 버튼이 생성됨???

예를 들어

ButtonCollection = new ObservableCollection<Button>();

ButtonCollection.add(new Button(arg1, arg2, arg3,....));

 

이래야 되는거 아닌교?

0
2023.07.20
@동천마을

ㄴㄴ 일단 ListView -> CollectionView로 바꾸고 성공함.

ObsevableCollection<Tuple(string, string)>으로 변경하고 itme2는 버튼의 text랑 바인딩,

Item1은 commandparameter와 바인딩히 했는데 command 바인딩을 아직 못 했음...

0
2023.07.20
@동천마을

위 소스 기준 listview의 이해는

'리스트뷰는 buttoncollection이랑 연동되어 있어! 그러니 목록의 수는 buttoncollection의 수 만큼이야!

그리고 그 안에는 버튼이 하나 있는데 텍스트는... 커맨드는...'

이런식으로 만들어지는거라 ObsevableCollection에 string만 넣어도 생성되는 듯 ㅇㅇ

0
2023.07.21
@집에가게해줘

워 자마린 컴파일러 성능 미쳤네.

존나 무거울 것 같은데.

0
ye
2023.07.24
@동천마을

XAML로 작성하고 컴파일러가 알아서 cs 코드로 변경함 ㅋㅋ WPF도 동일

0
2023.07.20

xml에 binding도 버튼 이자나여...

0
무분별한 사용은 차단될 수 있습니다.
번호 제목 글쓴이 추천 수 날짜 조회 수
5709 [프로그래밍] 패스트 캠퍼스 <---- 얘내는 가격 인상 원툴임? 5 조강현 0 1 일 전 256
5708 [프로그래밍] 클라가 파이썬 셀레니움같은거 써서 클릭하고 그러는걸 감지 ... 5 리옴므 0 3 일 전 192
5707 [프로그래밍] leetcode 50일 달성 1 JimmyMcGill 1 3 일 전 165
5706 [프로그래밍] 그냥 개인공부용 git 만들건데 5 년째재수강 0 3 일 전 253
5705 [프로그래밍] html 자바스크립트 질문 19 책걸이 0 3 일 전 298
5704 [프로그래밍] 아니 시바 이게 무슨일이야 4 인간지표 0 4 일 전 314
5703 [프로그래밍] 아두이노 키트 아무것도 모르고 사도 될까? 6 그것 0 4 일 전 258
5702 [프로그래밍] 횽들 Vimeo에 올라가있는 동영상의 원본크기를 확인할 수 있... 13 카뜨만두 0 4 일 전 185
5701 [프로그래밍] c# 이벤트와 델리게이트 13 RX7900XTX 0 7 일 전 304
5700 [프로그래밍] Aws 람다에 파이썬 올려서 결과 받아오는데 11 아르피쥐 0 9 일 전 343
5699 [프로그래밍] 마리아DB mediumtext 그냥 쓰고 싶은데 21 잉텔 0 10 일 전 220
5698 [프로그래밍] 안드로이드 씹뉴비 질문이요 2 집에가게해줘 0 10 일 전 125
5697 [프로그래밍] c언어 7년했는데 이런게 되는거 처음알았음.. 4 케로로중사 0 11 일 전 891
5696 [프로그래밍] 파이썬 1도 모르는데 GPT로 프로그램 뚝딱 만듬 2 푸르딩딩 1 14 일 전 743
5695 [프로그래밍] 담주 면접잡혔는데 8 삐라루꾸 0 14 일 전 503
5694 [프로그래밍] 아두이노 부트로더를 구웠는데.. 4 렙이말한다ㅡ니가옳다 0 15 일 전 233
5693 [프로그래밍] IOS 개발자 있나여? 1 g4eng 0 17 일 전 260
5692 [프로그래밍] 시스템 디자인 인터뷰 준비 도움좀!!! 1 Nognhyup 0 18 일 전 202
5691 [프로그래밍] 최근에 vscode 쓴 사람 도움! 3 172102 0 19 일 전 524
5690 [프로그래밍] 책을 또 사버리고 말았다... 1 찰나생멸 2 19 일 전 524