c#On使用名称在CS中单击CS中的按钮和文本框的方法?

时间:2016-05-02 07:58:19

标签: c# .net wpf xaml

  1. 我正在尝试使用一个小型登录通用Windows应用程序。
  2. 我有LoginPage.xaml和注册按钮。
  3. 在我的LoginPage.xaml.cs文件中,我有signup_click方法在该方法中,我必须通过tha signuppage.xaml。
  4. 这是我的代码我收到错误。
  5. <Page
        x:Class="LoginApp.MainPage1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:LoginApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Margin="12,17,0,28">
                <!--Title-->
                <TextBlock Text="Login Page" Foreground="Black" FontSize="40"/>
    
                <!--UserName-->
                <TextBlock Text="UserID" Foreground="Black" FontSize="30"/>
                <TextBox  BorderBrush="LightGray" Name="UserName" GotFocus="UserName_GotFocus"/>
    
                <!--Password-->
                <TextBlock  Foreground="Black" Text="Password" Margin="9,-7,0,0" FontSize="30"/>
                <PasswordBox BorderBrush="LightGray" Name="PassWord" GotFocus="UserName_GotFocus" />
    
                <!--Login Button-->
                <Button Content="Login" Background="#FF30DABB" Name="Login" Click="Login_Click" Width="338" />
    
                <!--  Registration Button-->
                <Button Content="Registration" Background="#FF30DABB" Name="SignUp" Click="SignUp_Click" Width="338"/>
    
            </StackPanel>
    
        </Grid>
    </Page>

    1. 这是我的LoginPage.xaml。
    2. using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using Windows.UI.Xaml;
      
      namespace LoginApp.Model
      {
          class LoginPage
      
          {
      
      
              public void Login_Click(object sender, RoutedEventArgs e) {
      
      
              }
              public void SiguUp_Click(object sender, RoutedEventArgs e) {
                  NavigationService.Navigate(new Uri("/Views/SignUpPage.xaml", UriKind.Relative));
      
              }
      
      
          }
      }
      
      1. 这是我的LoginPage.xaml.cs。
      2. 错误指向NavigationService“名称在当前上下文中不存在”。
      3. 我是c的新手#不知道这个错误的原因。
      4. 任何人都可以帮我解决纠正此错误的错误。
      5. 我非常感谢他们。
        1. 在我的SignUp.xaml中,我有一个名为TxtUserName的文本框。
        2. 在我的SignUp.xaml.cs中,我正在验证带有任何名称的文本框。
        3. 使用此时,错误消息类似于“当前上下文中不存在名称txtUsernname”。
        4. 以下是我的代码。
        5. <Page
              x:Class="LoginApp.SignUpPage"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:local="using:LoginApp"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              mc:Ignorable="d">
          
              <Grid x:Name="LayoutRoot" Background="White">
                  <Grid Margin="10,10,-5,-10">
                      <Grid.RowDefinitions>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="Auto"/>
          
                      </Grid.RowDefinitions>
                      <TextBlock Text="User Registration :" Grid.Row="0" FontSize="40" Foreground="Black"/>
                      <TextBlock Text="UserName" Grid.Row="1" Foreground="Black" Margin="0,25,0,0"/>
                      <TextBox Name="TxtUserName" BorderBrush="LightGray" Grid.Row="1" Margin="100,0,0,0" GotFocus="Txt_GotFocus"/>
                      <TextBlock Text="Password:" Grid.Row="2" Margin="0,25,0,0" Foreground="Black"/>
                      <PasswordBox Name="TxtPwd" BorderBrush="LightGray" Grid.Row="2" Margin="100,0,0,0" GotFocus="pwd_GotFocus"/>
                      <TextBlock Text="Address:" Grid.Row="3" Margin="0,25,0,0" Foreground="Black"/>
                      <TextBox Name="TxtAddr" BorderBrush="LightGray" Margin="100,0,0,0" GotFocus="Txt_GotFocus"/>
                      <TextBlock Text="Gender:" Grid.Row="4" Margin="0,25,0,0" Foreground="Black"/>
                      <RadioButton Name="GenMale" Background="LightGray" Grid.Row="4" Margin="100,0,0,0" Content="Male" Foreground="Black"/>
                      <RadioButton Name="GenFemale" Background="LightGray" Grid.Row="4" Margin="200,0,0,0" Content="Female" Foreground="Black"/>
                      <TextBlock Text="Phone No:" Grid.Row="5" Margin="0,25,0,0" Foreground="Black"/>
                      <TextBox Name="TxtPhNo" Grid.Row="5" Margin="100,0,0,0" Foreground="LightGray" MaxLength="10" InputScope="Digits" GotFocus="Txt_GotFocus"/>
                      <TextBlock Text="EmailID:" Grid.Row="6" Margin="0,25,0,0" Foreground="Black"/>
                      <TextBox Name="TxtEmail" Grid.Row="6" Margin="100,0,0,0" GotFocus="TxtGotFocus"/>
                      <Button BorderBrush="Transparent" Background="#FF30DABB" Content="Submit" Name="BtnSubmit" Click="Submit_Click" Grid.Row="7" Margin="0,25.667,0,-41.667" Width="345"/>
          
                  </Grid>
          
              </Grid>
          </Page>

          1. 这是SignUppage.xaml。
          2. using System;
            using System.Collections.Generic;
            using System.IO.IsolatedStorage;
            using System.Linq;
            using System.Text;
            using System.Text.RegularExpressions;
            using System.Threading.Tasks;
            using Windows.UI.Xaml;
            
            namespace LoginApp.Model
            {
                class SignUpPage
                {
            
                    IsolatedStorageFile ISOFile = IsolatedStorageFile.GetUserStoreForApplication();
                    public void Submit_Click(object sender, RoutedEventArgs e) {
            
                        if (!Regex.IsMatch(TxtUserName.Text.Trim(), @"^[A-Za-z_][a-zA-Z0-9_\s]*$")) {
                            MessageBox.Show("Invalid UserName");
            
                        }
            
                    }
            
            
            
                }
            }
            
            1. 这是SignUpPage.xaml.cs
            2. 任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

如果我完全理解你的所有代码,你就会分开你的* .xaml(在/ Views中)和你的* .xaml.cs(在/ Model中)。

我建议你将.xaml和.xaml.cs放在Views中,因为* .xaml.cs不是Model而是后面的代码。模型更像是数据库的一个元素(粗略地)。

然后,您的* .xaml.cs应该实现System.Windows.Controls.Page,这是一个例子:

public partial class LoginPage : Page

也不要忘记创建构造函数(但通常如果您使用Visual Studio的文件创建者,它将为您要创建的文件创建骨架):

public LoginPage()
{
    InitializeComponent();
}

最后,如果它仍然不起作用,请确保您正在使用Sebastian Schulz告诉您的正确引用。