【发布时间】:2014-11-24 23:05:51
【问题描述】:
我有一个 winforms 应用程序,我在其中以编程方式创建用户控件并将值传递给它。当我运行程序时,用户控件中的所有变量都为空。我不知道我做错了什么。当我查找类似的程序时,看起来我有相同的代码,但它不起作用。也许这里有人可以提供帮助。
这里是主要的表单代码:
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();
}
}
}
【问题讨论】:
-
您在何处/何时看到这些空值
-
你的意思是传递给 jpegSource1 的值是空的吗?您在用户控件的构造函数中填充它们并且之前没有初始化它们。因此,它们将具有字符串的默认值(空)和类的空值。
-
@BradleyDotNET 当我运行调试并检查用户控件中的“IPadd”时,它们为空。
-
但是在什么点。如果您签入构造函数,则分配尚未发生。如果你事后检查,这似乎是一个严重的问题。
标签: c# winforms user-controls