开发者

problem with RX and web service collection loading wp7

开发者 https://www.devze.com 2023-03-13 17:12 出处:网络
I\'m beginner with C# and wp7 platform and I have some problem with good idea to get request from web service.

I'm beginner with C# and wp7 platform and I have some problem with good idea to get request from web service.

I made webservice in PHP (nusoap - WSDL) and everything is working fine in "normal" using. Now I have ObservableCollection saved in IsolatedStorage with I load when Page is open (List of watched stacks exchange). Then I want to refresh data for every item from web service.

I don't know whether this is a good idea.

Code:

private GPWWebservicePortTypeClient client = new GPWWebservicePortTypeClient();
private ObservableCollection<WebServiceClass.ItemGetValues> StoredStock = 
           开发者_StackOverflow中文版           new ObservableCollection<WebServiceClass.ItemGetValues>();     
public const string _fileName = "listaObserwowanych.xml";
public Page()
{
   InitializeComponent();

   DataContext = App.ViewModel;

   this.Loaded += new RoutedEventHandler(Page_Loaded);

   client.GetLastValueCompleted +=
            new EventHandler<GetLastValueCompletedEventArgs>(client_GetLastValueCompleted);

   foreach (var itemGetValuese in App.ViewModel.Items)
   {
      client.GetLastValueAsync(itemGetValuese.name);
   }

   var o = 
       Observable.FromEvent<GetLastValueCompletedEventArgs(client,"GetLastValueCompleted")
                                                           .Subscribe(setList);
}

void client_GetLastValueCompleted(object sender, GetLastValueCompletedEventArgs e)
{
    if (e.Error != null)
    {
        MessageBox.Show(Convert.ToString(e.Error));
    }
    else
    {
        ObservableCollection<WebServiceClass.ItemGetValues> ListValues = 
          (ObservableCollection<WebServiceClass.ItemGetValues>)                                                                      
           JsonConvert.DeserializeObject(e.Result, 
           typeof(ObservableCollection<WebServiceClass.ItemGetValues>));
        StoredStock.Add(ListValues[0]);
    }
}

private void setList(IEvent<GetLastValueCompletedEventArgs> ex)
{
    List.ItemsSource = StoredStock;
}


void Page_Loaded(object sender, RoutedEventArgs e)
{
    App.ViewModel.LoadData();
    List.ItemsSource = App.ViewModel.Items;
}

Like u see I use RX to call method client_GetLastValueCompleted add store result to auxiliary variable (StoredStock). Then refresh List in setList method, but that method is client_GetLastValueCompleted what is not soo good idea, becouse I need to run that method only when all of runned GetLastValueAsync in foreach is completed. Second problem: becouse of async web service method StoredStock sometime have different order than App.ViewModel.Items .

Any good idea how to do that in right way? Best regards,

Lukas


You're really mixing up a number of ways to call web services and Rx. You really need to decide on a single way and stick to it.

If you're going to use Rx, then you'll have something like this:

public Page()
{
    InitializeComponent();

    DataContext = App.ViewModel;

    this.Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)
{
    App.ViewModel.LoadData();

    var storedStock =
        new ObservableCollection<WebServiceClass.ItemGetValues>();  

    List.ItemsSource = storedStock;

    var values =
        Observable.Using<WebServiceClass.ItemGetValues, GPWWebservicePortTypeClient>
            (() => new GPWWebservicePortTypeClient(), ws =>
            {
                var clientGetLastValue = Observable
                    .FromAsyncPattern<string, GetLastValueResponse>
                    (ws.BeginGetLastValue, ws.EndGetLastValue);

                Func<string, WebServiceClass.ItemGetValues> deserializeFirst = r =>
                    ((List<WebServiceClass.ItemGetValues>)JsonConvert
                    .DeserializeObject(r,
                        typeof(List<WebServiceClass.ItemGetValues>)))
                    .First();

                return
                    from item in App.ViewModel.Items
                    from e in clientGetLastValue(item)
                    select deserializeFirst(e.Result);
            });

    values.Subscribe(storedStock.Add);
}

You'll have to get the right method call names for your web service client, but the code should roughly be right. Let me know how you go.


I corrected the code above. Should have returned the query inside the Using call rather than assign it to values.


I corrected the call to FromAsyncPattern to use the correct method names and return type from the actual web service reference class sent via email.

It should look like this:

Observable.FromAsyncPattern<string, GetLastValueResponse>
    (ws.BeginGetLastValue, ws.EndGetLastValue);


If you're a beginner with C#, try to avoid RX for the time being. It is a cool technology, but if you use it without clear understanding of what is going on, it will bring more problems than solve.

Use a simple event, and when each async item arrives, locate and update the correspondent one in the stored list.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号