C#获取设置错误

本文关键字:错误 设置 获取 | 更新日期: 2023-09-27 18:01:00

此表单1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using Managed.Adb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        AndroidDebugBridge mADB;
        String mAdbPath;
        List<Device> devices = AdbHelper.Instance.GetDevices(AndroidDebugBridge.SocketAddress);

        public Form1()
        {
            InitializeComponent();

        }
        private void button1_Click(object sender, EventArgs e )
        {
            //mAdbPath = Environment.GetEnvironmentVariable("PATH");
            mAdbPath = "C:''Users''Nadun''AppData''Local''Android''android-sdk''platform-tools";
            mADB = AndroidDebugBridge.CreateBridge(mAdbPath + "''adb.exe", true);
            mADB.Start();
            var list = mADB.Devices;
            textBox1.Text = "" + list.Count;
            foreach (Device item in list)
            {
                Console.WriteLine("");
                listBox1.Items.Add("" + item.Properties["ro.build.product"].ToString() + "-" + item.SerialNumber.ToString() );
            }
            //Console.WriteLine("" + list.Count);
        }

        private void button2_Click(object sender, EventArgs e)
        {

            string text = listBox1.GetItemText(listBox1.SelectedItem);
            Form2 f2 = new Form2(text);
           // f2.Phone = "scs";
            SetPhone sp = new SetPhone();
            sp.PhoneModel = "Test";
            this.Visible = false;
            f2.ShowDialog();
        }


    }
}

这是Form 2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private string phone;
        public string Phone
        {
            get { return this.phone; }
            set { this.phone = value; }
        }
        public Form2(string a)
        {
            InitializeComponent();
            textBox1.Text = a;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //Form2 f2 = new Form2();
            //f2.phone = "s";
            //textBox1.Text = f2.Phone;
            SetPhone sp = new SetPhone();
            textBox1.Text = sp.PhoneModel;
            Console.WriteLine("sefsef-"+sp.PhoneModel);
        }
    }
}

这是我的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
    class SetPhone
    {
        private string phoneModel;
        public string PhoneModel {
            get { return this.phoneModel; }
            set { this.phoneModel = value; }
        }
    }
}

总是空着回来。我不知道为什么。

我正在尝试从"form1"设置值。

我也为此编写了类。但当我从"form2"中获得值时,它返回空。我不知道为什么

C#获取设置错误

在button_2_click中调用setter的SetPhone类对象是一个局部变量,因此当您尝试使用另一个本地变量在Form2_Load中访问该对象时,它是一个全新的对象,Get返回一个空字符串(默认值(。您应该能够在表单之间共享SetPhone变量,可能使用构造函数,然后它将保留使用setter 设置的值