Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何通过单击usecontrol中的按钮导航到其他用户控件_Wpf_Navigation - Fatal编程技术网

Wpf 如何通过单击usecontrol中的按钮导航到其他用户控件

Wpf 如何通过单击usecontrol中的按钮导航到其他用户控件,wpf,navigation,Wpf,Navigation,我在主窗口中有一个用户控件(UserLogin.xaml),我想通过单击UserLogin用户控件内的按钮导航到另一个用户控件(ShowPage.xaml)。我想用代码来做这件事 这是我的UserLogin UserControl <UserControl x:Class="bAV.UserLogin" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="h

我在主窗口中有一个用户控件(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>
这是主窗口

<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 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>

导航服务(您的尝试)

WPF有两个导航器:
NavigationWindow
Frame
。只有这些导航器具有导航服务来导航内容。因此,首先必须将
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>
导航服务(您的尝试)

WPF有两个导航器:
NavigationWindow
Frame
。只有这些导航器具有导航服务来导航内容。因此,首先必须将
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:UserControl1.xaml和UserControl2.xaml
  • 在每个UserControl中,我创建了一个简单的特性,只是为了区分它们
  • 示例:文本框和文本块
  • 在主窗口中,我创建了以下内容:
  • 
    普里梅拉·奥普朗
    Segunda opço
    
    由谷歌翻译,对不起:)

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

  • 我创建了两个UserControls:UserControl1.xaml和UserControl2.xaml
  • 在每个UserControl中,我创建了一个简单的特性,只是为了区分它们
  • 示例:文本框和文本块
  • 在主窗口中,我创建了以下内容:
  • 
    普里梅拉·奥普朗
    Segunda opço
    

    改进你的问题,解释你想要什么(用代码或图纸),我不明白你说的“打开新用户控件”/“导航到用户控件”是什么意思,你是说用户控件在一个框架内,单击该按钮将导航到新创建的usercontrol?我在该窗口中有一个主窗口,我称之为我的第一个创建的usercontrol。单击此usercontrol中的按钮时,我想打开新创建的第二个usercontrol,有许多方法可以通过代码来实现。但是还需要更多的细节,如usercontrol是如何实现的(因此我们可以连接按钮的Click事件),以及它是如何添加到MainWindow的(保存usercontrol的容器是什么?)在Mainwindow.xaml中,在Userlogin.xaml之后,我像这样调用了我的第一个uesrconyrol。这是usercontrol。在这个usercontrol中,我有一个按钮。单击这个按钮时,我想再显示一个用户控件,即usercontrol2.xamli改进你的问题,解释你想要什么(用代码或图形),我不明白你的意思“打开新用户控件”/“导航到用户控件”“,您的意思是说usercontrol位于一个框架内,单击该按钮将导航到新创建的usercontrol吗?我在该窗口内有一个主窗口,称为我的第一个创建的usercontrol。单击此usercontrol中的按钮时,我想打开新创建的第二个用户控件,有许多方法可以通过代码来实现。但是还需要更多的细节,如usercontrol是如何实现的(因此我们可以连接按钮的Click事件),以及它是如何添加到MainWindow的(保存usercontrol的容器是什么?)在Mainwindow.xaml中,我在Userlogin.xaml之后调用了我的第一个uesrconyrol。这是usercontrol。在这个usercontrol中,我有一个按钮。单击这个按钮时,我想再显示一个用户控件,它是usercontrol2。xaml抱歉..我不明白..在Mainwindow中,我调用了usercontrolkennyzx,我附上了我的代码作为答案2..请勾选它你不发布答案,只需编辑你的初始帖子(添加你在评论中所说的内容并添加代码)。您可以根据自己的意愿对其进行多次编辑,这就是其工作原理。为了使用navigationservice,您必须使用
    Frame
    来托管您的登录控件,我更新了我的答案。抱歉..我不明白..在主窗口中我调用了usercontrolkennyzx,我将我的代码作为答案附加了2..请检查您没有发布答案,只需编辑您的初始帖子(添加您在评论中所说的内容并添加代码)。您可以根据需要对其进行多次编辑,这就是其工作原理。为了使用navigationservice,您必须使用
    Frame
    来承载您的登录控件,我更新了我的答案。
    <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>
    
    private void btnlogin_Click(object sender, RoutedEventArgs e)
    {
        this.Visibility = System.Windows.Visibility.Collapsed;
    }