当您打开应用程序时,我有一个登录作为我的第一个屏幕。登录后我还有10页。我想要的只是登录所有页面的人的名字,如(欢迎,用户!)。我不知道如何做到这一点
这是我的代码:
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
txtUsername.IsEnabled = true;
txtPassword.IsEnabled = true;
txtMessage.Text = "";
txtMessage.IsEnabled = false;
try
{
SqlConnection oConnection = new SqlConnection(_ConnectionString);
SqlCommand oCommand = new SqlCommand("Select * from register where Username = '" + txtUsername.Text + "' AND Password = '" + txtPassword.Password + "'", oConnection);
if (oConnection.State == ConnectionState.Closed)
oConnection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlDataReader reader = oCommand.ExecuteReader();
string Username = null;
if (reader.HasRows)
{
username = txtUsername.Text;
oConnection.Close();
NavigationService _Nav = NavigationService.GetNavigationService(this);
_Nav.Navigate(new Uri("Menuxaml.xaml", UriKind.RelativeOrAbsolute));
}
else if (txtUsername.Text.Trim().Length == 0)
Errormessage.Text = "Please enter your Username";
else if (txtPassword.Password.Trim().Length == 0)
Errormessage.Text = "Please enter your Password";
else
{
Errormessage.Text = "Invalid Username or Password!";
txtPassword.Password = "";
txtUsername.Text = "";
}
oConnection.Close();
}
}
答案 0 :(得分:2)
如果你需要所有10个页面中的用户名,我建议你在应用程序资源中添加它,并从整个应用程序中访问它。
让我告诉你如何做到这一点。
Application.Current.Resources.Add("UserName",txtUsername.Text);
然后在第二页上使用此资源设置标签。
lbl.Content = Application.Current.Resources["UserName"];
答案 1 :(得分:0)
您可以通过第二页公开的构造函数或公共方法将值传递给第二页。
以下是详细信息:
Page1:
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Page2 p = new Page2(2);
this.NavigationService.Navigate(p);
}
}
Page2:
public partial class Page2 : Page
{
int valueFromPage1;
public Page2()
{
InitializeComponent();
}
public Page2(int val):this()
{
valueFromPage1 = val;
this.Loaded += new RoutedEventHandler(Page2_Loaded);
}
void Page2_Loaded(object sender, RoutedEventArgs e)
{
lbl.Content = "Value passed from page1 is: " + valueFromPage1;
}
}
如果您还有其他任何问题,请与我们联系。
如果你想使用相同的结构,那么
page.NavigationService.Navigate(new Uri("/Views/Page.xaml?parameter=test", UriKind.Relative));
目标网页:
string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("parameter", out parameter))
{
this.label.Text = parameter;
}