question

LudoTxx-9485 avatar image
0 Votes"
LudoTxx-9485 asked LudoTxx-9485 answered

WPF databinding from other DLL project.

Hello,

I'm coming here because i need your help.

So here the structure of my program :

  • Read joystick with DirectX on one computer.

  • Affect thoses values to my own object (implement INotifyPropertyChanged) on an static classe (objects in this static class are used everywhere on project)

  • Refresh the GUI with my object values, using databinding - datacontext (Works great)

  • Send this object via UDP to another computer (who reference my *.dll with my own object, same line 2 of this list) - using JsonSerializer.Serialize.

  • Receive my object on another computer, affect thoses values to my object on a static class again.

  • [TRY] Refresh my GUI on this computer with my received values. [DO NOT WORK]


how i receive values and affect to my object in my static class :

         private static void ReceiveUDP()
         {
             try
             {
                 UdpClient listener = new UdpClient(50002);
                 IPEndPoint groupEP = new IPEndPoint(VariablesGlobales.IpClient, 50002);
    
                 Task.Run(() => 
                 {
                     while(true)
                     {
                         byte[] byteArray = listener.Receive(ref groupEP);
                         VariablesGlobales.objClient = JsonSerializer.Deserialize<DataObjects.PelleDeminage.Client>(byteArray); 
                     }
                 });
             }
             catch(Exception ex)
             {
                 // TODO
             }
         }


How i use it as datacontext :

              this.DataContext = new 
              {
                 client = VariablesGlobales.objClient,
              };

And how i use databinding in xaml (DevExpress component support value databing, same control used in sender computer GUI):

 <dxga:LinearScaleMarker  x:Name="mrk_Godet"  Value="{Binding client.Godet}"  />


My object in my *.dll :

     [Serializable]
     public class Client : INotifyPropertyChanged
     {
    
         public event PropertyChangedEventHandler PropertyChanged;
         private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
    
    
         public int Godet
         { 
             get { return this.GodetValue; } 
             set 
             {
                 if (value != this.GodetValue)
                 {
                     this.GodetValue = value;
                     NotifyPropertyChanged();
                 }
             }
         }
    
 /*rest of code*/


If i put an Debug.WriteLine with my value "client.Godet" the value is good, change verytime i move my joystick but my control are not refreshed.

Did you know how can use this object in my *.dll as datacontext in my receiver computer? I use extacly the same code for my sender and receiver computers, sender work great but receiver did not, it seems my PropertyChanged event are never called.

Thank you in advance.

Ludo.

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

Maybe you must have a unique 'VariablesGlobales.objClient = new DataObjects.PelleDeminage.Client( )' on initialisation stage. The receiver will do this:

 var received_obj = JsonSerializer.Deserialize<DataObjects.PelleDeminage.Client>( byteArray );
 VariablesGlobales.objClient.Godet = received_obj.Godet;

Or do something like this:

 DataContext = received_obj;


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LudoTxx-9485 avatar image
0 Votes"
LudoTxx-9485 answered Viorel-1 commented

Ho well thanks, after your answer I have thinking about the fact my joysticks affect value by value and not the object at once, so in my receiver, if I understand good, my PropertyChanged event was never call, I feel stupid sorry.

My receiver GUI is very laggy, did you know if I can improve this?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe you should simplify the transmission. For example, instead of serialisation and parsing of JSON strings, you can send a byte array with your variables (such as Godet), using something like BinaryWriter with network stream or with MemoryStream, or BitConverter.GetBytes, or Marshal.StructureToPtr, etc. The receiver will use the corresponding reading technique.


0 Votes 0 ·
LudoTxx-9485 avatar image
0 Votes"
LudoTxx-9485 answered

In fact after some test, my object was serialized, sended and deserialized really fast.

I have solve my problem with :


 System.Windows.Application.Current.Dispatcher.Invoke(() => 
 {
     VariablesGlobales.objClient.Godet = obj.Godet;
 },System.Windows.Threading.DispatcherPriority.DataBind);


It work really smooth and nice, is my code good or maybe its really not recomended to call my application dispatcher in this case.

Thanks for all your help.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.