Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3788080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:56:30+00:00 2026-05-19T11:56:30+00:00

I am building a WP7 client app that talk to a web service (SOAP

  • 0

I am building a WP7 client app that talk to a web service (SOAP like) using Mvvm-Light.

I have a ViewModel that both implements INotifyPropertyChanged and calls RaisePropertryChanged with the broadcast flag set.

Both my view (XAML) and my model (which does HTTP requests to the web service) subscribe to property changes. XAML, obviously, because of INotifyPropertyChanged, and my model by calling

Messenger.Default.Register<SysObjectCreatedMessage>(this, (action) => SysObjectCreatedHandler(action.SysObject));

This pattern is not going to work, i’m afraid, because of the following:

When I get data back from the web service I set properties on my ViewModel (using DispatcherHelper.CheckBeginInvokeUI). I actually use Reflection and my call looks like this:

GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() => pinfo.SetValue(this, e.Response, null));

Here’s the problem: The resultant property set caused by that call to SetValue causes my property set to call RaisePropertryChanged causing me to send the data I just got from the server right back to it.

EDIT – Adding more context Per Jon’s suggestion

Here’s some of my XAML. My GarageDoorOpener class has a GarageDoorOpened property

On the home control server there are a bunch of garage door objects that have a boolean property representing whether they are open or not. I can access these using HTTP POSTs of the form:

http://server/sys/Home/Upstairs/Garage/West Garage Door?f??GarageDoorOpened

The the resultant HTTP body will contain either True or False.

The same model applies to other objects in the home with other types (Strings, Integers, etc…).

For now I’m just focusing on the garage door openers.

The View Model for the garage door looks like this:

public class GarageDoorSensor : SysObject
{
    public static new string SysType = "Garage Door Sensor";
    public const string GarageDoorOpenedPropertyName = "GarageDoorOpened";
    public Boolean _GarageDoorOpened = false;
    [SysProperty]
    public Boolean GarageDoorOpened
    {
        get
        {
            return _GarageDoorOpened;
        }

        set
        {
            if (_GarageDoorOpened == value)
            {
                return;
            }

            var oldValue = _GarageDoorOpened;
            _GarageDoorOpened = value;

            // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
            RaisePropertyChanged(GarageDoorOpenedPropertyName, oldValue, value, true);
        }
    }
}

The SysObject class this inherits from looks like this (simplified):

public class SysObject : ViewModelBase
{
    public static string SysType = "Object";
    public SysObject()
    {
        Messenger.Default.Send<SysObjectCreatedMessage>(new SysObjectCreatedMessage(this));
        }
    }

    protected override void RaisePropertyChanged<T>(string propertyName, T oldValue, T newValue, bool broadcast)
    {
        // When we are initilizing, do not send updates to the server
        // if (UpdateServerWithChange == true)

        // ****************************
        // ****************************
        // 
        // HERE IS THE PROBLEM
        // 
        // This gets called whenever a property changes (called from set())
        // It both notifies the "server" AND the view
        //
        // I need a pattern for getting the "SendPropertyChangeToServer" out
        // of here so it is only called when properties change based on 
        // UI input.
        // 
        // ****************************
        // ****************************
        SendPropertyChangeToServer(propertyName, newValue.ToString());

        // Check if we are on the UI thread or not
        if (App.Current.RootVisual == null || App.Current.RootVisual.CheckAccess())
        {
            base.RaisePropertyChanged(propertyName, oldValue, newValue, broadcast);
        }
        else
        {
            // Invoke on the UI thread
            // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
            GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() =>
                base.RaisePropertyChanged(propertyName, oldValue, newValue, broadcast));
        }
    }

    private void SendPropertyChangeToServer(String PropertyName, String Value)
    {
          Messenger.Default.Send<SysObjectPropertyChangeMessage>(new SysObjectPropertyChangeMessage(this, PropertyName, Value));
    }

    // Called from PremiseServer when a result has been returned from the server.
    // Uses reflection to set the appropriate property's value 
    public void PropertySetCompleteHandler(HttpResponseCompleteEventArgs e)
    {
        // BUGBUG: this is wonky. there is no guarantee these operations will modal. In fact, they likely
        // will be async because we are calling CheckBeginInvokeUI below to wait on the UI thread.

        Type type = this.GetType();
        PropertyInfo pinfo = type.GetProperty((String)e.context);

        // TODO: Genericize this to parse not string property types
        //
        if (pinfo.PropertyType.Name == "Boolean")
        {
            GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() => pinfo.SetValue(this, Boolean.Parse(e.Response), null));
            //pinfo.SetValue(this, Boolean.Parse(e.Response), null);
        }
        else
        {
            GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() => pinfo.SetValue(this, e.Response, null));
            //pinfo.SetValue(this, e.Response, null);
        }
    }
}

