尝试在 WPF windows 之间共享对象
Attempting to share Object between WPF windows
我在我的 WPF 应用程序的 MainWindow 中实例化了一个 class,然后在单击事件时将该对象传递给第二个 window。但是当我尝试 运行 我传递给 Window_Loaded 方法中的第二个 window 对象的方法时,我得到错误:
The name object
does not exist in the current context.
这是我的主要Window代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Object objectName;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
objectName = new Object();
objectName.Text = "text";
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Settings settingsWindow = new Settings(objectName);
settingsWindow.Show();
}
}
和设置Window:
public partial class Settings : Window
{
public Settings(Object object)
{
InitializeComponent();
}
private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
{
// get text in object
String name = object.Text;
}
}
所以我可以将对象传递给 Window 但我如何在新的 windows Window_Loaded 方法中使用它?
您没有在设置中实例化对象。
使用:
public partial class Settings: Window
{
Object _object;
public Settings(Object object)
{
_object = object
InitializeComponent();
}
private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
{
// get text in object
String name = _object.Text;
}
还有一些题外话提示:
- 使用 MVVM
- 不要直接使用对象,而是使用更具体的 class 类型(创建自己的类型)。
- 将设置重命名为 SettingsWindow,因为很可能您还有一个包含数据的设置 class。
你在一些不同的事情上出错了。首先,您不能使用具有此语法的对象。您只能通过使用反射或将其转换为字典来遍历它。所以实际上为它制作一个 class 会更好:
class MyClass{
public string Text;
}
现在在您的设置中进行更改 window:
MyClass 对象名;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
objectName = new MyClass();
objectName.Text = "text";
}
您最后需要更改的是另一种形式,它看起来像这样:
public partial class Settings: Window
{
MyClass _object;
public Settings(Object object)
{
_object = object
InitializeComponent();
}
private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
{
// get text in object
var name = _object.Text;
}
}
我在我的 WPF 应用程序的 MainWindow 中实例化了一个 class,然后在单击事件时将该对象传递给第二个 window。但是当我尝试 运行 我传递给 Window_Loaded 方法中的第二个 window 对象的方法时,我得到错误:
The name
object
does not exist in the current context.
这是我的主要Window代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Object objectName;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
objectName = new Object();
objectName.Text = "text";
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
Settings settingsWindow = new Settings(objectName);
settingsWindow.Show();
}
}
和设置Window:
public partial class Settings : Window
{
public Settings(Object object)
{
InitializeComponent();
}
private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
{
// get text in object
String name = object.Text;
}
}
所以我可以将对象传递给 Window 但我如何在新的 windows Window_Loaded 方法中使用它?
您没有在设置中实例化对象。
使用:
public partial class Settings: Window
{
Object _object;
public Settings(Object object)
{
_object = object
InitializeComponent();
}
private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
{
// get text in object
String name = _object.Text;
}
还有一些题外话提示:
- 使用 MVVM
- 不要直接使用对象,而是使用更具体的 class 类型(创建自己的类型)。
- 将设置重命名为 SettingsWindow,因为很可能您还有一个包含数据的设置 class。
你在一些不同的事情上出错了。首先,您不能使用具有此语法的对象。您只能通过使用反射或将其转换为字典来遍历它。所以实际上为它制作一个 class 会更好:
class MyClass{
public string Text;
}
现在在您的设置中进行更改 window:
MyClass 对象名;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
objectName = new MyClass();
objectName.Text = "text";
}
您最后需要更改的是另一种形式,它看起来像这样:
public partial class Settings: Window
{
MyClass _object;
public Settings(Object object)
{
_object = object
InitializeComponent();
}
private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
{
// get text in object
var name = _object.Text;
}
}