Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Multiple Handlers to a single object type

$
0
0

Can I register multiple handlers to a single type, via HandlerAttribute, and loop through the registered types?

or

Can I use a string to register / retrieve a DependencyService, instead of a type?

In my case I have an object (that is always the same type), but I want to have multiple classes that can handle this object, and I want to select the correct handler based on a string contained in the original object to be handled.

Do I need to scan the CurrentDomain assemblies myself?

To be more specific, I want to register an ImageSourceHandler, that can handle UriImageSource objects, when the Uri Scheme is "content",
allowing more uri schemes to be registered.

Something like:


[assembly: ExportUriImageSourceProvider(
    scheme: "content",
    typeof(Sample.ContentImageSourceProvider))]

[assembly: ExportImageSourceHandler(
    typeof(Xamarin.Forms.ContentImageSource),
    typeof(Sample.ContentImageSourceHandler))]

namespace Sample
{
    using Microsoft.Xna.Framework.Graphics;
    using System.Threading;
    using System.Threading.Tasks;
    using Xamarin.Forms;

    public sealed class ContentImageSource : ImageSource
    {
        public readonly string Asset;
        public ContentImageSource(string asset)
        {
            Asset = asset;
        }
    }

    public sealed class ContentImageSourceProvider : IImageSourceProvider, IRegisterable
    {
        public ImageSource ConvertToImageSource(string imageSource)
        {
            return new ContentImageSource(imageSource);
        }
    }

    public sealed class ContentImageSourceHandler : IImageSourceHandler, IRegisterable
    {
        public Task LoadImageAsync(ImageSource imageSource, CancellationToken cancellationToken)
        {
            return Task.Factory.StartNew(delegate
            {
                var fileSource = (ContentImageSource)imageSource;
                return Forms.Game.Content.Load(fileSource.Asset);
            });
        }
    }
}

(I think this could also be used in the string to ImageSource implicit conversion)


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>