1+ using System . Collections . ObjectModel ;
2+ using System . ComponentModel ;
3+ using System . Runtime . CompilerServices ;
4+
5+ namespace Maui . Controls . Sample . Issues ;
6+
7+ [ Issue ( IssueTracker . Github , 28051 , "ObjectDisposedException Occurs During View Recycling When PlatformBehavior Is Attached on Android" , PlatformAffected . Android ) ]
8+ public class Issue28051 : TestContentPage , INotifyPropertyChanged
9+ {
10+ ObservableCollection < string > items ;
11+
12+ public ObservableCollection < string > Items
13+ {
14+ get => items ;
15+ set
16+ {
17+ if ( items != value )
18+ {
19+ items = value ;
20+ OnPropertyChanged ( ) ;
21+ }
22+ }
23+ }
24+
25+ protected override void Init ( )
26+ {
27+ BindingContext = this ;
28+ LoadItems ( ) ;
29+
30+ Button refreshItemsButton = new Button
31+ {
32+ AutomationId = "RefreshItemsButton" ,
33+ Text = "Refresh"
34+ } ;
35+ refreshItemsButton . Clicked += OnReproClicked ;
36+
37+ StackLayout contentLayout = new StackLayout ( ) { Spacing = 10 } ;
38+
39+ StackLayout itemContainer = new StackLayout ( ) ;
40+ itemContainer . SetBinding ( BindableLayout . ItemsSourceProperty , new Binding ( "Items" ) ) ;
41+
42+ BindableLayout . SetItemTemplate ( itemContainer , CreateItemTemplate ( ) ) ;
43+
44+ RefreshView refreshView = new RefreshView
45+ {
46+ Content = itemContainer
47+ } ;
48+
49+ contentLayout . Children . Add ( refreshItemsButton ) ;
50+ contentLayout . Children . Add ( refreshView ) ;
51+
52+ this . Content = contentLayout ;
53+ }
54+
55+ DataTemplate CreateItemTemplate ( )
56+ {
57+ DataTemplate itemTemplate = new DataTemplate ( ( ) =>
58+ {
59+ Button button = new Button
60+ {
61+ BackgroundColor = Colors . DarkGreen ,
62+ TextColor = Colors . White
63+ } ;
64+ button . SetBinding ( Button . TextProperty , new Binding ( "." ) ) ;
65+ LongPressBehavior longPressBehavior = new LongPressBehavior ( ) ;
66+ button . Behaviors . Add ( longPressBehavior ) ;
67+ return button ;
68+ } ) ;
69+
70+ return itemTemplate ;
71+ }
72+
73+ void OnReproClicked ( object sender , EventArgs e )
74+ {
75+ Items = null ;
76+ Items = new ObservableCollection < string >
77+ {
78+ "Item A" ,
79+ "Item B" ,
80+ "Item C"
81+ } ;
82+ }
83+
84+ void LoadItems ( )
85+ {
86+ Items = new ObservableCollection < string >
87+ {
88+ "Item 1" ,
89+ "Item 2" ,
90+ "Item 3"
91+ } ;
92+ }
93+
94+ public new event PropertyChangedEventHandler PropertyChanged ;
95+ protected new void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
96+ => PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
97+ }
98+
99+ #if ANDROID
100+ public class LongPressBehavior : PlatformBehavior < Element , Android . Views . View >
101+ {
102+ protected override void OnAttachedTo ( Element bindable , Android . Views . View platformView )
103+ {
104+ BindingContext = bindable . BindingContext ;
105+ }
106+
107+ protected override void OnDetachedFrom ( Element bindable , Android . Views . View platformView )
108+ {
109+ BindingContext = null ;
110+ }
111+ }
112+ #elif IOS || MACCATALYST
113+ public class LongPressBehavior : PlatformBehavior < Element , UIKit . UIView >
114+ {
115+ protected override void OnAttachedTo ( Element bindable , UIKit . UIView platformView )
116+ {
117+ BindingContext = bindable . BindingContext ;
118+ }
119+
120+ protected override void OnDetachedFrom ( Element bindable , UIKit . UIView platformView )
121+ {
122+ BindingContext = null ;
123+
124+ }
125+ }
126+ #elif WINDOWS
127+ public class LongPressBehavior : PlatformBehavior < Element , Microsoft . UI . Xaml . FrameworkElement >
128+ {
129+ protected override void OnAttachedTo ( Element bindable , Microsoft . UI . Xaml . FrameworkElement platformView )
130+ {
131+ BindingContext = bindable . BindingContext ;
132+ }
133+
134+ protected override void OnDetachedFrom ( Element bindable , Microsoft . UI . Xaml . FrameworkElement platformView )
135+ {
136+ BindingContext = null ;
137+ }
138+ }
139+ #else
140+ public class LongPressBehavior : PlatformBehavior < Element , object >
141+ {
142+ protected override void OnAttachedTo ( Element bindable , object platformView )
143+ {
144+ BindingContext = bindable . BindingContext ;
145+ }
146+
147+ protected override void OnDetachedFrom ( Element bindable , object platformView )
148+ {
149+ BindingContext = null ;
150+ }
151+ }
152+ #endif
0 commit comments