Forum Cross Platform with Xamarin

Xamarin Forms Load Platform Specific Files

Hey guys so I'm working on a Xamarin cross platform project that uses .net shared code, and then has .iOS . Android and .UWP projects as well.

What I want to do is be able to is put some files into either the resources directory or root, of the platform specific files, so I can access them from the shared code project.

Specifically I have a class in the shared project that takes an input stream reading of a .svg file, and then draws it as an svg. That works fine if the svg is in the shared project, but I want to be able to have different icons for different platforms, so just as you would do with Images, I would like to be able to have different versions of the in the platform specific projects.

The issue, is that I have no way of accessing the files from the Shared project. I tried doing the following stackoverflow.com/a/29233573 (sorry too new to post links) however when I try that I'm not able to get the assembly that contains the embedded resources of the platform specific projects. If there was some way to reference those assemblies that may solve my issue, otherwise I need a completely different approach than the ManifestResources to be able to read these files I guess.

Any help would be appreciated, I've been banging my head against this all day and am really pretty drained at this point.

Tagged:

Best Answer

Answers

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Instead of using a 3rd party package with its own dependencies, I'd suggest using dependency injection.

    Create an Interface for the file operations you want, and implement it on each platform to return the platform specific resource.
  • lcm98lcm98 Member ✭✭
    edited May 2018

    @JamesLavery said:
    Instead of using a 3rd party package with its own dependencies, I'd suggest using dependency injection.

    Create an Interface for the file operations you want, and implement it on each platform to return the platform specific resource.

    Is it a 3rd party package though? Xamarin.Essentials seems to be made by Xamarin themselves and just seems to be a new set of tools they're making to work alongside everything else.

    While I agree that I don't usually like installing entire packages, it seems like they're making it with the intent to handle a whole bunch of stuff that you would otherwise need dependency injection for.

  • SolomonFriedSolomonFried USMember ✭✭

    @lcm98 said:

    @JamesLavery said:
    Instead of using a 3rd party package with its own dependencies, I'd suggest using dependency injection.

    Create an Interface for the file operations you want, and implement it on each platform to return the platform specific resource.

    Is it a 3rd party package though? Xamarin.Essentials seems to be made by Xamarin themselves and just seems to be a new set of tools they're making to work alongside everything else.

    While I agree that I don't usually like installing entire packages, it seems like they're making it with the intent to handle a whole bunch of stuff that you would otherwise need dependency injection for.

    You are correct. The Xamarin.Essentials is currently documented on the Xamarin main document page.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Yes it's an official xamarin nuget, but as you say yourself, it handles a 'whole bunch of stuff' and therefore has a 'whole bunch of code' which you don't need.

    It's also pre release - but that doesn't mean it's unstable.

    Personally I'd write my own interface and implementations. This keeps your code small and tight.
  • lcm98lcm98 Member ✭✭

    @JamesLavery said:
    Yes it's an official xamarin nuget, but as you say yourself, it handles a 'whole bunch of stuff' and therefore has a 'whole bunch of code' which you don't need.

    It's also pre release - but that doesn't mean it's unstable.

    Personally I'd write my own interface and implementations. This keeps your code small and tight.

    I'll keep that in mind, figure out how much of the package has features I actually want to use, and then make a decision on package vs just implementing what I need. tthanks for the input!

Sign In or Register to comment.