中文來源:
http://blog.163.com/liulei0729@126/blog/static/32530612201211362532114/
Page 是從 UserControl 繼承的,首先就有著「is a UserControl」 的關係;如果要找更完整的
Page 封裝了對 Frame 的導航,這也是 Page 的最大特點和職責,而 UserControl 沒有這個職責。
因為 UserControl 從 UIElement 繼承,這樣就導致了 UserControl 可以用在
this.RootVisual = userControl ;
Grid.Children.Add( userControl );
等等地方使用(其效果往往就是使UserControl呈現在界面上),而由於「Page is a UserControl」 ,這就往往讓人搞不清楚是要使用Page還是UserControl。在使用場合上,個人認為是顧名思義就可以了,如果你用過 ASP.NET ,那麼 Page 就相當於是一個個的 .aspx 頁面,而 UserControl 則是 .aspx 中使用的自定義控件。
stackoverflow
https://stackoverflow.com/questions/12206120/window-vs-page-vs-usercontrol-for-wpf-navigation
A Window object is just what it sounds like: its a newWindowfor your application. You should use it when you want to pop up an entirely new window. I don't often use more than oneWindowin WPF because I prefer to put dynamic content in my main Window that changes based on user action.
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 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 aUserControlwhen I want to build in some custom functionality (for example, aCalendarControl), or when I have a large amount of related XAML code, such as aViewwhen using the MVVM design pattern.
When navigating between windows, you could simply create a newWindowobject and show it
but like I said at the beginning of this answer, I prefer not to manage multiple windows if possible.var NewWindow = new MyWindow(); newWindow.Show();
My preferred method of navigation is to create some dynamic content area using aContentControl, and populate that with aUserControlcontaining whatever the current view is.
and in your navigate event you can simply set it using<Window x:Class="MyNamespace.MainWindow" ...> <DockPanel> <ContentControl x:Name="ContentArea" /> </DockPanel> </Window>
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:ContentArea.Content = new MyUserControl();
<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>
如果還是不懂,還有日文版
https://qiita.com/nie/items/3e2f6f37b3425585952b
意外と説明しているところが無かったので
WPF Window
獨立して表示可能な存在、PageやUserControlのホストになる存在。
WPF Page
ホスト下で表示し、「進む」「戻る」などのナビゲーションアプリケーションでの使用を前提とした使い方。
ホストで使用するにはフレームコントロールを経由して使います。
WPF UserControl
コントロールを自作します。これ単體では獨立して動作せず、Window內に設置する必要があります。
自作した後、一度プロジェクトをビルドしないとコントロールを設置出來ません。
自作コントロールの使い方は通常コントロールと使用方法は同じです。
コントロールに固有名詞を設定すれば、メインウィンドウからメソッドの呼び出しも可能です。
例:自作コントロールに固有名詞 MyControlを設定
メインウィンドウからボタンを押すと、MyControl內の DoWorkメソッドを実行する場合自作コントロール內部のメソッドvoid DoWork() { ///自作コントロールに実行させたい挙動 }
メインウィンドウとは無関係に、自作コントロール內のボタンを押した場合の挙動は、自作コントロール側に挙動を書き込みます。メインウィンドウからボタンを押すと、自作コントロール內のメソッドを呼び出すprivate void Button_Click(object sender, RoutedEventArgs e) { MyControl.DoWork(); }
これは深いことを考えず、ウィンドウにボタンを設置してその挙動を書く時のコードと同じです。

沒有留言:
張貼留言