【发布时间】:2014-10-09 06:35:49
【问题描述】:
我的 MainWindow 中有一个用户控件 (UserLogin.xaml),我想通过单击 UserLogin 用户控件内的按钮导航到另一个用户控件 (ShowPage.xaml)。我想在代码中做到这一点。
这是我的 UserLogin 用户控件。
<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
}
这是主窗口
<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>
【问题讨论】:
-
改进你的问题,解释你想要什么(用代码或绘图),我不明白你的意思是“打开新的用户控件”/“导航到用户控件”,你的意思是用户控件在 Frame 内,单击按钮将导航到新创建的用户控件?
-
我在该窗口中有一个主窗口,我称之为我的第一个创建的用户控件。当单击此用户控件中的按钮时,我想打开新创建的第二个用户控件
-
在代码中有很多方法可以做到这一点。但是还需要更多细节,比如用户控件是如何实现的(这样我们就可以连接按钮的 Click 事件),以及它是如何添加到 MainWindow 中的(保存用户控件的容器是什么?)
-
在 Mainwindow.xaml 我已经像这样调用了我的第一个 uesrconyrol
跨度> -
在 Userlogin.xaml 之后这是用户控件。在这个用户控件中我有按钮。当单击此按钮时,我想显示另一个用户控件,即 usercontrol2.xaml
标签: wpf navigation