开发者

How to navigate after callling async web method completed?

开发者 https://www.devze.com 2023-03-02 12:44 出处:网络
Im working on a windows phone application 开发者_高级运维using a webservice. I would like to navigate to an other page after completed a webmethod call.

Im working on a windows phone application 开发者_高级运维using a webservice.

I would like to navigate to an other page after completed a webmethod call. I have no idea about how it can be possible.

Here is a part of my view behing code:

private void Button1Button_Click(object sender, RoutedEventArgs e)
    {
        this._ws.InitializeConnexion("my name");
        this.NavigationService.Navigate(new Uri("/View/profile.xaml", UriKind.Relative));
    }

And here is my view model class:

public sealed class MobileViewModel : INotifyPropertyChanged
{
   private WSClient _ws;
   private T_member _member;
   public T_member Member
   {
       get
       {
           return _member;
       }
       set
       {
           _member = value;
           this.RaisePropertyChanged("Member");
       }
   }
   public MobileViewModel()
   {
        _ws = new WSMobileClient();
        _ws.InitializeConnexionCompleted += new EventHandler<InitializeConnexionCompletedEventArgs>(_ws_InitializeConnexionCompleted);
   }
   public event PropertyChangedEventHandler PropertyChanged;
   private void RaisePropertyChanged(string propertyName)
   {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
   }
   public void InitializeConnexion(string name)
   {
       _ws.InitializeConnexionAsync(name);
   }
   private void _ws_InitializeConnexionCompleted(object sender, InitializeConnexionCompletedEventArgs e)
   {
       if (e.Error == null)
       {
           this.Member = e.Result;
       }
       else
       {
           MessageBox.Show("error.");
       }
   }
}

Does anyone can help me ?

Thanks.


I would pass in a continuation lambda to the method that is triggering the web method call - the continuation is then executed when the call has been successfully completed:

private void Button1Button_Click(object sender, RoutedEventArgs e)
{
   InitializeConnexion("my name", () => 
   {
       this.NavigationService.Navigate(new Uri("/View/profile.xaml", UriKind.Relative));
   });
}

You can store this as Action in the MobileViewModel class.

   Action _webCallCompletedAction;
   public void InitializeConnexion(string name, Action action)
   {
       webCallCompletedAction = action;
       _ws.InitializeConnexionAsync(name);
   }

and finally execute it after your web service all is completed:

private void _ws_InitializeConnexionCompleted(object sender, 
                                              InitializeConnexionCompletedEventArgs e)
   {
       if (e.Error != null)
       {
           this.Member = e.Result;
           webCallCompletedAction(); 
       }
       else
       {
           MessageBox.Show("error.");
       }
   }
}


You certainly can do this. A couple of suggestions:

1) Use an ICommand and bind it to your button not the code behind. This puts that logic in your viewmodel where it belongs. Here's one example of how to do that. And another.

2) Once you have that logic in your viewmodel then you can orchestrate navigiation with your connection status without needing to pass messages back to the view. Something like:

 private void _ws_InitializeConnexionCompleted(object sender, InitializeConnexionCompletedEventArgs e)
 {
        if (e.Error != null)
        {
            this.Member = e.Result;
            this.Navigate("/View/profile.xaml");
        }
        else
        {
            MessageBox.Show("error.");
        }
    }
}

protected void Navigate(string address)
{
    if (string.IsNullOrEmpty(address))
          return;

    Uri uri = new Uri(address, UriKind.Relative);
    Debug.Assert(App.Current.RootVisual is PhoneApplicationFrame);
    ((PhoneApplicationFrame)App.Current.RootVisual).Navigate(uri);            
}
0

精彩评论

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