MyVariable
,它在代码隐藏中的一个方法中得到它的值,例如MouseClick
,但是MyVariable
的值用于另一个类中的方法(我使用不同的模式)。当然,在这个课程中MyVariable
是不可见的,但最好做什么?
public partial class MainWindow : Window
{
Hero Hero1 = new Hero();
Notify newNotify = new Notify();
public MainWindow()
{
InitializeComponent();
Collections coll = new Collections();
}
private void weapons_DragLeave(object sender, DragEventArgs e)
{
Hero1.Attack = ((Weapon) listweapons.SelectedItem)._attack;
newNotify.ThrowHero().Attack = Hero1.Attack;
newNotify.Dropped += Show_Message;
newNotify.DroppedEquipment();
TextBox2.Text = newNotify.ThrowHero().Description;//this gets its value from attack
}
public void Show_Message()
{
Console.WriteLine(newNotify.ThrowHero().Description =
"Защита:" + newNotify.ThrowHero().Protection + "атака:" + newNotify.ThrowHero().Attack);
}
}
然后我在另一个类
中有另一种方法public class SavingInWord : IWordSaving
{
public void ExportToWord()
{
var wordApp = new Word.Application();
wordApp.Visible = false;
Word.Document doc = wordApp.Documents.Add();
doc.Select();
wordApp.Selection.TypeText("Description:" + newNotify.ThrowHero().Description); //newNotify.ThrowHero().Description) can't be seen here, but i need to have the value here which i got from weapons_DragLeave method
doc.SaveAs(@"D:...doc");
wordApp.Visible = true;
}
}
在另一个班级:
public class Notify
{
Hero hero1 = new Hero();
public Hero ThrowHero()
{
return hero1;
}
public delegate void MyEventhandler();
public event MyEventhandler Dropped;
public void DroppedEquipment()
{
if (Dropped != null)
Dropped();
}
}
答案 0 :(得分:1)
在你的根最namespace
public static class Globals
{
Notify newNotify = new Notify();
}
然后,您就可以从任何地方访问它。