我目前正在编写一个桌面应用程序,但我似乎无法理解在将某人重定向到应用程序的新部分时要使用的内容.
我的选择似乎是
但是我不明白它们之间的区别是什么,以及什么时候我应该使用它们.
有人可以为我解释这些差异,并举例说明你可以使用哪些情况/应用程序?
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)

Tig*_*ran 13
Windows.Forms.Form,所以只是一个新窗口根据在线文档,页面是:
封装可以导航到Windows Internet Explorer,NavigationWindow和Frame并由其托管的内容页面.
因此,如果你想象一些HTML内容,你基本上使用它
UserControl适用于您想要创建一些可重用组件(但不是独立组件)以在多个不同组件中使用它的情况Windows
一切都取决于您正在尝试构建的应用程序.Window如果您正在构建基于对话框的应用程序,请使用s.Page如果您正在构建基于导航的应用程序,请使用s .UserControl无论您走的方向如何,s都将非常有用,因为您可以在Windows和Pages中使用它们.
一个开始探索的好地方是:http://windowsclient.net/learn
我们通常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 添加页面,同时添加新页面检查控件是否已经存在然后只导航,否则添加和导航。我希望这会帮助某人