Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PrismでViewとViewModelを別アセンブリにする場合のコードサンプル
ViewTypeToViewModelTypeResolver _resolver;
protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
_resolver = new ViewTypeToViewModelTypeResolver(typeof(MainWindowViewModel).Assembly); // とりあえず適当なVMからAssembly取得して設定しておく
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(_resolver.Resolve);
}
public class ViewTypeToViewModelTypeResolver
{
private readonly Assembly _assembly;
public ViewTypeToViewModelTypeResolver(Assembly assembly)
{
_assembly = assembly;
}
public Type Resolve(Type viewType)
{
if (viewType == null) throw new ArgumentNullException(nameof(viewType));
// ReSharper disable once PossibleNullReferenceException
var vmTypeName = $"{viewType.Namespace.Replace("Views", "ViewModels")}.{viewType.Name}ViewModel";
var vmType = _assembly.GetType(vmTypeName);
return vmType;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment