| 网站首页 | 业界新闻 | 群组 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 266 人关注过本帖
标题:C#把数据存放在公共类中,结果调不出来
只看楼主 收藏
米粒大小3
Rank: 1
等 级:新手上路
帖 子:38
专家分:7
注 册:2015-10-25
结帖率:66.67%
  问题点数:0  回复次数:2   
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;
            
        }
    }
}

2018-01-30 15:58
米粒大小3
Rank: 1
等 级:新手上路
帖 子:38
专家分:7
注 册:2015-10-25
  得分:0 
附件: 您没有浏览附件的权限,请 登录注册
2018-01-30 15:59
米粒大小3
Rank: 1
等 级:新手上路
帖 子:38
专家分:7
注 册:2015-10-25
  得分:0 
回复 楼主 米粒大小3
完整代码在下面
2018-01-30 15:59







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.091152 second(s), 9 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved