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

Tag: xamarin

  • Xamarin.UWP: Image from embedded resource throws “Operation is not supported on this platform” error

    This content has 3 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 are getting the error “Operation is not supported on this platform” when you are trying to set an ImageResource from file for an image on Xamarin.UWP, then the problem comes from this line of code:

    IconSource = ImageSource.FromResource("afolder.animage.jpg");
    

    The solution: Image as Embadded Ressource shows “operation is not supported on this platform” in UWP and Release (microsoft.com)
    In a short brief: You need to use the second parameter for FromResource method call. You must pass an assembly in order to success on UWP platform. iOS and Android is not affected with this error.

    IconSource = ImageSource.FromResource("afolder.animage.jpg", typeof(AClassFromTheAssembly).Assembly);
    
  • Xamarin.UWP DryIoC error: Code generation not supported on this platform

    This content has 3 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 are getting the following error when using DryIoC with Xamarin.Forms on UWP:

    System.PlatformNotSupportedException: Dynamic code generation is not
    supported on this platform.
    at System.Reflection.Emit. TypeBuilder.GetMethod (Type, Methodinfo) + 0x2d
    at
    Dryloc.FastExpressionCompiler.LightExpression.ExpressionCompiler.CompileNoA
    rgsNew(Constructorlnfo, Type, Type[, Type) + 0x5b
    at
    Dryloc.FastExpressionCompiler.LightExpression.ExpressionCompiler.TryCompileB
    oundToFirstClosureParam(Type, Expression, IParameterProvider, Typel, Type.
    CompilerFlags) + 0x73
    at Dryloc.FactoryDelegateCompiler.CompileToFactoryDelegate(Expression.
    Boolean) + 0x14c
    at Dryloc.Container.Dryloc.IResolver.Resolve(Type, IfUnresolved) + 0x23c
    

    Then instantiate your container with this code:

    Container = new Container(rules =>
    {
        // https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/ResolutionPipeline.md
        return rules.WithUseInterpretation();
    });
    

    The reason why you need to do this is here: DryIoc/ResolutionPipeline.md at master · dadhi/DryIoc · GitHub

  • Xamarin.Forms: Android app forgets the style when using DynamicResource

    This content has 3 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 break the look and feel of the app when you switch apps on mobile, and you use styles as DynamicResources, which you add as MergedDictionaries in App.Xaml,
    you should pay attention to this:

    When you don’t exit the Android app, but bring it back to the foreground after a very long time, the constructor logic in App.xaml.cs runs again. Don’t forget to add MergedDictionary from code, which contains the styles. If you forget this, all controls will appear in the app with their default look.

    I wrote about how to handle restarting the application here: https://www.banditoth.hu/2021/03/22/xamarin-forms-reopening-application-best-pratices/

  • Xamarin.Forms : Focus to the entry and set the cursor after the last character

    This content has 3 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 want to make the cursor in Xamarin.Forms Entry blink behind the text you have already typed, instead of in front of it, after focusing, you need to do the following:

    Subscribe to the Focused event of the Entry and modify the eventhandler as follows:

    		public void OnEntryFocused(object sender, EventArgs args)
    		{
    			EntryInstance.CursorPosition = EntryInstance?.Text?.Length ?? 0;
    		}
    

    You can find more information about the CursorPosition at: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.entry.cursorposition?view=xamarin-forms

  • Xamarin.Android get resource id without ResourceManager.

    This content has 3 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 calling ResourceManager.GetResource(“resourceName”) doesn’t work because the framework doesn’t return the resource you want, you can bypass it with the following code snippet:

                string audioFileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(audiofile);
                int audioResourceId = (int)typeof(Resource.Raw).GetField(audioFileNameWithoutExtension).GetValue(null);
                Android.Net.Uri audioUri = Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + Application.Context.PackageName + "/" + audioResourceId);
    
                channel.SetSound(audioUri, audioAttributes);
    

    I used this code snippet to set a custom push notification sound