我正在学习windows store应用程序中的棱镜。我遇到了问题。我有很多意见,我想将它们分成不同的文件夹。但问题在于NavigationService无法找到它们。说明书说,所有视图显示放入视图文件夹,然后导航服务可以使用navigationservice.navigate(“main”,null)导航到它们,但是如果视图不在导航服务找不到它们的视图文件夹的根目录棱镜如何找到视图文件夹的子文件夹中的视图
0
A
回答
5
NavigationService根据约定查找Views和ViewModels。所有视图必须位于“视图”文件夹中,并且ViewModels必须位于“ViewModels”文件夹中。如果ID不适合你可以做到以下几点:
添加下面的类到你的项目:
public class ViewViewModelTypeResolver { private const string ViewNameSuffix = "Page"; // You can change this View name suffix private const string ViewModelNameSuffix = "ViewModel"; // You can change this ViewModel name suffix private readonly Lazy<Dictionary<string, Type>> _uiAssemblyExportedTypes; private Dictionary<string, Type> UiAssemblyExportedTypes { get { return _uiAssemblyExportedTypes.Value; } } public ViewViewModelTypeResolver(Type typeFromUiAssembly) { _uiAssemblyExportedTypes = new Lazy<Dictionary<string, Type>>(() => GetUiAssemblyExportedTypes(typeFromUiAssembly)); } public Type GetViewType(string viewTypeName) { return UiAssemblyExportedTypes[viewTypeName]; } public Type GetViewModelType(Type viewType) { var pageNameWithoutSuffix = viewType.Name.Remove(viewType.Name.LastIndexOf(ViewNameSuffix, StringComparison.Ordinal)); var viewModelName = String.Concat(pageNameWithoutSuffix, ViewModelNameSuffix); return UiAssemblyExportedTypes[viewModelName]; } private static Dictionary<string, Type> GetUiAssemblyExportedTypes(Type typeFromUiAssembly) { return typeFromUiAssembly.GetTypeInfo().Assembly.ExportedTypes.ToDictionary(type => type.Name, type => type, StringComparer.Ordinal); } }
添加ViewViewModelTypeResolver到App构造函数:
public App() { var mainPage = typeof(MainPage); // Any of pages from your solution. Pay attention that the resolver will be able to resolve Views and ViewModels that locates in the same project as specified page. _viewViewModelTypeResolver = new ViewViewModelTypeResolver(mainPage); }
当你使用Prism时,我假设你从
MvvmAppBase
继承你的App类。如果是这样,你应该重写GetPageType
方法:protected override Type GetPageType(string pageToken) { return _viewViewModelTypeResolver.GetViewType(pageToken); }
然后在
OnInitializeAsync
方法添加SetDefaultViewTypeToViewModelTypeResolver
:protected override async Task OnInitializeAsync(IActivatedEventArgs args) { // Your initialization code ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(GetViewModelType); // Your initialization code } private Type GetViewModelType(Type pageType) { return _viewViewModelTypeResolver.GetViewModelType(pageType); }
现在你可以浏览到任何网页上,即使它们被分成不同的文件夹。
0
来自Alexander.Ermolaev的很好回答。我想扩展他的课程以满足我的需求。我不喜欢公开所有的ViewModel。当我们真正需要的是Views和ViewModels的类型时,我也不想保留所有类型的字典。这将查找从UserControl继承的所有视图,因为ViewModelLocator将与UserControl以及Pages一起使用。这将找到所有从基本视图模型类继承的ViewModel,通常是Prism的ViewModelBase。从App.cs
sealed class ViewViewModelTypeResolver
{
private readonly Assembly _assembly;
private readonly Type _baseViewModelType;
private readonly string _viewNameSuffix;
private readonly string _viewModelNameSuffix;
private readonly Lazy<Dictionary<string, Type>> _views;
private readonly Lazy<Dictionary<string, Type>> _viewModels;
private Dictionary<string, Type> Views => _views.Value;
private Dictionary<string, Type> ViewModels => _viewModels.Value;
/// <summary>
/// Resolves Views and ViewModels.
/// </summary>
/// <param name="typeFromUiAssembly">Use any type from the UI Assembly.</param>
/// <param name="baseViewModelType">All view models need to inherit from this type.</param>
/// <param name="viewNameSuffix">Prism convention is to name your views with a Page suffix.</param>
/// <param name="viewModelNameSuffix">Prism convention is name your viewmodels with a ViewModel suffix.</param>
public ViewViewModelTypeResolver(Type typeFromUiAssembly, Type baseViewModelType, string viewNameSuffix = "Page", string viewModelNameSuffix = "ViewModel")
{
_baseViewModelType = baseViewModelType;
_viewNameSuffix = viewNameSuffix;
_viewModelNameSuffix = viewModelNameSuffix;
_assembly = typeFromUiAssembly.GetTypeInfo().Assembly;
_views = new Lazy<Dictionary<string, Type>>(GetViews);
_viewModels = new Lazy<Dictionary<string, Type>>(GetViewModels);
}
/// <summary>
/// Gets a View type for the given name.
/// </summary>
/// <param name="viewTypeName">Name of the view without the suffix.</param>
public Type GetViewType(string viewTypeName)
{
var pageName = string.Concat(viewTypeName, _viewNameSuffix);
return this.Views[pageName];
}
/// <summary>
/// Gets a ViewModel for the given view type.
/// </summary>
/// <param name="viewType">Type of view.</param>
public Type GetViewModelType(Type viewType)
{
var viewModelName = string.Concat(viewType.Name, _viewModelNameSuffix);
return this.ViewModels[viewModelName];
}
/// <summary>
/// Gets all the View types by finding all types that inherit from Page and are defined in the UI Assembly.
/// </summary>
private Dictionary<string, Type> GetViews()
{
var types = _assembly.DefinedTypes.Where(type => !type.IsAbstract && type.IsSubclassOf(typeof(UserControl)));
return types.ToDictionary(typeInfo => typeInfo.Name, typeInfo => typeInfo.AsType(), StringComparer.Ordinal);
}
/// <summary>
/// Gets all the ViewModel types by finding all types that inherit from a base ViewModel type defined in the UI Assembly.
/// </summary>
private Dictionary<string, Type> GetViewModels()
{
var types = _assembly.DefinedTypes.Where(type => !type.IsAbstract && type.IsSubclassOf(_baseViewModelType));
return types.ToDictionary(typeInfo => typeInfo.Name, typeInfo => typeInfo.AsType(), StringComparer.Ordinal);
}
}
用法:
private readonly ViewViewModelTypeResolver _viewViewModelTypeResolver;
public App()
{
this.InitializeComponent();
_viewViewModelTypeResolver = new ViewViewModelTypeResolver(this.GetType(), typeof(ViewModelBase));
}
//other methods are the same as Alexander's answer.
感谢亚历山大,你的代码对我来说是很好的起点。
相关问题
- 1. ServiceStack /视图子文件夹
- 2. 从视图文件夹中的文件导航到laravel中视图文件夹中的另一个文件
- 3. Kohana - 视图文件夹内的子文件夹
- 4. ASP MVC 5 - 视图文件夹外的部分视图文件
- 5. 子文件夹视图expressjs 3x/nodejs
- 6. CodeIgniter:从子文件夹访问视图
- 7. 子文件夹res文件夹没有找到图片
- 8. CSHTML文件在视图文件夹外
- 9. 如何更改棱镜中的视图?
- 10. 视图中的HAML文件夹
- 11. C#表单中的文件夹视图
- 12. 清单中的Plone文件夹视图
- 13. HMVC和文件夹中的视图(Codeigniter)
- 14. 如何查找树视图中根文件夹的索引?
- 15. 获取文件夹到树形视图
- 16. 的mvc操作链接重定向到视图文件夹中的文件夹
- 17. 如何监视新文件或文件夹的文件夹
- 18. 如何从Sdcard文件夹的子文件夹获取图像?
- 19. 在特定文件夹的透视图中添加视图
- 20. 如何让MVC在嵌套文件夹中查找视图
- 21. 如何在视图文件夹中包含js.erb文件
- 22. 将文件排序到树视图中的月份命名的子文件夹
- 23. 更正用户文件夹的视图
- 24. ASP.NET MVC - 当前文件夹的视图
- 25. VB.NET +将文件夹添加到树视图和文件到列表视图
- 26. sencha cmd,如何在子文件夹中生成视图?
- 27. 找到文件夹内的文件夹
- 28. 500视频文件到一个文件夹的批量截图
- 29. 如何从资产文件夹的子文件夹中播放视频android
- 30. .htaccess - 子文件夹到子文件夹=子文件夹404
欢迎来到StackOverflow!为了让人们更容易回答,您可能需要提供有关您遇到的问题的更多详细信息。代码或屏幕截图,以及对问题和已经尝试过的更详细的解释可以提供帮助。 – Denise 2014-09-23 02:46:55