如何在Silverlight中同步Web服务操作?

时间:2014-11-03 01:38:18

标签: web-services silverlight sync

我现在正在开展一个关于使用silverlight和Web服务(asmx页面)控制设备的项目。项目流程如下:

按下silverlight UI上的按钮,它将使用套接字向中间线发送包。然后midwire将接受包并解构它并将另一个包返回到silverlight UI。我使用下面的代码加载按钮状态,这将触发数据库查询:

 private ButtonStateModel _buttonStateModel = new ButtonStateModel() { BtnOneImage = "Skins/images/flag/QU.png", BtnOneVisible = Visibility.Visible, BtnTwoVisible = Visibility.Collapsed };
    public ButtonStateModel ButtonStateModel
    {
        get
        {
                    ButtonStateModel btnState = null;
                    dataService.GetPR(BusEquipmentPermission.Control.RtuID, BusEquipmentPermission.Control.DevID, (result) =>
                    {
                        if (result != null && result.Count > 0)
                        {
                            var flag = result[0].PRFlag;
                            if (flag == 1)
                            {
                                btnState = new ButtonStateModel()
                                {
                                    BtnOneImage = "Skins/images/flag/OP.png",
                                    BtnOneVisible = Visibility.Visible,
                                    BtnTwoImage = "Skins/images/flag/OFF.png",
                                    BtnTwoVisible = Visibility.Visible
                                };
                            }
                            else if (flag == 2)
                            {
                                btnState = new ButtonStateModel()
                                {
                                    BtnOneImage = "Skins/images/flag/OFF.png",
                                    BtnOneVisible = Visibility.Visible,
                                    BtnTwoImage = "Skins/images/flag/OR.png",
                                    BtnTwoVisible = Visibility.Visible
                                }; 
                            }
                        }
                    });
                    return btnState;
        }
        set
        {
            if (value == _buttonStateModel)
            {
                return;
            }
            _buttonStateModel = value;
            RaisePropertyChanged("ButtonStateModel");
        }
    }

现在的问题是,每当我加载silverlight应用程序时,UI上的按钮都无法正确加载其状态。我知道原因是因为GetPR函数来自webservice(asmx),很奇怪我在Silverlight生成的客户端代码中使用AutoResetEvent无法进行同步操作:

  public void GetPR(string rtuID, string devID, Action<List<BusControlPR>> action)
    {
        ServiceSoapClient proxy = new ServiceSoapClient();
        proxy.GetPRAsync(rtuID, devID);
        proxy.GetPRCompleted += (sender, args) =>
        {
            //I cannt do Sync Operation Here by using AutoResetEvent.
            if (action != null)
                action(args.Result.ToList());
        };
    }

我正在使用webservice(asmx页面)而不是WCF ria服务。 以上问题是我遇到的问题,任何人都可以给我一些启示?

1 个答案:

答案 0 :(得分:0)

&#34; GetPR&#34;方法仍然是异步运行的,所以&#34; ButtonStateModel&#34; getter将立即返回null(&#34;已完成&#34;操作将无效)。并且,您不希望在getter中使用任何类型的阻塞,因为这会阻止UI。相反,你应该把&#34; GetPR&#34;在初始化中,并使用设置&#34; ButtonStateModel&#34;财产到适当的价值:

public class TheViewModel
{
    public ButtonStateModel ButtonStateModel
    {
        get
        {
            return _buttonStateModel;
        }
        set
        {
            if (value == _buttonStateModel)
            {
                return;
            }
            _buttonStateModel = value;
            RaisePropertyChanged("ButtonStateModel");
        }
    }

    public TheViewModel()
    {
        Initialize();
    }

    private void Initialize()
    {
        dataService.GetPR(BusEquipmentPermission.Control.RtuID, BusEquipmentPermission.Control.DevID, (result) =>
        {
            ButtonStateModel btnState = null;
            if (result != null && result.Count > 0)
            {
                var flag = result[0].PRFlag;
                if (flag == 1)
                {
                    btnState = new ButtonStateModel()
                    {
                        BtnOneImage = "Skins/images/flag/OP.png",
                        BtnOneVisible = Visibility.Visible,
                        BtnTwoImage = "Skins/images/flag/OFF.png",
                        BtnTwoVisible = Visibility.Visible
                    };
                }
                else if (flag == 2)
                {
                    btnState = new ButtonStateModel()
                    {
                        BtnOneImage = "Skins/images/flag/OFF.png",
                        BtnOneVisible = Visibility.Visible,
                        BtnTwoImage = "Skins/images/flag/OR.png",
                        BtnTwoVisible = Visibility.Visible
                    }; 
                }
            }
            ButtonStateModel = btnState;
        });
    }
}