我的MainWindow中有一个用户控件(UserLogin.xaml),我想通过单击UserLogin用户控件中的按钮导航到另一个用户控件(ShowPage.xaml)。我想在代码中这样做。
这是我的UserLogin UserControl。
<UserControl x:Class="bAV.UserLogin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="{Binding SystemParameters.PrimaryScreenHeight}"
Width="{Binding SystemParameters.PrimaryScreenWidth}">
<Grid>
<Button Content="LogIn" Name="btnlogin" Click="btnlogin_Click" />
</Grid>
</UserControl>
单击该按钮时,我想导航到另一个UserControl ShowPage.xaml。
private void btnlogin_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("Showpage.xaml", UriKind.Relative);
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Navigate(uri);
//here I am getting object reference not set to an instance of an object
}
这是MainWindow
<Window x:Class="bAV.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" xmlns:my="clr-namespace:bAV" WindowState="Maximized">
<Grid>
<my:UserLogin HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Window>
我要导航到新的UserControl。
<UserControl x:Class="bAV.Showpage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="{Binding SystemParameters.PrimaryScreenHeight}"
Width="{Binding SystemParameters.PrimaryScreenWidth}">
<Grid>
</Grid>
</UserControl>
答案 0 :(得分:2)
NavigationService(您的尝试)
WPF有两个导航器:NavigationWindow
和Frame
。只有这些导航器具有NavigationService来导航内容。因此,您首先必须将UserLogin
置于框架中,否则当您致电NavigationService.GetNavigationService
时,您将获得 null 。
<Window x:Class="bAV.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" xmlns:my="clr-namespace:bAV" WindowState="Maximized">
<Grid>
<Frame Source="UserLogin.xaml" />
</Grid>
</Window>
不使用导航服务的另一种解决方案
将两个UserControls添加到MainWindow,首先是Showpage,然后是UserLogin,首先只显示UserLogin并隐藏Showpage。
<Window x:Class="bAV.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" xmlns:my="clr-namespace:bAV" WindowState="Maximized">
<Grid>
<my:Showpage />
<my:UserLogin />
</Grid>
</Window>
单击按钮时,隐藏UserLogin以显示Showpage。
private void btnlogin_Click(object sender, RoutedEventArgs e)
{
this.Visibility = System.Windows.Visibility.Collapsed;
}
答案 1 :(得分:0)
翻译Google翻译,抱歉:)
在下面描述一个简单的解决方案:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0">
<ListBoxItem Name="PrimeiraJanela"
MouseUp="PrimeiraJanela_MouseUp">
Primeira opção</ListBoxItem>
<ListBoxItem Name="SegundaJanela"
MouseUp="SegundaJanela_MouseUp">
Segunda opção</ListBoxItem>
</ListBox>
<ContentControl x:Name="UserControls" Grid.Column="1"/>
</Grid>
public partial class MainWindow : Window
{
UserControl1 user1 = new UserControl1();
UserControl2 user2 = new UserControl2();
public MainWindow()
{
InitializeComponent();
}
private void PrimeiraJanela_MouseUp(object sender, MouseButtonEventArgs e)
{
UserControls.Content = null;
UserControls.Content = user1;
}
private void SegundaJanela_MouseUp(object sender, MouseButtonEventArgs e)
{
UserControls.Content = null;
UserControls.Content = user2;
}
}
这是我所知道的最简单的解决方案。 (我是WPF的初学者)