我有两个WPF窗口:登录和信息。如何将Login.xaml.cs中的变量“userName”调用到Info.xaml.cs?我想让“userName”中的输入显示在Info窗口的列表视图中。
Login.xaml.cs:
public string userName { get; set; }
public void UNameInput_TextChanged(object sender, TextChangedEventArgs e)
{
userName = UNameInput.Text;
}
Info.xaml:
<ListView x:Name="AInfoLv" FontFamily="Tahoma" FontSize="11" Background="White" BorderBrush="{x:Null}" Height="264"
Width="262" Foreground="Black" Margin="-4,-1,0,0">
<ListView.View>
<GridView>
<GridViewColumn x:Name="LabelColumn" Header="Label" Width="100" DisplayMemberBinding="{Binding Path=Label}"/>
<GridViewColumn x:Name="ValueColumn" Header="Value" Width="130" DisplayMemberBinding="{Binding Path=Value}"/>
</GridView>
</ListView.View>
</ListView>
Info.xaml.cs:
public Info()
{
InitializeComponent();
this.AInfoLv.Items.Add(new { Label = "Login" });
this.AInfoLv.Items.Add(new { Label = " Username" });
}
答案 0 :(得分:1)
通过构造函数。
将构造函数Info()
更改为:
Info(string userName)
在Login.xaml.cs
中创建信息对象时(这里有userName
输入值)将此值传递给Info
构造函数,如下所示:
// userName is your input value
var oInfo = new Info(userName)
在info类的构造函数中,使用传递的值显示在屏幕上:
public Info(string userName)
{
InitializeComponent();
this.AInfoLv.Items.Add(new { Label = "Login" });
this.AInfoLv.Items.Add(new { Label = " Username" });
// userName variable contains value of input
}
更新:
A =登录窗口,B =主窗口C =信息窗口
这是创建A
类对象的B
类:
class A {
public void processB() {
var b_obj = new b(username); // username is enter by user
}
}
这是B
类,用于存储username
传递的A
,并具有创建C
类对象的方法。
class B {
private string usernameFromA;
public B (string tmpUsername) {
usernameFromA = tmpUsername;
}
public void processC() {
var c_obj = new c(usernameFromA);
}
}
此类C
可从B
获取用户名。
class C {
public C(string tmpUsername) {
// Here you have your username in tmpUsername variable
}
}
答案 1 :(得分:0)
public string userName;
public void UNameInput_TextChanged(object sender, TextChangedEventArgs e)
{
userName = UNameInput.Text;
}
private void LoginButton_Click(object sender, RoutedEventArgs e)
{
someWindow window = new someWindow(userName);
page.ShowDialog();
}
someWindow.xaml.cs:
public string UserName;
public someWindow(string name) //get the value from login window into someWindow using constructor
{
InitializeComponent();
UserName = name;
}
答案 2 :(得分:0)
您是一个Singleton类来存储公共数据。
检查我在下面定义的Session
课程。
Session.cs
using System;
namespace Singleton
{
public class Session
{
private static object _InstanceLock = new object();
private static Session _Instance;
public static Session Instance
{
get {
if (_Instance == null)
{
lock(_InstanceLock)
{
if (_Instance == null)
_Instance = new Session();
}
}
return _Instance; }
}
// Private constructor, so that no other class can instantiate Session class
private Session()
{
}
public String UserName { get; set; }
}
}
Login.xaml.cs
public void UNameInput_TextChanged(object sender, TextChangedEventArgs e)
{
Session.Instance.UserName = UNameInput.Text;
}
Info.xaml.cs
您可以通过Session.Instance.UserName
如果你想编写单元测试,那么让你的Session类实现一个接口。
答案 3 :(得分:0)
如果您只打算在一个地方使用userName
,那么最简单的方法是在下面声明static readonly property
。
<强> Login.xaml.cs: 强>
public static string userName { get; private set; }
public void UNameInput_TextChanged(object sender, TextChangedEventArgs e)
{
userName = UNameInput.Text;
}
<强> Info.xaml.cs: 强>
public Info()
{
InitializeComponent();
this.AInfoLv.Items.Add(new { Label = "Login" });
this.AInfoLv.Items.Add(new { Label = " Username" });
// use it here
var userName = Login.userName;
}