窗口VS网页与用户控件为WPF导航? [英] Window vs Page vs UserControl for WPF navigation?
问题描述
我想知道,如果有人可以帮助我。我是新来WPF和我目前正在写一个桌面应用程序,但我似乎无法让我的头周围有人重定向到应用程序的一个新的部分时,使用什么。
I wondered if someone could help me. I'm new to WPF and am currently writing a desktop application, but I cannot seem to get my head around what to use when redirecting someone to a new section of the application.
我的选择似乎是
- 窗口
- 页
- 用户控件
但我不明白它们之间的区别是什么,当我应该使用每个人。
but I don't understand what the difference between them is, and when I should use each one.
有人能解释我的差异,并给出你可以使用什么情况下/应用程序每一个的例子?
Could someone explain the differences for me, and give an example of what situations/applications you may use each one for?
推荐答案
A 窗口对象正是这听起来像:它是一个新的窗口
为您的应用。当你想弹出一个全新的窗口,你应该使用它。我不经常使用一个以上的窗口
在WPF,因为我preFER将动态内容在变化基于用户行为我的主窗口。
A Window object is just what it sounds like: its a new Window
for your application. You should use it when you want to pop up an entirely new window. I don't often use more than one Window
in WPF because I prefer to put dynamic content in my main Window that changes based on user action.
A 网页是窗口内的页面。它主要用于像一个XBAP,在那里你有一个浏览器窗口,不同的页面可以在窗口中承载基于Web的系统。它也可以在导航应用的喜欢的 sellmeadog说。
A Page is a page inside your Window. It is mostly used for web-based systems like an XBAP, where you have a single browser window and different pages can be hosted in that window. It can also be used in Navigation Applications like sellmeadog said.
A 用户控件是一个可重用的用户创建的控件,您可以添加到您的用户界面,你会添加任何其他控制方式相同。通常我创建一个用户控件
时,我想建立一些自定义的功能(例如, CalendarControl
),或当我有大量相关的XAML code的,如查看使用MVVM设计模式时
。
A UserControl is a reusable user-created control that you can add to your UI the same way you would add any other control. Usually I create a UserControl
when I want to build in some custom functionality (for example, a CalendarControl
), or when I have a large amount of related XAML code, such as a View
when using the MVVM design pattern.
在窗口之间进行导航,你可以简单地创建一个新的窗口
对象,并显示它
When navigating between windows, you could simply create a new Window
object and show it
var NewWindow = new MyWindow();
newWindow.Show();
但就像我在这个答案开始所说的,我preFER如果可能的话不是管理多个窗口。
but like I said at the beginning of this answer, I prefer not to manage multiple windows if possible.
导航我的preferred方法是使用创建一些动态内容区<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx\"><$c$c>ContentControl$c$c>,并填充以用户控件
包含无论目前的观点。
My preferred method of navigation is to create some dynamic content area using a ContentControl
, and populate that with a UserControl
containing whatever the current view is.
<Window x:Class="MyNamespace.MainWindow" ...>
<DockPanel>
<ContentControl x:Name="ContentArea" />
</DockPanel>
</Window>
和在你的导航事件中,你可以通过简单地将其
and in your navigate event you can simply set it using
ContentArea.Content = new MyUserControl();
但如果你使用WPF的工作,我会强烈建议的MVVM设计模式。我有我的博客非常基本的例子,说明你会如何使用导航MVVM,使用这种模式:
But if you're working with WPF, I'd highly recommend the MVVM design pattern. I have a very basic example on my blog that illustrates how you'd navigate using MVVM, using this pattern:
<Window x:Class="SimpleMVVMExample.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SimpleMVVMExample"
Title="Simple MVVM Example" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:HomeViewModel}">
<local:HomeView /> <!-- This is a UserControl -->
</DataTemplate>
<DataTemplate DataType="{x:Type local:ProductsViewModel}">
<local:ProductsView /> <!-- This is a UserControl -->
</DataTemplate>
</Window.Resources>
<DockPanel>
<!-- Navigation Buttons -->
<Border DockPanel.Dock="Left" BorderBrush="Black"
BorderThickness="0,0,1,0">
<ItemsControl ItemsSource="{Binding PageViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}"
Command="{Binding DataContext.ChangePageCommand,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding }"
Margin="2,5"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
<!-- Content Area -->
<ContentControl Content="{Binding CurrentPageViewModel}" />
</DockPanel>
</Window>
这篇关于窗口VS网页与用户控件为WPF导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!