MediaSource
using System;
using System.Runtime.CompilerServices;
using Xamarin.CommunityToolkit.Helpers;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Xamarin.CommunityToolkit.Core
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[TypeConversion(typeof(MediaSourceConverter))]
public abstract class MediaSource
{
private readonly WeakEventManager weakEventManager = new WeakEventManager();
internal event EventHandler SourceChanged {
add {
weakEventManager.AddEventHandler(value, "SourceChanged");
}
remove {
weakEventManager.RemoveEventHandler(value, "SourceChanged");
}
}
public static MediaSource FromFile([System.Runtime.CompilerServices.Nullable(2)] string file)
{
return new FileMediaSource {
File = file
};
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static MediaSource FromUri(Uri uri)
{
if (uri == (Uri)null)
return null;
if (uri.IsAbsoluteUri)
return new UriMediaSource {
Uri = uri
};
throw new ArgumentException("Uri must be be absolute", "uri");
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public static MediaSource FromUri(string uri)
{
return FromUri(new Uri(uri));
}
public static implicit operator MediaSource(string source)
{
if (!Uri.TryCreate(source, UriKind.Absolute, out Uri result) || !(result.Scheme != "file"))
return FromFile(source);
return FromUri(result);
}
public static implicit operator MediaSource(Uri uri)
{
return FromUri(uri);
}
protected void OnSourceChanged()
{
weakEventManager.RaiseEvent(this, EventArgs.Empty, "SourceChanged");
}
protected MediaSource()
: this()
{
}
}
}