locked
Cyclically Update UI : Toggle Switch

    Question

  • Dear All,

    I am developing a Network Application (Metro App on SurfaceRT) that cyclically exchanges binary values from a remote device.

    The Binary values are usually returned as an array of bytes in a TCP frame in response to a Read_Request Frame sent every 300ms to the remote device.

    Accordingly on the UI I have groups of ToggleSwitches (8 in each group) to Monitor & Modify these binary values.
    Further, when the user taps one if the toggle switch in a group the corresponding binary value should toggle on the remote device and the changes should reflect on all other clients running the same app.

    I have a good hand at network programming and communication part of the application isn't a problem.  The problem that I am facing is to link these byte arrays to UI and stay consistent on the UI.

    The problems I am facing and the question I have :

    1.) How can I programmatically link these binary values to respective ToggleSwithes. At first data binding the UI Element (Toggle Switches) comes to mind. But Is this approach efficient (I mean I have so many-around 128 ToggleSwitches on a Frame)? Also a code snippet to BIND IsOn property of a toggle switch to a bit in an element of a Byte array would be great.

    The Array is diclared as :

    Dim Values As Byte() = GetRemoteValues(IPEndPoint,TimeOut)

    2.) When the user taps a Toggle Switch a frame needs to be sent to the device to update(toggle) the value on the remote device. Now sending the frame to device isn't a problem but the event OnToggled is fired only after the value of the toggleSwitch.IsOn is changed i.e the state of the toggle switch changes just by tapping it, before the frame is sent.

    Basically I want the state of the ToggleSwitch to be updated from actual values read from the remote device  every 300ms and not by just tapping it. And on tapping the ToggleSwitch I want to send a frame without altering its state.

    So, Can I use a different event on ToggleSwitch or modify the interaction properties to achieve the desired result.

    Sorry for a little long post, If any additional information is required feel free to ask.

    Thanks in Advance

    Thursday, February 27, 2014 4:33 PM

Answers

  • I'd make a class 'viewmodel' that the toggles are all just databound to.  Forget the OnCLick/toggle/etc - let the XAML handle it.

    When I have TCP data, I'd set a flag indicating that it wasn't user based, update the data in the 'viewmodel' and reset the flag.

    Now, if a user taps a toggle, it would change the value, and usually a PropertyChange is fired.  I'd use the propertyChange to fire the change across TCP.

    Sort of example in c#

    class ViewModel : INotifyPropertyChanged { public byte Val1 { get { return _val1; } set { If(_val1!=value) { _val1=value; NotifyChange("Val1"); } } } void UpdateFromTCP() { _bTCPUpdate = true; /// set the Val1 ... 128 properties here. _bTCPUpdate = false; } void NotifyChange(string prop) { if(PropertyChanged!=null && !_bTCPUPdate) PropertyChanged(new PropertyChangedEventArgs(prop));
    if(!_bTCPUPdate)
    {
    /// send to TCP
    } } }


    Darin R.

    Thursday, February 27, 2014 7:47 PM

All replies

  • What language and UI layer are you using so we can get you to an appropriate forum?

    Thursday, February 27, 2014 5:05 PM
    Owner
  • Hi Rob

    Sorry about that. I am using XAML & VB.NET

    But I thought posting to Forum Location :Building Windows Store apps with C# or VB was enough.

    Thanks

    Warm Regards

    Thursday, February 27, 2014 6:37 PM
  • I'd make a class 'viewmodel' that the toggles are all just databound to.  Forget the OnCLick/toggle/etc - let the XAML handle it.

    When I have TCP data, I'd set a flag indicating that it wasn't user based, update the data in the 'viewmodel' and reset the flag.

    Now, if a user taps a toggle, it would change the value, and usually a PropertyChange is fired.  I'd use the propertyChange to fire the change across TCP.

    Sort of example in c#

    class ViewModel : INotifyPropertyChanged { public byte Val1 { get { return _val1; } set { If(_val1!=value) { _val1=value; NotifyChange("Val1"); } } } void UpdateFromTCP() { _bTCPUpdate = true; /// set the Val1 ... 128 properties here. _bTCPUpdate = false; } void NotifyChange(string prop) { if(PropertyChanged!=null && !_bTCPUPdate) PropertyChanged(new PropertyChangedEventArgs(prop));
    if(!_bTCPUPdate)
    {
    /// send to TCP
    } } }


    Darin R.

    Thursday, February 27, 2014 7:47 PM