WPF 中的 UserControl VS 页面 [英] UserControl VS Page in WPF

查看:29
本文介绍了WPF 中的 UserControl VS 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的应用程序,它有一个菜单,当用户选择每个 MenuItem 时,我想更改窗口的内容以显示所选内容.

I'm on writing a simple application, it has a menu and when user choices each MenuItem, i want to change my window's content to display the selected content.

我有两个选择.

  1. 我可以在我的窗口中添加一个 并编写一些页面.
  2. 我可以写一些UserControls并将它们放在ContentControl
  1. i can add a <Frame></Frame> to my window and write some pages.
  2. i can write some UserControls and put them in a ContentControl

当用户触发 MenuItem 点击事件时.

as user fires MenuItem click event.

所以我很困惑为此目的选择正确的选择.

so I'm confused to select the right choice for this purpose.

推荐答案

使用 Frame/Pages 或 ContentControl/Views 可以成功实现导航.这是一个选择问题.

Navigation can be succefully implemented by using Frame/Pages or ContentControl/Views. It is a matter of choice.

然而,Frame/Page 有一些问题,例如page.DataContext 不是从父 Frame 继承的?

However, Frame/Page have some gotchas, e.g. page.DataContext not inherited from parent Frame?

如果您不需要专门隔离,则坚持使用 ContentControl. 框架中的导航是用位于的区域构建的在不同类型的控件中(例如 ContentControl、TabControl),而不是 Frame(查看文档)

If you don't need isolation specifically, then stick to ContentControl. Navigation in prism framework is built with regions which are located in different type of controls (e.g. ContentControl, TabControl), not Frame (see docs)

另一种简单导航方法是基于 ViewModel.

one more approach for simple navigation is ViewModel based.

示例:

WPF MVVM 导航视图

Rachel Lim 使用 MVVM 导航(外部)

这篇关于WPF 中的 UserControl VS 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