c# - WPF/C#5.0 Publish/Subscribe to an array of static fields? -
using c# 5.0, i'm creating publish/subscribe relationship on static field, can access multiple pages. in host window, have
public enum plcstates { good, bad, disabled }; public static class plcsafestates { public static event eventhandler teststates1changed; private static plcstates _teststates1; public static plcstates teststates1 { { return _teststates1; } set { if (value != _teststates1) { _teststates1 = value; if (teststates1changed != null) teststates1changed(null, eventargs.empty); } } } }
and in pages hosted window, have things like:
public fb1() { initializecomponent(); safteyfaults.plcsafestates.teststates1changed += plcsafestates_teststates1changed; } private void plcsafestates_teststates1changed(object sender, eventargs e) { var test2 = safteyfaults.plcsafestates.teststates1; if (test2 == safteyfaults.plcstates.bad) { visualstatemanager.gotostate(btnfb, "plcbad", true); } if (test2 == safteyfaults.plcstates.good) { visualstatemanager.gotostate(btnfb, "plcgood", false); } } private void btnfb_click(object sender, routedeventargs e) { visualstatemanager.gotostate(btnout1, "plcbad",false); if (safteyfaults.plcsafestates.teststates1 == safteyfaults.plcstates.good) safteyfaults.plcsafestates.teststates1=safteyfaults.plcstates.bad; else safteyfaults.plcsafestates.teststates1 = safteyfaults.plcstates.good; }
(right now, don't have business logic wired yet- once working, i'll link actual data).
anyhow, of works create single field can subscribe to, modify, etc. need 20+ of these fields. want make 'teststates1' array, i've not been able work.
if make following edits code shown far, compiles , runs, throws error when try access field (e.g. click on button change it):
//window public static class plcsafestates { public static event eventhandler teststates1changed; private static plcstates[] _teststates1; public static plcstates[] teststates1 { { return _teststates1; } set { if (value != _teststates1) { _teststates1 = value; if (teststates1changed != null) teststates1changed(null, eventargs.empty); } } } } //page public fb1() { initializecomponent(); safteyfaults.plcsafestates.teststates1changed += plcsafestates_teststates1changed; } private void plcsafestates_teststates1changed(object sender, eventargs e) { var test2 = safteyfaults.plcsafestates.teststates1[0]; if (test2 == safteyfaults.plcstates.bad) { visualstatemanager.gotostate(btnfb, "plcbad", true); } if (test2 == safteyfaults.plcstates.good) { visualstatemanager.gotostate(btnfb, "plcgood", false); } } private void btnfb_click(object sender, routedeventargs e) { visualstatemanager.gotostate(btnout1, "plcbad",false); if (safteyfaults.plcsafestates.teststates1[0] == safteyfaults.plcstates.good) safteyfaults.plcsafestates.teststates1[0]=safteyfaults.plcstates.bad; else safteyfaults.plcsafestates.teststates1[0] = safteyfaults.plcstates.good; }
Comments
Post a Comment