用于 WPF 导航的窗口 vs 页面 vs UserControl? [英] Window vs Page vs UserControl for WPF navigation?

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

问题描述

我目前正在编写一个桌面应用程序,但在将某人重定向到应用程序的新部分时,我似乎无法理解该使用什么.

I 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?

推荐答案

Window 对象正如其名:它是您应用程序的新 Window.当你想弹出一个全新的窗口时,你应该使用它.我不经常在 WPF 中使用多个 Window,因为我更喜欢将动态内容放在我的主窗口中,这些内容会根据用户操作而改变.

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.

页面 是窗口内的页面.它主要用于基于 Web 的系统,例如 XBAP,在这种系统中您有一个浏览器窗口,并且可以在该窗口中托管不同的页面.它也可以用于导航应用程序,例如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.

UserControl 是一个可重复使用的用户创建的控件,您可以像添加任何其他控件一样将其添加到 UI 中.通常,当我想要构建一些自定义功能(例如,CalendarControl),或者当我有大量相关的 XAML 代码时,我会创建一个 UserControl,例如使用 MVVM 设计模式时的 View.

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.

在窗口之间导航时,您可以简单地创建一个新的 Window 对象并显示它

When navigating between windows, you could simply create a new Window object and show it

var NewWindow = new MyWindow();
newWindow.Show();

但是就像我在本答案开头所说的那样,如果可能,我不想管理多个窗口.

but like I said at the beginning of this answer, I prefer not to manage multiple windows if possible.

我首选的导航方法是使用 ContentControl,并使用包含当前视图的 UserControl 填充它.

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>

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

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