C# 用于 WPF 导航的窗口 vs 页面 vs UserControl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12206120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:27:49  来源:igfitidea点击:

Window vs Page vs UserControl for WPF navigation?

c#wpf

提问by Steve

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.

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

My options appear to be

我的选择似乎是

  • Window
  • Page
  • UserControl
  • 窗户
  • 用户控件

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?

有人可以为我解释这些差异,并举例说明您可以将每种情况/应用程序用于哪些情况?

回答by Tigran

  • Windowis like Windows.Forms.Form, so just a new window
  • Pageis, according to online documentation:

    Encapsulates a page of content that can be navigated to and hosted by Windows Internet Explorer, NavigationWindow, and Frame.

    So you basically use this if going you visualize some HTML content

  • UserControlis for cases when you want to create some reusable component (but not standalone one) to use it in multiple different Windows

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

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

    所以如果你要可视化一些 HTML 内容,你基本上会使用它

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

回答by sellmeadog

All depends on the app you're trying to build. Use Windows if you're building a dialog based app. Use Pages if you're building a navigation based app. UserControls will be useful regardless of the direction you go as you can use them in both Windows and Pages.

一切都取决于您尝试构建的应用程序。Window如果您正在构建基于对话框的应用程序,请使用s。Page如果您正在构建基于导航的应用程序,请使用s 。UserControl无论您往哪个方向发展,s 都会很有用,因为您可以在 Windows 和 Pages 中使用它们。

A good place to start exploring is here: http://windowsclient.net/learn

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

回答by Rachel

A Windowobject is just what it sounds like: its a new Windowfor your application. You should use it when you want to pop up an entirely new window. I don't often use more than one Windowin WPF because I prefer to put dynamic content in my main Window that changes based on user action.

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

A Pageis 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 Applicationslike sellmeadog said.

一个网页是你的窗口内的页面。它主要用于基于 Web 的系统,例如 XBAP,在这种系统中您有一个浏览器窗口,并且可以在该窗口中托管不同的页面。它也可以用在导航应用程序中,sellmedog 所说

A UserControlis 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 UserControlwhen 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 Viewwhen using the MVVM design pattern.

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

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

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

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.

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

My preferred method of navigation is to create some dynamic content area using a ContentControl, and populate that with a UserControlcontaining whatever the current view is.

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

<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();

But if you're working with WPF, I'd highly recommend the MVVM design pattern. I have a very basic example on my blogthat illustrates how you'd navigate using MVVM, using this pattern:

但如果您使用 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>

Screenshot1Screenshot2

截图1截图2

回答by Hitesh

Most of all has posted correct answer. I would like to add few links, so that you can refer to them and have clear and better ideas about the same:

最重要的是已经发布了正确的答案。我想添加几个链接,以便您可以参考它们并对此有清晰和更好的想法:

UserControl: http://msdn.microsoft.com/en-IN/library/a6h7e207(v=vs.71).aspx

用户控制:http: //msdn.microsoft.com/en-IN/library/a6h7e207(v= vs.71).aspx

The difference between page and window with respect to WPF: Page vs Window in WPF?

页面和窗口在 WPF方面的区别:WPF 中的 页面与窗口?

回答by dnxit

We usually use One Main Windowfor the application and other windows can be used in situations like when you need popups because instead of using popup controls in XAML which are not visible we can use a Window that is visible at design time so that'll be easy to work with

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

on the other hand we use many pages to navigate from one screen to another like User management screen to Order Screen etc In the main Window we can use Framecontrol for navigation like below XAML

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

    <Frame Name="mainWinFrame" NavigationUIVisibility="Hidden"  ButtonBase.Click="mainWinFrame_Click">
    </Frame>

C#

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");
        }
    }

That's one way of doing it We can also use a Tab Control instead of Fram and Add pages to it using a Dictionary while adding new page check if the control already exists then only navigate otherwise add and navigate. I hope that'll help someone

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