Created
August 5, 2017 04:34
-
-
Save kuju63/2f97ea24319855d591841c694a04e01f to your computer and use it in GitHub Desktop.
Xamarin.Forms + PrismでViewModelの名称をViewModelとして名前解決する場合の設定
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
using System; | |
using System.Reflection; | |
using Prism.Unity; | |
using Alermer.Views; | |
using Prism.Mvvm; | |
using Xamarin.Forms; | |
namespace Alermer | |
{ | |
public partial class App : PrismApplication | |
{ | |
public App(IPlatformInitializer initializer = null) : base(initializer) { } | |
protected override void OnInitialized() | |
{ | |
InitializeComponent(); | |
NavigationService.NavigateAsync("NavigationPage/MainPage?title=Hello%20from%20Xamarin.Forms"); | |
} | |
protected override void RegisterTypes() | |
{ | |
Container.RegisterTypeForNavigation<NavigationPage>(); | |
Container.RegisterTypeForNavigation<MainPage>(); | |
} | |
protected override void ConfigureViewModelLocator() | |
{ | |
base.ConfigureViewModelLocator(); | |
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver( | |
viewType => | |
{ | |
var viewName = viewType.FullName; | |
viewName = viewName.Replace(".Views.", ".ViewModels."); | |
string viewModelName; | |
if (viewName.EndsWith("Page")) | |
{ | |
viewModelName = | |
$"{viewName.Substring(0, viewName.LastIndexOf("Page", StringComparison.Ordinal))}ViewModel"; | |
} | |
else | |
{ | |
var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel"; | |
viewModelName = $"{viewName}{suffix}"; | |
} | |
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName; | |
return Type.GetType($"{viewModelName}, {viewAssemblyName}"); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment