用于WPF导航的Window vs Page vs UserControl?

Ste*_*eve 186 c# wpf

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

我的选择似乎是

  • 窗口
  • 用户控件

但是我不明白它们之间的区别是什么,以及什么时候我应该使用它们.

有人可以为我解释这些差异,并举例说明你可以使用哪些情况/应用程序?

Rac*_*hel 325

一个Window对象就是它的声音:它是一个Window适用于您的应用程序的新对象.当你想要弹出一个全新的窗口时,你应该使用它.我不经常Window在WPF中使用多个,因为我更喜欢将动态内容放在我的主窗口中,该内容根据用户操作而变化.

一个网页是你的窗口内的页面.它主要用于基于Web的系统,如XBAP,您可以在其中拥有单个浏览器窗口,并且可以在该窗口中托管不同的页面.它也可以用于像sellmeadog这样的导航应用程序.

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

在窗口之间导航时,您只需创建一个新Window对象并显示它即可

var NewWindow = new MyWindow();
newWindow.Show();
Run Code Online (Sandbox Code Playgroud)

但就像我在这个答案的开头所说的那样,如果可能的话,我宁愿不管理多个窗口.

我首选的导航方法是使用a创建一些动态内容区域ContentControl,并使用UserControl包含当前视图的内容填充它.

<Window x:Class="MyNamespace.MainWindow" ...>
    <DockPanel>
        <ContentControl x:Name="ContentArea" />
    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

在您的导航事件中,您可以使用它进行简单设置

ContentArea.Content = new MyUserControl();
Run Code Online (Sandbox Code Playgroud)

但是如果你正在使用WPF,我强烈推荐MVVM设计模式.我在我的博客上有一个非常基本的例子,说明了如何使用这种模式使用MVVM进行导航:

<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>
Run Code Online (Sandbox Code Playgroud)

Screenshot1 Screenshot2

  • @Herrozerro如果我想使用MVVM创建一个Audit表单,我会有一个`AuditViewModel`包含表单的所有数据和功能,我会使用`AuditView` UserControl或者只是一个`DataTemplate来绘制它` (2认同)

Tig*_*ran 13

  • 窗口就像Windows.Forms.Form,所以只是一个新窗口
  • 根据在线文档,页面是:

    封装可以导航到Windows Internet Explorer,NavigationWindow和Frame并由其托管的内容页面.

    因此,如果你想象一些HTML内容,你基本上使用它

  • UserControl适用于您想要创建一些可重用组件(但不是独立组件)以在多个不同组件中使用它的情况Windows

  • 还有一个我认为应该添加的项目:`DataTemplates`.当您想要告诉WPF如何在特定范围内绘制项目时,可以使用这些.例如,如果你想把你的`Buttons'画成圆圈,你可以简单地使用`DataTemplate`而不是'UserControl`.当我想要一个具有自己功能的新控件时,或者当我有一个单独组件的大量XAML时,我通常使用`UserControls`,例如``View`.对于不需要任何特殊功能的较小位XAML,您应该使用`DataTemplate`而不是创建`UserControl` (6认同)
  • 一般来说,`Page`的内容不是HTML而是XAML.但是,"页面"与导航框架相关联,在概念上类似于在Web浏览器中完成导航的方式.(如果应用程序是XBAP应用程序,页面甚至可以托管在浏览器中.) (3认同)

sel*_*dog 6

一切都取决于您正在尝试构建的应用程序.Window如果您正在构建基于对话框的应用程序,请使用s.Page如果您正在构建基于导航的应用程序,请使用s .UserControl无论您走的方向如何,s都将非常有用,因为您可以在Windows和Pages中使用它们.

一个开始探索的好地方是:http://windowsclient.net/learn


dnx*_*xit 6

我们通常Window为应用程序使用 One Main ,其他窗口可以在需要弹出窗口的情况下使用,因为我们可以使用在设计时可见的窗口,而不是在 XAML 中使用不可见的弹出控件,这样会很容易跟...共事

另一方面,我们使用许多页面从一个屏幕导航到另一个屏幕,例如用户管理屏幕到订单屏幕等在主窗口中,我们可以使用Frame控件进行导航,如下面的 XAML

    <Frame Name="mainWinFrame" NavigationUIVisibility="Hidden"  ButtonBase.Click="mainWinFrame_Click">
    </Frame>
Run Code Online (Sandbox Code Playgroud)

C#

     private void mainWinFrame_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (e.OriginalSource is Button)
            {
                Button btn = (Button)e.OriginalSource;

                if ((btn.CommandParameter != null) && (btn.CommandParameter.Equals("Order")))
                {

                    mainWinFrame.Navigate(OrderPage);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error");
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是一种方法 我们也可以使用 Tab Control 而不是 Fram 并使用 Dictionary 添加页面,同时添加新页面检查控件是否已经存在然后只导航,否则添加和导航。我希望这会帮助某人