【问题标题】:How to navigate to another user control by clicking a button in a usecontrol如何通过单击 usecontrol 中的按钮导航到另一个用户控件
【发布时间】: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


【解决方案1】:

NavigationService(您的尝试)

WPF 有两个导航器:NavigationWindowFrame。只有这些导航器具有 NavigationService 来导航内容。所以你首先必须把UserLogin放在一个Frame里面,否则当你调用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;
}

【讨论】:

  • 对不起..我不太明白..在主窗口中我调用了 usercontrol
  • kennyzx 我附上了我的代码作为答案 2..请检查它
  • 您不发布答案,只需编辑您的初始帖子(添加您在 cmets 中所说的内容并添加您的代码)。您可以根据需要多次编辑它,这就是 SO 的工作原理。
  • 为了使用导航服务,您必须使用Frame 来托管您的登录控件,我更新了我的答案。
【解决方案2】:

由谷歌翻译,对不起:)

下面描述一个简单的解决方案:

  1. 我创建了两个 UserControl:UserControl1.xaml 和 UserControl2.xaml
  2. 在每个 UserControl 中,我创建了一个简单的功能,只是为了区分它们。
  3. 示例:TextBox 和 TextBlock...
  4. 在 MainWindow 中,我创建了以下内容:

<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>
  1. 目标是当你点击ListBoxItem“PrimeiraJanela”时ContentControl加载UserControl1,当你点击ListBoxItem“SegundaJanela”时ContentControl加载UserControl2。
  2. 编辑代码隐藏,执行以下操作:

    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 的初学者)

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多