My “model” is called PremiseServer. It wraps Http POSTs and handles causing the server to be “queried” for the latest data every once and a while. I plan on eventually implementing notifications, but for now i am polling. It uses a bit of Reflection to dynamically translate the HTTP results into property sets. At it’s core it looks like this (i’m pretty proud of myself for this, although I probably should be ashamed instead).

    protected virtual void OnRequery()
    {
        Debug.WriteLine("OnRequery");
        Type type;

        foreach (SysObject so in sysObjects)
        {
            type = so.GetType();
            PropertyInfo[] pinfos = type.GetProperties();

            foreach (PropertyInfo p in pinfos)
            {
                if (p.IsDefined(typeof(SysProperty),true))
                    SendGetProperty(so.Location, p.Name, so, so.PropertySetCompleteHandler);
            }

        }
    }

    protected delegate void CompletionMethod(HttpResponseCompleteEventArgs e);
    protected void SendGetProperty(string location, string property, SysObject sysobject, CompletionMethod cm)
    {
        String url = GetSysUrl(location.Remove(0, 5));
        Uri uri = new Uri(url + "?f??" + property);
        HttpHelper helper = new HttpHelper(uri, "POST", null, true, property);
        Debug.WriteLine("SendGetProperty: url = <" + uri.ToString() + ">");
        helper.ResponseComplete += new HttpResponseCompleteEventHandler(cm);
        helper.Execute();
    }

Note that OnRequery is not the only place I’ll eventually be calling SendGetProperty from; it’s just there for initialization scaffolding for now. The idea is that I can have a generic piece of code that gets a “message from the server” and translates it into a SysObject.Property.SetValue() call…

END EDIT

I need a pattern that will let me both bind to my data on the XAML side but also on my model side in a thread safe way.

