简体   繁体   中英

How to Call Server when Checkmark is hit using MVVM + WP7 + LongListSelector

I am using LongListSelector for Windows Phone and ran into a problem. I have in this list a checkbox for each row.

If a user checks that checkbox I want to send to the server it was checked. If the user unchecks the checkbox again it is sent to the server and stored as unchecked.

When the user loads up the view again all the rows are repopulated from the server and the appropriate checkboxs are checked.

I am not sure how to do this though. I am using MVVM pattern so I have IsChecked property in a model(one for model for each row in the list).

At first I thought I could have in IsChecked property the call to the server but if I would do that then every time the initial loading would happen the server would be called.

/// <summary>
/// The <see cref="IsChecked" /> property's name.
/// </summary>
public const string IsCheckedPropertyName = "IsChecked";

private bool isChecked = false;

/// <summary>
/// Sets and gets the IsChecked property.
/// Changes to that property's value raise the PropertyChanged event. 
/// </summary>
public bool IsChecked
{
    get
    {
        return isChecked;
    }

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

        // if I do this here then it will solve my problem when a user checks a box but when I do initial load I will face the problem
        // that it will call my server for every row for no reason at all.
        CallWebService(value);

        RaisePropertyChanging(() => IsChecked);
        isChecked = value;
        RaisePropertyChanged(() => IsChecked);
    }
}

any suggestions?

You could add a separate method/setter for the setting. This would be used exclusively by the code responsible for setting the initial value from the server.

public bool IsCheckedSetting
{
    set
    {
        if (value == isChecked)
            return;
        isChecked = value;
        RaisePropertyChanged(() => IsChecked);
    }
}

The above approach is ok, but it is kind of a drag to create 2 properties for every setting, if you have a lot of settings. In this case, an alternative approach would be to create a wrapper class around your service proxy. This wrapper should keep track of the values for each property, and only call the remote service if the value has actually been updated. Just for illustration:

public class SettingsWrapper
{
    Dictionary<string, object> settings = new Dictionary<string, object>();
    // TODO populate with initial values from server

    public bool UpdateSetting<T>(string name, T value)
    {
        // only update if the initial value is ready, and if the new value is different
        if (settings.ContainsKey(name) && (T)settings[name] != value)
            CallWebService();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM