public interface IDataStore<T>
{
Task<bool> AddItemAsync(T item);
Task<bool> UpdateItemAsync(T item);
Task<bool> DeleteItemAsync(string id);
Task<T> GetItemAsync(string id);
Task<IEnumerable<T>> GetItemsAsync(bool forceRefresh = false);
}
public class Item
{
public string Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
}
DependencyService.Register<MockDataStore>();
public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>();
public class MockDataStore : IDataStore<Item>
{
readonly List<Item> items;
public MockDataStore()
{
items = new List<Item>()
{
new Item { Id = Guid.NewGuid().ToString(), Text = "First item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Second item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Third item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Fourth item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Fifth item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Sixth item", Description="This is an item description." }
};
}
public async Task<bool> AddItemAsync(Item item)
{
items.Add(item);
return await Task.FromResult(true);
}
public async Task<bool> UpdateItemAsync(Item item)
{
var oldItem = items.Where((Item arg) => arg.Id == item.Id).FirstOrDefault();
items.Remove(oldItem);
items.Add(item);
return await Task.FromResult(true);
}
public async Task<bool> DeleteItemAsync(string id)
{
var oldItem = items.Where((Item arg) => arg.Id == id).FirstOrDefault();
items.Remove(oldItem);
return await Task.FromResult(true);
}
public async Task<Item> GetItemAsync(string id)
{
return await Task.FromResult(items.FirstOrDefault(s => s.Id == id));
}
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
{
return await Task.FromResult(items);
}
}
public static void Register<T>() where T : class
{
Type typeFromHandle = typeof(T);
if (!DependencyTypes.Contains(typeFromHandle))
{
DependencyTypes.Add(typeFromHandle);
}
}
private static readonly List<Type> DependencyTypes = new List<Type>();
public abstract class RouteFactory
{
public abstract Element GetOrCreate();
}
private class TypeRouteFactory : RouteFactory
{
private readonly Type _type;
public TypeRouteFactory(Type type)
{
_type = type;
}
public override Element GetOrCreate()
{
return (Element)Activator.CreateInstance(_type);
}
public override bool Equals(object obj)
{
TypeRouteFactory typeRouteFactory = obj as TypeRouteFactory;
if (typeRouteFactory != null)
{
return typeRouteFactory._type == _type;
}
return false;
}
public override int GetHashCode()
{
return _type.GetHashCode();
}
}
private static Dictionary<string, RouteFactory> s_routes = new Dictionary<string, RouteFactory>();
public static void RegisterRoute(string route, Type type)
{
RegisterRoute(route, new TypeRouteFactory(type));
}
public static void RegisterRoute(string route, RouteFactory factory)
{
if (!string.IsNullOrWhiteSpace(route))
{
route = FormatRoute(route);
}
ValidateRoute(route, factory);
s_routes[route] = factory;
}
public static void UnRegisterRoute(string route)
{
if (s_routes.TryGetValue(route, out RouteFactory _))
{
s_routes.Remove(route);
}
}
internal static string[] GetRouteKeys()
{
string[] array = new string[s_routes.Count + s_implicitPageRoutes.Count];
s_routes.Keys.CopyTo(array, 0);
s_implicitPageRoutes.Keys.CopyTo(array, s_routes.Count);
return array;
}
public static Element GetOrCreateContent(string route)
{
Element element = null;
if (s_implicitPageRoutes.TryGetValue(route, out Page value))
{
return value;
}
if (s_routes.TryGetValue(route, out RouteFactory value2))
{
element = value2.GetOrCreate();
}
if (element == null)
{
Type type = Type.GetType(route);
if (type != null)
{
element = (Activator.CreateInstance(type) as Element);
}
}
if (element != null)
{
SetRoute(element, route);
}
return element;
}
private static void ValidateRoute(string route, RouteFactory routeFactory)
{
if (string.IsNullOrWhiteSpace(route))
{
throw new ArgumentNullException("route", "Route cannot be an empty string");
}
routeFactory = (routeFactory ?? throw new ArgumentNullException("routeFactory", "Route Factory cannot be null"));
string[] array = new Uri(route, UriKind.RelativeOrAbsolute).OriginalString.Split(new char[2]
{
'/',
'\\'
}, StringSplitOptions.RemoveEmptyEntries);
foreach (string text in array)
{
if (IsImplicit(text))
{
throw new ArgumentException("Route contains invalid characters in \"" + text + "\"");
}
}
RouteFactory value = null;
if (s_routes.TryGetValue(route, out value) && !value.Equals(routeFactory))
{
throw new ArgumentException("Duplicated Route: \"" + route + "\"");
}
}
register code
最新推荐文章于 2023-10-06 12:50:33 发布