register code



 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 + "\"");
            }
        }
		
		
		
		
		
		
		
		




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值