我有一个winforms应用程序,我以编程方式创建用户控件并将值传递给它。当我运行程序时,用户控件中的所有变量都为null。我不知道自己做错了什么。当我查找类似的程序时,看起来我有相同的代码,但它不起作用。也许这里有人可以提供帮助。
以下是主要的表单代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net;
namespace AddPanel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DisplayImage();
}
private void DisplayImage()
{
FileStream fs = new FileStream("ntst.jpg", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
cameraTable = (from x in db.CamTable1s
select x).ToList();
test nt = new test();
nt.Location = new System.Drawing.Point(33, 20);
nt.Name = "test1";
nt.usrID = "username";
nt.IPadd = "ipaddress";
nt.pswd = "password";
nt.ws = fs;
nt.Size = new System.Drawing.Size(408, 266);
this.Controls.Add(nt);
}
}
}
用户控制代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;
using AForge;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;
namespace AddPanel
{
public partial class test : UserControl
{
public string IPadd { get; set; }
public string usrID { get; set; }
public string pswd { get; set; }
public string filename { get; set; }
public FileStream ws { get; set; }
public test()
{
JPEGStream jpegSource1 = new JPEGStream("http://" + IPadd + "/jpg/image.jpg?resolution=320x240");
jpegSource1.Login = usrID;
jpegSource1.Password = pswd;
jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource1_NewFrame);
//source1.VideoSourceError += new VideoSourceErrorEventHandler(source1_VideoSourceError);
jpegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
Player1.VideoSource = jpegSource1;
InitializeComponent();
}
void jpegSource1_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap image = new Bitmap(eventArgs.Frame, 320, 240);
image.Save(ws, System.Drawing.Imaging.ImageFormat.Bmp);
}
void jpegSource1_VideoSourceError(object sender, VideoSourceErrorEventArgs eventArgs)
{
//Error handler
Debug.WriteLine(eventArgs.Description);
Bitmap ErPic = new Bitmap(320, 240);
using (var g = Graphics.FromImage(ErPic))
{
using (var arialFontLarge = new Font("Arial", 15))
{
g.DrawString("Camera Offline", arialFontLarge, Brushes.White, 75, 100);
}
}
ErPic.Save(ws, ImageFormat.Bmp);
}
private void StartBut_Click(object sender, EventArgs e)
{
Player1.VideoSource.Start();
}
private void StopBut_Click(object sender, EventArgs e)
{
Player1.VideoSource.Stop();
ws.Close();
}
}
}
答案 0 :(得分:3)
当您启动UserControl时,您的UserControls构造函数会运行,您在事后设置值。我个人要么创建一个可以在初始设置中传递的构造函数,要么在填充值之后创建一个执行的方法来初始化它。
这样的事情:
public partial class test : UserControl
{
public string IPadd { get; set; }
public string usrID { get; set; }
public string pswd { get; set; }
public string filename { get; set; }
public FileStream ws { get; set; }
public test()
{
InitializeComponent();
}
public test(string Ip, string Id, string Pass, string file, FileStream stream )
{
InitializeComponent();
IPadd = Ip;
usrID = Id;
pswd = Pass;
filename = file;
ws = stream;
JPEGStream jpegSource1 = new JPEGStream("http://" + IPadd + "/jpg/image.jpg?resolution=320x240");
jpegSource1.Login = usrID;
jpegSource1.Password = pswd;
jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource1_NewFrame);
source1.VideoSourceError += new VideoSourceErrorEventHandler(source1_VideoSourceError);
pegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
Player1.VideoSource = jpegSource1;
}
}
答案 1 :(得分:0)
问题在于我对在表单之间使用变量的理解是错误的。
我应该放
public string IPadd { get; set; }
public string usrID { get; set; }
public string pswd { get; set; }
public string filename { get; set; }
public FileStream ws { get; set; }
在Form1中然后在测试中访问它们。像这样: Form1中
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;
namespace AddPanel
{
public partial class Form1 : Form
{
private string iPadd;
public string IPadd
{
get { return iPadd;}
set
{
iPadd = value;
}
}
private string usrID;
public string UsrID
{
get { return usrID; }
set
{
usrID = value;
}
}
private string Pswd;
public string pswd
{
get{return Pswd;}
set
{
Pswd = value;
}
}
private string Filename;
public string filename
{
get { return Filename; }
set
{
Filename = value;
}
}
public Form1()
{
InitializeComponent();
DisplayImage();
}
private void DisplayImage()
{
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
UsrID = "webservice";
IPadd = "10.108.212.100";
pswd = "E4emw@tch!ngU";
WS = fs;
test nt = new test();
nt.Location = new System.Drawing.Point(33, h);
nt.Name = "test1";
nt.Size = new System.Drawing.Size(408, 266);
this.Controls.Add(nt);
}
}
}
然后我可以在测试中访问这些变量:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;
using AForge;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;
namespace AddPanel
{
public partial class test : UserControl
{
public test()
{
Form1 f1 = new Form1();
JPEGStream jpegSource1 = new JPEGStream("http://" + f1.IPadd + "/jpg/image.jpg?resolution=320x240");
jpegSource1.Login = f1.UsrID;
jpegSource1.Password = f1.pswd;
jpegSource1.NewFrame += new NewFrameEventHandler(jpegSource1_NewFrame);
//source1.VideoSourceError += new VideoSourceErrorEventHandler(source1_VideoSourceError);
jpegSource1.VideoSourceError += new VideoSourceErrorEventHandler(jpegSource1_VideoSourceError);
Player1.VideoSource = jpegSource1;
InitializeComponent();
}
void jpegSource1_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap image = new Bitmap(eventArgs.Frame, 320, 240);
image.Save(ws, System.Drawing.Imaging.ImageFormat.Bmp);
}
void jpegSource1_VideoSourceError(object sender, VideoSourceErrorEventArgs eventArgs)
{
//Error handler
Debug.WriteLine(eventArgs.Description);
Bitmap ErPic = new Bitmap(320, 240);
using (var g = Graphics.FromImage(ErPic))
{
using (var arialFontLarge = new Font("Arial", 15))
{
g.DrawString("Camera Offline", arialFontLarge, Brushes.White, 75, 100);
}
}
ErPic.Save(ws, ImageFormat.Bmp);
}
private void StartBut_Click(object sender, EventArgs e)
{
Player1.VideoSource.Start();
}
private void StopBut_Click(object sender, EventArgs e)
{
Player1.VideoSource.Stop();
ws.Close();
}
}
}