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부분을 구현했는데
내가 원했던 모습은
이것처럼 ButtonCollection의 있는 String들을 Text로 갖는 버튼들이 생기게 하려고 했지만 버튼들이 전혀 생성 되질 않는다.
WPF하고 비슷하면서도 다른게 아주 엿같네... 이유가 뭐지?
10개의 댓글
무분별한 사용은 차단될 수 있습니다.
남궁동생
ye
CollectionView? ListView? 이중에 쓰면 되는거같은데
동천마을
콜렉션에는 데이터가 들어갔지만 xamarin ui thread에서는 버튼이 추가된 걸 모르고 있는 듯...
propertychanged나 collectionchanged, notify 뭐 이런 Interface 찾아보셈
A700
버튼콜렉션에 하나하나 넣지말고 따로 변수 만들어서 다 넣은다음에 콜렉션에 뭉태기로 넣어조보삼
동천마을
아니 애초에 string타입만 받게 해놨는데... 어케 버튼이 생성됨???
예를 들어
ButtonCollection = new ObservableCollection<Button>();
ButtonCollection.add(new Button(arg1, arg2, arg3,....));
이래야 되는거 아닌교?
집에가게해줘
ㄴㄴ 일단 ListView -> CollectionView로 바꾸고 성공함.
ObsevableCollection<Tuple(string, string)>으로 변경하고 itme2는 버튼의 text랑 바인딩,
Item1은 commandparameter와 바인딩히 했는데 command 바인딩을 아직 못 했음...
집에가게해줘
위 소스 기준 listview의 이해는
'리스트뷰는 buttoncollection이랑 연동되어 있어! 그러니 목록의 수는 buttoncollection의 수 만큼이야!
그리고 그 안에는 버튼이 하나 있는데 텍스트는... 커맨드는...'
이런식으로 만들어지는거라 ObsevableCollection에 string만 넣어도 생성되는 듯 ㅇㅇ
동천마을
워 자마린 컴파일러 성능 미쳤네.
존나 무거울 것 같은데.
ye
XAML로 작성하고 컴파일러가 알아서 cs 코드로 변경함 ㅋㅋ WPF도 동일
동천마을
xml에 binding도 버튼 이자나여...