Suggestions?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:56:31+00:00Added an answer on May 19, 2026 at 11:56 am

    I re-engaged on this project over the last few weeks and finally came up with a solution. Given the comments and thoughts people posted above, I’m not sure anyone but me understands what I’m trying to do, but I figured it might be worthwhile posting how I solved this. At the minimum, writing it up will ensure I understand it :-).

    Summarizing the question again:

    I have a home control server that exposes objects in my home via a SOAP interface. Home.LivingRoom.Fireplace, for example is exposed as:

    http://server/Home/LivingRoom/Fireplace?property=DisplayName
    http://server/Home/LivingRoom/Fireplace?property=PowerState

    Doing an HTTP GET against these will result in the HTTP reply containing the property value (e.g. “Living Room Fireplace” and “Off” respectively).

    A garage door (e.g. Home.Garage.EastGarageDoor) is exposed as:

    http://server/Home/Upstairs/EastGarageDoor?property=DisplayName
    http://server/Home/Upstairs/EastGarageDoor?property=GarageDoorOpened
    http://server/Home/Upstairs/EastGarageDoor?property=Trigger

    Here we have a property that if set causes an action (Trigger). Doing a POST against this with a HTTP body of “True” will cause the door to open/close.

    I am building a WP7 app as a front end to this. I have decided to follow the Mvvm model and am using Mvvm-Light.

    WP7 does not have a built in way of supporting notifications from REST interfaces, and I am not yet ready to build my own (although it is on my radar). So for the UI to show up-to-date state I need to poll. The # of entities & amount of data is relatively small and I have now proven that I can make it work well with polling, but there are some optimizations I can do to improve it (including adding smarts to the server to enable a notifcation like system).

    In my solution I’ve blurred the lines between my model & my viewmodel. If you really wanted to be pendantic about it my “Model” is just the low level classes I have for wrapping my Http requests (e.g GetPropertyAsync(objectLocation, propertyName, completionMethod)).

    What I ended up doing is defining a generic class for properties. It looks like this:

    namespace Premise.Model
    {
        //where T : string, bool, int, float 
        public class PremiseProperty<T>  
        {
            private T _Value;
            public PremiseProperty(String propertyName)
            {
                PropertyName = propertyName;
                UpdatedFromServer = false;
            }
    
            public T Value
            {
                get { return _Value; }
    
                set { _Value = value; }
            }
            public String PropertyName { get; set; }
            public bool UpdatedFromServer { get; set; }
        }
    }
    

    I then created a ViewModelBase (from Mvvm-Light) derived base class PremiseObject which represents the base class each object in the control system is based on (e.g. which is literally called `Object’).

    The most important method on PremiseObject is it’s override of RaisePropertyChanged:

        /// </summary>
        protected override void RaisePropertyChanged<T>(string propertyName, T oldValue, T newValue, bool sendToServer)
        {
            if (sendToServer)
                SendPropertyChangeToServer(propertyName, newValue);
    
            // Check if we are on the UI thread or not
            if (App.Current.RootVisual == null || App.Current.RootVisual.CheckAccess())
            {
                // broadcast == false because I don't know why it would ever be true
                base.RaisePropertyChanged(propertyName, oldValue, newValue, false);
            }
            else
            {
                // Invoke on the UI thread
                // Update bindings 
                // broadcast == false because I don't know why it would ever be true
                GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    base.RaisePropertyChanged(propertyName, oldValue, newValue, false));
            }
    
        }
    

    Note a few things:
    1) I am over-riding/re-purposing the broadcast parameter. If it is True then a the property change is “sent to the server” (I do an HTTP POST). I don’t use broadcast property changes anywhere else (and I’m not actually even sure what I would use it for).
    2) I always pass broadcast to False when calling base..

    PremiseObject has a set of standard PremiseProperty properties on it: Location (the URL to the object), Name, DisplayName, Value (the value property). DisplayName looks like this:

        protected PremiseProperty<String> _DisplayName = new PremiseProperty<String>("DisplayName");
    
        public string DisplayName
        {
            get
            {
                return _DisplayName.Value;
            }
    
            set
            {
                if (_DisplayName.Value == value)
                {
                    return;
                }
    
                var oldValue = _DisplayName;
                _DisplayName.Value = value;
    
                // Update bindings and sendToServer change using GalaSoft.MvvmLight.Messenging
                RaisePropertyChanged(_DisplayName.PropertyName, 
                       oldValue, _DisplayName, _DisplayName.UpdatedFromServer);
            }
        }
    

    So this means anytime .DisplayName changes within my program it gets relayed to all UI and IF AND ONLY IF _DisplayName.UpdatedFromServer is True does it also get sent back to the server.

    So how does .UpdatedFromServer get set? When we get our callback from an async Http request:

        protected void DisplayName_Get(PremiseServer server)
        {
            String propertyName = _DisplayName.PropertyName;
    
            _DisplayName.UpdatedFromServer = false;
            server.GetPropertyAsync(Location, propertyName, (HttpResponseArgs) =>
            {
                if (HttpResponseArgs.Succeeded)
                {
                    //Debug.WriteLine("Received {0}: {1} = {2}", DisplayName, propertyName, HttpResponseArgs.Response);
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        DisplayName = (String)HttpResponseArgs.Response; // <-- this is the whole cause of this confusing architecture
                        _DisplayName.UpdatedFromServer = true;
                        HasRealData = true;
                    });
                }
            });
        }
    

    Whenever the UI wants fresh data these XXX_Get functions get called (e.g. on a polling timer, when a view changes, app start, etc…)

    I have to duplicate the code above for each property I define which is pretty painful, but I have not yet found a way to genericize it (trust me, I’ve tried, but my knowledge of C# is just not strong enough and I just keep moving the problem). But this works, and works well.

    To cover all bases, here’s an example of the Trigger property on the GarageDoor class:

        protected PremiseProperty<bool> _Trigger = new PremiseProperty<bool>("Trigger");
        public bool Trigger
        {
            set
            {
                if (value == true)
                    RaisePropertyChanged(_Trigger.PropertyName, false, value, true);
            }
        }
    

    Note how I force the broadcast parameter to RaisePropertyChanged to true and how this is a “Write only” property? This generates an HTTP POST against the ‘GarageDoor.Location’ URL + ?propertyName= + value.ToString().

    I’m pretty happy with this turned out. It’s a bit of a hack, but I’ve now implemented several sophisticated views and it works well. The separation I’ve created will allow me to change the underling protocol (e.g. batching up requests and having the server only send changed data) and my ViewModels will not have to change.

    Thoughts, comments, suggestions?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When building an application and you are using a table that has a primary
i building a drawing app that uses openGl. i am new to openGL but
Building a django app with some mostly static pages at the front of the
Building an application, before using a real database, just to get things work I
i am starting to learn MVVM, and i decided to choose Caliburn Micro to
Building a string for post request in the following way, var itemsToAdd = sl.SelProds.ToList();
Im building my own forum where you can use diffrent types of BBcode and
Edit: Building off of aL3xa's answer below, I've modified his syntax below. Not perfect,
I' building an hobby project in WPF also decided to use Metro style theme
When building a system which needs to respond very consistently and fast, is having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.