c# - External class handler interacting with UI -
i working on class communicate external device through serialport class, need update ui according messages device send back.
i tried following code not work:
using system.io.ports; namespace learningsteps { /// <summary> /// interaction logic comm.xaml /// </summary> /// public class teste { private serialport _myport; public teste(serialport p) { _myport = p; } public void funcao(serialdatareceivedeventhandler h) { _myport.datareceived += h; byte[] vetor = new byte[] { 0x24, 0x24, 0xcf, 0x00, 0x01, 0x02, 0x25, 0x33, 0x7e }; _myport.write(vetor, 0, 9); } } public partial class comm : window { serialport port; teste t; public delegate void atualizacallback(string message); system.text.asciiencoding encoding = new system.text.asciiencoding(); public comm() { initializecomponent(); port = new serialport("com5",115200,parity.none,8,stopbits.one); port.rtsenable = false; port.dtrenable = false; port.handshake = handshake.none; port.newline = "~"; port.open(); t = new teste(port); } private void recebido(object sender, serialdatareceivedeventargs e) { serialport sp = (serialport)sender; string indata = sp.readexisting(); my_label.dispatcher.invoke(new atualizacallback(this.atualiza),new object[]{indata}); } private void bt_click(object sender, routedeventargs e) { t.funcao(recebido); } private void atualiza(string s) { byte[] vet = encoding.getbytes(s); my_label.content = bitconverter.tostring(vet); } } } i error when try compile, think because i'm trying send internal serialdatareceivedeventhandler argument. don't know how use t.funcao , update ui otherwise.
the error says: "an unhandled exception of type 'system.window.markup.xamlparseexception' occurred in presentationframework.dll
additional information: 'the invocation of constructor type 'learningsteps.comm' matches specified binding constraints threw exception.' line number '3' , line position '9'.
if there handler exception, program may safely continued."
can me please?
Comments
Post a Comment