Last active
August 29, 2015 13:56
-
-
Save JohanLarsson/9160226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NinjectLoader : IContentLoader | |
| { | |
| private readonly IKernel _kernel; | |
| internal readonly Dictionary<string, Func<IKernel, object>> Locator = new Dictionary<string, Func<IKernel, object>>(); | |
| private readonly string[] _splits = { "component/" }; | |
| public NinjectLoader(IKernel kernel) | |
| { | |
| _kernel = kernel; | |
| Add<Content.LanguagesView>(); | |
| Add<Pages.OrderView>(); | |
| Add<Pages.Parameters>(); | |
| Add<Pages.SettingsView>(); | |
| Add<Pages.SettingsTabs>(); | |
| Add<Pages.Home>(); | |
| } | |
| private void Add<T>() where T : UserControl | |
| { | |
| var type = typeof(T); | |
| Locator.Add(type.Name + ".xaml", x => x.Get<T>()); | |
| } | |
| public Task<object> LoadContentAsync(Uri uri, CancellationToken cancellationToken) | |
| { | |
| var key = uri.ToString().Split('/').Last(); | |
| var page = Locator[key].Invoke(_kernel); | |
| return Task.FromResult(page); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Test] | |
| public void LocatorTest() | |
| { | |
| var assembly = typeof(MainWindow).Assembly; | |
| var pages = assembly.GetTypes().Where(x => x.IsSubclassOf(typeof(UserControl))); | |
| var loader = IoC.Container.Get<NinjectLoader>(); | |
| var types = new List<Type>(); | |
| var name = assembly.GetName().Name; | |
| foreach (var page in pages) | |
| { | |
| if (!loader.Locator.ContainsKey(page.Name + ".xaml")) | |
| { | |
| types.Add(page); | |
| } | |
| } | |
| if (types.Any()) // Print the code to paste into NinjectLoader ctor | |
| { | |
| foreach (var page in pages) | |
| { | |
| var pageName = page.FullName.Substring(name.Length + 1); | |
| Console.WriteLine(@"Add<{0}>();", pageName); | |
| } | |
| } | |
| Assert.IsFalse(types.Any()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NinjectReflectionLoader : IContentLoader | |
| { | |
| private readonly IKernel _kernel; | |
| private readonly Type[] _userControlTypes; | |
| public NinjectReflectionLoader(IKernel kernel) | |
| { | |
| _kernel = kernel; | |
| _userControlTypes = typeof(MainWindow).Assembly.GetTypes() | |
| .Where(x => x.IsSubclassOf(typeof(UserControl))) | |
| .ToArray(); | |
| } | |
| public Task<object> LoadContentAsync(Uri uri, CancellationToken cancellationToken) | |
| { | |
| var key = uri.ToString().Split('/').Last(); | |
| var typeName = key.Substring(0, key.Length - 5); | |
| var type = _userControlTypes.Single(x => x.Name == typeName); | |
| return Task.FromResult(_kernel.Get(type)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment