WPF NavigationService为null

时间:2016-02-29 12:06:11

标签: c# wpf navigationservice

我正在学习WPF(从过程PHP转移)并编写了以下内容以从'MainWindow'导航到'Page1',其中登录凭据没有错误,但局部变量'nav'始终为null (因此显示错误消息):

namespace YM_POS_20160229_0949
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void ClickLoginSubmitButton(object sender, RoutedEventArgs e)
        {

            // Dictionary object is c# equivalent of PHP's 'array["key"] = "value"'
            Dictionary<string, string> errMsg = new Dictionary<string, string>();

            // declare variables
            string varUserName;
            string varUserPass;

            // define the variables whilst trimming the values passed
            varUserName = LoginUsername.Text.Trim();
            varUserPass = LoginPassword.Password.Trim();

            // ensure something has been submitted & perform validation on the values submitted

            // if there are no errors, navigate to the users dashboard (aka Page1)
            NavigationService nav = NavigationService.GetNavigationService(new Page1());

            // check if the nav variable is populated
            if (nav != null)
            { 
                nav.Navigate(nav);
            }
            else
            {
                // display an error message to the user advising them an error has occurred and Page1 is not available
                MessageBox.Show("An error has occured.  unable to proceed to " + nav);
            }
        }
    }
}

非常感谢任何帮助。

由于

Newbie Matt

// UI / XAML

<Window x:Class="YM_POS_20160229_0949.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:YM_POS_20160229_0949"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="123*"/>
                    <ColumnDefinition Width="394*"/>
                </Grid.ColumnDefinitions>
                <TextBox x:Name="LoginUsername" HorizontalAlignment="Left" Height="23" Margin="56.649,55,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Grid.Column="1"/>
                <PasswordBox x:Name="LoginPassword" HorizontalAlignment="Left" Height="23" Margin="56.649,100,0,0" Password="Password" VerticalAlignment="Top" Width="120" Grid.Column="1" />
                <Button x:Name="LoginSubmitButton" Content="Submit" HorizontalAlignment="Left" Margin="81.649,172,0,0" VerticalAlignment="Top" Width="75"  Click="ClickLoginSubmitButton" Grid.Column="1"/>
            </Grid>
        </DockPanel>
</Window>

0 个答案:

没有答案