C#把数据存放在公共类中,结果调不出来
C#在操作WPF时由于窗口数据较多,想法是将所有数据保存在一个类中,然后通过调用类来调用数据,结果出错了!!!,不知道有没有保存进去还是调用的方法出错了,恳请各位指点一二!!!以下是做的一个简单窗口的代码:窗口一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Craneplay
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
IsOkPressed = false;
PlayData = new playdata();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play1Window pw = new play1Window();
Save();
pw.ShowDialog();
}
public playdata PlayData
{
get;
set;
}
//public MainWindow(playdata PlayData)
//{
// InitializeComponent();
// IsOkPressed = false;
// PlayData = new playdata(PlayData);
// load();
//}
public bool IsOkPressed
{
get;
set;
}
private void Save()
{
PlayData.A = Convert.ToDouble(tb1.Text);
PlayData.B = Convert.ToDouble(tb2.Text);
IsOkPressed = true;
}
//private void load()
//{
// tb1.Text = PlayData.A.ToString();
// tb2.Text = PlayData.B.ToString();
//}
}
}
自己写的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Craneplay
{
public class playdata
{
////字段
//public double A;
//public double B;
//属性
public double A
{
get;
set;
}
public double B
{
get;
set;
}
public playdata() { }
public playdata(double a,double b)
{
this.A = a;
this.B = b;
}
public playdata(playdata py)
{
A = py.A;
B = py.B;
}
public double add()
{
double c = A + B;
return c;
}
}
}
另一个窗口调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Craneplay
{
/// <summary>
/// play1Window.xaml 的交互逻辑
/// </summary>
public partial class play1Window : Window
{
public play1Window()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// playdata py = new playdata();
//double c= py.add();
//if (c == 12)
//{
// MessageBox.Show("ok");
//}
//else
//{
// MessageBox.Show("false");
//}
playdata py = new playdata();
double c = py.A;
double d = py.B;
double dd = c + d;
}
}
}