简体   繁体   English

当使用MVVM + WP7 + LongListSelector选中对号时,如何调用服务器

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

I am using LongListSelector for Windows Phone and ran into a problem. 我在Windows Phone上使用LongListSelector,遇到了问题。 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). 我正在使用MVVM模式,所以我在模型中具有IsChecked属性(列表中每一行的模型都具有一个)。

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. 起初,我以为我可以在IsChecked属性中调用服务器,但是如果这样做,那么每次初始加载都将调用服务器。

/// <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. 上面的方法是可以的,但是如果您有很多设置,那么为每个设置创建2个属性是一种拖累。 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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