Migrate Android app using Xamarin.Android.Support.v4 to .NET6

Tomáš Němec 1 Reputation point
2022-08-09T06:19:25.663+00:00

Hi,
I have a Xamarin.Android game, which I am trying to port to .NET6
I have a dependency on Xamarin.Android.Support.v4 - which provides functionality described below:

Android.Support.V4.View:

  • IOnApplyWindowInsetsListener
  • ViewCompat.SetOnApplyWindowInsetsListener

Android.Support.V4.Content:

  • ContextCompat.CheckSelfPermission
  • FileProvider.GetUriForFile

I want to continue targeting API 32 with minimal version set to API 19.
What do I need to reference instead? Many thanks for any useful tips!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 44,011 Reputation points Microsoft Vendor
    2022-08-10T06:25:24.123+00:00

    Hello,

    For using those APIs you mentioned, you could refer to the following documentations:

    AndroidX.Core.View

    AndroidX.Core.Content:

    And here are the samples of using the APIs in Xamarin.Forms on Android platform:

    Implement IOnApplyWindowInsetsListener:

       public class RootViewDeferringInsetsCallback : WindowInsetsAnimationCompat.Callback, IOnApplyWindowInsetsListener  
       {  
           public RootViewDeferringInsetsCallback(int dispatchMode) : base(dispatchMode)  
           {  
           }  
           public WindowInsetsCompat OnApplyWindowInsets(View v, WindowInsetsCompat insets)  
           {  
           }  
           public override WindowInsetsCompat OnProgress(WindowInsetsCompat insets, IList<WindowInsetsAnimationCompat> runningAnimations)  
           {  
           }  
       }  
    

    ViewCompat.SetOnApplyWindowInsetsListener:

       RootViewDeferringInsetsCallback callback = new RootViewDeferringInsetsCallback(WindowInsetsAnimationCompat.Callback.DispatchModeStop);  
       ViewCompat.SetOnApplyWindowInsetsListener(your_view, callback);  
    

    ContextCompat.CheckSelfPermission:

       var selfPermission = ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera);  
       if (selfPermission == Permission.Granted)  
       {  
         
       }  
       else  
       {  
         
       }  
    

    FileProvider.GetUriForFile:

    At first, please refer to FileProvider to configure your FileProvider.
    Then, in you Xamarin project, you could use the following code to call FileProvider.GetUriForFile:

       File imagePath = new File(FilesDir, "my_images");  
       File newFile = new File(imagePath, "default_image.jpg");  
       Android.Net.Uri contentUri = FileProvider.GetUriForFile(this, "com.mydomain.fileprovider", newFile);  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.