在同一解决方案中,我需要在winform(c#)项目和Webform项目之间传递动态值。 我已经将一个项目的参考添加到另一个项目。 我可以获取分配给公共静态变量的初始值,但不能获取动态值。
我已经尝试了该站点上的几个相关示例(运行时值,全局变量)。
namespace TestWeb_01 // Source Project
{
public partial class WebForm1 : System.Web.UI.Page
{
public static string Time_Web = "xxxx";
public static GlobalVariablesClass globals = new GlobalVariablesClass();
public void Page_Load(object sender, EventArgs e)
{
Time_Web = DateTime.Now.ToString(@"hh\:mm\:ss");
RuntimeValues.Runtime_String1 = "zzzz";
globals.VariableOne = "www";
Refresh();
}
protected void Button3_Click(object sender, EventArgs e)// Write Time
{
Time_Web = DateTime.Now.ToString(@"hh\:mm\:ss");
RuntimeValues.Runtime_String1 =
DateTime.Now.ToString(@"hh\:mm\:ss");
globals.VariableOne = DateTime.Now.ToString(@"hh\:mm\:ss");
Refresh();
}
...
using TestWeb_01;// Winform project
namespace FormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Time_Web_lbl.Text = "Initial";
Runtime_lbl.Text = "Initial";
Global_lbl.Text = "Initial";
}
public void Form1_Load(object sender, EventArgs e)
{
Refresh();
}
public void button1_Click(object sender, EventArgs e)
{
Refresh();
}
public void Refresh()
{
Time_Web_lbl.Text = WebForm1.Time_Web;
Runtime_lbl.Text = RuntimeValues.Runtime_String1;
Global_lbl.Text = WebForm1.globals.VariableOne;
}
...
我只能在Winform中获得“ xxxx”值,在Runtime和Global变量中没有空格,并且白天没有更新值。
答案 0 :(得分:0)
如果您使用了引用,那么您实质上要传递的是非运行时项目到另一个项目的副本,反之亦然。这意味着两个项目只能访问另一个项目的起始值,而不能在运行时查看它们的内存。
在运行时,不能仅通过引用来获取不同项目中的值,您需要使用系统上的文件或网络(最好通过sockets,{{3})来传递它们}或在某些情况下HTTP request
项目产生WCF.,并且在运行时无法窥视彼此的内存。