-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.txt
123 lines (107 loc) · 4.72 KB
/
post.txt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<p>Tabs in dotnet maui have an 'OnAppearing' event which you would expect to be called each time a tab is activated. This however is <a href="https://github.com/dotnet/maui/issues/9531" target="_blank" rel="noopener">not the case</a> and a possible workaround is to initialize data in a static manner and pass arguments using the <a href="https://learn.microsoft.com/en-us/dotnet/maui/xaml/pass-arguments?view=net-maui-6.0" target="_blank" rel="noopener">'Arguments'</a>-construct like shown in this post :</p>
<p><b>Tab1:</b></p>
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp1.Pages.Tab1" xmlns:local="clr-namespace:MauiApp1.Data" Title="Tab1">
<ContentPage.BindingContext>
<local:ItemViewModel>
<x:Arguments>
<x:Int32>10</x:Int32>
</x:Arguments>
</local:ItemViewModel>
</ContentPage.BindingContext>
<VerticalStackLayout>
<Label Text="Integers as argument"/>
<CollectionView x:Name="collectionView" Margin="5" ItemTemplate="{StaticResource DataTemplate1}" ItemsSource="{Binding ItemCollection}" SelectionMode="Single" />
</VerticalStackLayout>
</ContentPage>
[/sourcecode]
<p><b>Tab2:</b></p>
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp1.Pages.Tab2" xmlns:local="clr-namespace:MauiApp1.Data" Title="Tab2" >
<ContentPage.BindingContext>
<local:ItemViewModel>
<x:Arguments>
<x:Int32>20</x:Int32>
</x:Arguments>
</local:ItemViewModel>
</ContentPage.BindingContext>
<VerticalStackLayout>
<Label Text="Integers as argument"/>
<CollectionView x:Name="collectionView" Margin="5" ItemTemplate="{StaticResource DataTemplate1}" ItemsSource="{Binding ItemCollection}" SelectionMode="Single" />
</VerticalStackLayout>
</ContentPage>
[/sourcecode]
<p><b>Tab3:</b></p>
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiApp1.Pages.Tab3" xmlns:local="clr-namespace:MauiApp1.Data" Title="Array of integers" >
<ContentPage.BindingContext>
<local:ItemViewModel>
<x:Arguments>
<x:Array Type="{x:Type x:Int32}">
<x:Int32>10</x:Int32>
<x:Int32>11</x:Int32>
<x:Int32>12</x:Int32>
<x:Int32>13</x:Int32>
<x:Int32>14</x:Int32>
<x:Int32>15</x:Int32>
</x:Array>
</x:Arguments>
</local:ItemViewModel>
</ContentPage.BindingContext>
<VerticalStackLayout>
<Label Text="Array of integers as arguments"/>
<CollectionView x:Name="collectionView" Margin="5" ItemTemplate="{StaticResource DataTemplate1}" ItemsSource="{Binding ItemCollection}" SelectionMode="Single" />
</VerticalStackLayout>
</ContentPage>
[/sourcecode]
<p>Where the Itemcollection looks like this:</p>
[sourcecode language="csharp"]
public class ItemViewModel : INotifyPropertyChanged
{
public ItemViewModel( ) {}
public ItemViewModel( int categoryId)
{
this.ItemCollection = new ObservableCollection<Item>( );
for( int i = 0; i ‹ 4; i++)
{
this.ItemCollection.Add( new Item( ) { Name = $"V1-{categoryId}", Description = $"c{categoryId}/T1 rocks"});
}
}
public ItemViewModel( int[] categoryIds)
{
this.ItemCollection = new ObservableCollection<Item>( );
for( int i = 0; i ‹ categoryIds.Length; i++ )
{
int categoryId = categoryIds[i];
this.ItemCollection.Add( new Item( ) { Name = $"V1-{categoryId}", Description = $"c{categoryId}/T1 rocks"});
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
ArgumentNullException.ThrowIfNull(e);
PropertyChanged?.Invoke(this, e);
}
public ObservableCollection<Item> ItemCollection { get; set; }
}
[/sourcecode]
<p>This will result in a nice flexible way of initialisation for tabs:</p>
<table>
<tbody>
<tr>
<td>
<figure class="wp-block-image size-full"><img class="wp-image-13844" src="http://gtechcorner.com/wp-content/uploads/2023/01/t1.jpg" alt="Integer argument" /></figure>
</td>
<td>
<figure class="wp-block-image size-full"><img class="wp-image-13845" src="http://gtechcorner.com/wp-content/uploads/2023/01/t2.jpg" alt="Integer argument" /></figure>
</td>
<td>
<figure class="wp-block-image size-full"><img class="wp-image-13845" src="http://gtechcorner.com/wp-content/uploads/2023/01/t3.jpg" alt="Array of integers as argument" /></figure>
</td>
</tr>
</tbody>
</table>
<p>An example project can be loaded from <a href="https://github.com/stephandeckers/maui_tab_argument.git" target="_blank" rel="noopener">here</a>.</p>