C# 多窗口WPF应用程序:发送值和共享数据
我是WPF的新手,目前已经设置了一个1窗口WPF。 但是现在我需要一个新窗口来共享字典clientData; 单击主窗口中列表框中的条目时,我需要将entryId传递给可以访问clientData[entryId]的新窗口 我过去总是制作单窗口应用程序,所以我对这一点还是新手C# 多窗口WPF应用程序:发送值和共享数据,c#,wpf,C#,Wpf,我是WPF的新手,目前已经设置了一个1窗口WPF。 但是现在我需要一个新窗口来共享字典clientData; 单击主窗口中列表框中的条目时,我需要将entryId传递给可以访问clientData[entryId]的新窗口 我过去总是制作单窗口应用程序,所以我对这一点还是新手 如何做到这一点?通常,两个窗口之间有两种简单的通信方式 可能性1: 您可以在两个窗口中创建public和static变量,如下所示: public static int Property1 { get; set;
如何做到这一点?通常,两个窗口之间有两种简单的通信方式 可能性1: 您可以在两个窗口中创建
public
和static
变量,如下所示:
public static int Property1 { get; set; }
public int Property2 { get; set; }
public void ShowThis(int parameter)//Gets called by your MainWindow
{
this.Property2 = parameter;
this.Show();
}
public partial class MainWindow : Window
{
Window1 window1;//your subwindow
public MainWindow()
{
InitializeComponent();
window1 = new Window1();
}
private void buttonShow_Click(object sender, RoutedEventArgs e)//button to show subwindow
{
int[] testData = new int[5] { 1, 3, 5, 7, 9 };
window1.ShowThis(testData);
}
}
可能性2:
您可以创建一个参数化方法来显示子窗口,并将变量返回为public
,如下所示:
public static int Property1 { get; set; }
public int Property2 { get; set; }
public void ShowThis(int parameter)//Gets called by your MainWindow
{
this.Property2 = parameter;
this.Show();
}
public partial class MainWindow : Window
{
Window1 window1;//your subwindow
public MainWindow()
{
InitializeComponent();
window1 = new Window1();
}
private void buttonShow_Click(object sender, RoutedEventArgs e)//button to show subwindow
{
int[] testData = new int[5] { 1, 3, 5, 7, 9 };
window1.ShowThis(testData);
}
}
编辑
根据您的问题:
您的子窗口:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public void ShowThis<T>(IEnumerable<T> data)
{
listBox.Items.Clear();
foreach(var item in data)
{
listBox.Items.Add(item);
}
this.Show();
}
}
通常,两个窗口之间有两种简单的通信方式 可能性1: 您可以在两个窗口中创建
public
和static
变量,如下所示:
public static int Property1 { get; set; }
public int Property2 { get; set; }
public void ShowThis(int parameter)//Gets called by your MainWindow
{
this.Property2 = parameter;
this.Show();
}
public partial class MainWindow : Window
{
Window1 window1;//your subwindow
public MainWindow()
{
InitializeComponent();
window1 = new Window1();
}
private void buttonShow_Click(object sender, RoutedEventArgs e)//button to show subwindow
{
int[] testData = new int[5] { 1, 3, 5, 7, 9 };
window1.ShowThis(testData);
}
}
可能性2:
您可以创建一个参数化方法来显示子窗口,并将变量返回为public
,如下所示:
public static int Property1 { get; set; }
public int Property2 { get; set; }
public void ShowThis(int parameter)//Gets called by your MainWindow
{
this.Property2 = parameter;
this.Show();
}
public partial class MainWindow : Window
{
Window1 window1;//your subwindow
public MainWindow()
{
InitializeComponent();
window1 = new Window1();
}
private void buttonShow_Click(object sender, RoutedEventArgs e)//button to show subwindow
{
int[] testData = new int[5] { 1, 3, 5, 7, 9 };
window1.ShowThis(testData);
}
}
编辑
根据您的问题:
您的子窗口:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public void ShowThis<T>(IEnumerable<T> data)
{
listBox.Items.Clear();
foreach(var item in data)
{
listBox.Items.Add(item);
}
this.Show();
}
}
使两个窗口使用相同的视图模型。视图模型?是否可以创建窗口类的新实例并访问它?:)WPF应用程序通常使用MVVM(模型-视图-模型)设计模式构建。建议您使用类似于编程WPF的书籍。如果您不使用视图模型,则将当前窗口传递给第二个窗口,并让第二个窗口调用第一个窗口上的方法来更新它(或设置属性)。通常,这就是视图模型的用途。使两个窗口使用相同的视图模型。视图模型?是否可以创建窗口类的新实例并访问它?:)WPF应用程序通常使用MVVM(模型-视图-模型)设计模式构建。建议您使用类似于编程WPF的书籍。如果您不使用视图模型,则将当前窗口传递给第二个窗口,并让第二个窗口调用第一个窗口上的方法来更新它(或设置属性)。通常这就是视图模型的用途。但是我需要设置第二个窗口的列表框,而不是第一个窗口?我在主窗口类之后插入了Window1类,并得到错误:严重性代码描述项目文件行错误CS0103当前上下文中不存在名称“InitializeComponent”。您是以何种方式添加Window1的?就在后面代码隐藏文件中主窗口的定义?您需要以以下方式添加窗口:项目-->添加窗口,但我需要设置第二个窗口的列表框而不是第一个窗口?我在主窗口类之后插入了Window1类,并获得错误:严重性代码描述项目文件行错误CS0103名称“InitializeComponent”在当前上下文中不存在。您以何种方式添加了Window1?就在代码隐藏文件中主窗口的定义之后?您需要以以下方式添加窗口:项目-->添加窗口