András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

  • Xamarin Forms: Infinite scrolling ListView

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    To get a dynamic “paged” loading for your list view, follow the steps below:

    Make a new usercontrol in your forms project. Derive from ContentView instead from ListView. By deriving from listview, you simply can not Count the items binded to the ItemsSource property, which is the base of the Fetching logic.

    public class InfiniteScrollListView : ContentView
    

    Make a countable ItemsSource bindable property for your new usercontrol.

    		public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
    														propertyName: "ItemsSource",
    														returnType: typeof(IList),
    														declaringType: typeof(View),
    														defaultValue: null,
    														defaultBindingMode: BindingMode.TwoWay,
    														propertyChanged: null);
    
    		public IList ItemsSource
    		{
    			get { return (IList)GetValue(ItemsSourceProperty); }
    			set { SetValue(ItemsSourceProperty, value); }
    		}
    

    We need to get the usercontrol to display a listview for us. Make a new instance of a listview. You can specify a caching strategy for the listview, which can be useful on lists have a lot of items. Set the contenview’s content to the new listview instance.

    		public InfiniteScrollListView()
    		{
    			_listViewInstance = new ListView(cachingStrategy: ListViewCachingStrategy.RecycleElement);
    			_listViewInstance.HorizontalOptions = LayoutOptions.Fill;
    			_listViewInstance.VerticalOptions = LayoutOptions.Fill;
    			_listViewInstance.ItemAppearing += InfiniteScrollListView_ItemAppearing;
    			_listViewInstance.SetBinding(ListView.ItemsSourceProperty, new Binding(nameof(ItemsSource), BindingMode.TwoWay, source: this));
    			Content = _listViewInstance;
    		}
    
    		~InfiniteScrollListView()
    		{
    			if (_listViewInstance != null)
    				_listViewInstance.ItemAppearing -= InfiniteScrollListView_ItemAppearing;
    		}
    

    The ItemAppearing event is the most important event in our Infinite scrolling listview instance. It’s called, when an item is displayed “phisically” on the screen. Let’s make an eventhandler for it. We are using a new Bindable Property for fetching data command, which will be called, when the last item of the ItemsSource has been displayed, and an another Bindable property, to turn off the fetching if the Data access layer could not provide more elements for the query. The _lastElementIndex will be the e.ItemIndex.

    if (_lastItemDisplayedIndex == (ItemsSource?.Count - 1 ?? int.MinValue))
    			{
    				if (HasMoreItems)
    				{
    					if (FetchDataCommand?.CanExecute(null) == true)
    					{
    						FetchDataCommand?.Execute(null);
    					}
    				}
    			}
    

  • Xamarin Android: Dismiss lock screen or show activity on lock screen

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    To gain an Activity to be showed on the lock screen, or bypass the phone’s lock screen (aka Keyguard) for important notification handling, for example an Incoming VOIP call, you need to do the following:

    Set the permission for disabling keyguard. Add the following permission to the AndroidManifest.xml:

      <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
    

    Add flags to the Activity’s Window in order to show the activity on the lock screen, or to bypass the lock screen.

    this.Window.AddFlags(WindowManagerFlags.DismissKeyguard | WindowManagerFlags.ShowWhenLocked);
    

    Dismiss the keyguard if needed. (CrossCurrentActivity plugin is used to get the current activity):

    KeyguardManager keyguardManager = (KeyguardManager)CrossCurrentActivity.Current.Activity?.GetSystemService(Context.KeyguardService);
    if (keyguardManager != null)
    {	
    	keyguardManager.RequestDismissKeyguard(CrossCurrentActivity.Current.Activity, null);
    }
    

  • Xamarin: Android bindings library could not build

    This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    It seems, the new Visual Studio 2019 release with version 16.7 could not build the AndroidBindings projects. (For further information about what is an android binding proj, see: https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/)

    The compiler gives the following error message:

    ...\MSBuild\Xamarin\Android\Xamarin.Android.Bindings.targets(327,5): warning MSB6002: The command-line for the "BindingsGenerator" task is too long. Command-lines longer than 32000 characters are likely to fail. Try reducing the length of the command-line by breaking down the call to "BindingsGenerator" into multiple calls with fewer parameters per call.
    

    Workarounds like: Change the path of the soulution to shorter one does not seems to work.
    The only solution for the problem was to downgrade VisualStudio to 16.5.4 (to the last working one for me)

  • Xamarin Android: Native image scaling, images are pixelated

    This content has 5 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    If you use Android’s native image scaling, from ldpi, mdpi, hdpi, to xxxhdpi, and have all the image resource in the right directory of your Xamarin.Android project folder, and find the pictures displaying pixelated: Make sure, you are correctly set the Build action for each images to AndroidResource. When adding an existing item to the resources folder from Windows version of VisualStudio’s browse dialog, the Build action will be set to BundleResource, and won’t appear in the Resources.designer.cs file, so it will be unavailable to use from code. If the picture is displaying, but it is pixelated, you may check the higher DPI version image files, have the correct buid action

  • Xamarin Android: “Program type already present”

    This content has 5 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

    AndroidX migráció után a következő hibaüzenettel nem fordul le az alkalmazás:

    D8 : error : Program type already present: android.support.v4.app.INotificationSideChannel$Stub$Proxy
    

    Próbáltam mindent, kitöröltem a lokális NuGet Cache-t, az összes bin és obj mappát, Cleanelni a Solutiont, Restoreolni a NuGet-eket. Nem járt eredménnyel semmi sem. Aztán nyitottam egy hibát az AndroidX repo-n (https://github.com/xamarin/AndroidX/issues/100) , ahol a megszokotthoz képest lassan reagáltak, akkor sem tudtak érdemben segíteni, így magamnak kellett megoldást találni a problémára.

    Elsőre feltelepítettem egy virtuális gépre friss Windows 10-et, rá pedig egy 2019-es Visual Studio Community-t. Lecloneoztam a repositoryt, megfordítottam, és sikeresen lefordult. Itt már gondoltam, hogy valami specifikus hiba lesz. Pár hétre jegelve lett a projekt, majd végül a megoldása az lett, hogy a host gépen teljesen eltávolítottam fizikailag az Azure DevOps GIT alapú repositoryt, majd újra leszedtem a remote-ról, és működött.