调用异步Web方法完成后如何导航?

时间:2011-04-30 16:00:52

标签: c# silverlight wcf windows-phone-7

我正在使用网络服务处理Windows Phone应用程序。

我想在完成webmethod调用后导航到另一个页面。 我不知道它是如何可能的。

以下是我的视图behing代码的一部分:

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

这是我的视图模型类:

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.");
       }
   }
}

有人可以帮助我吗?

感谢。

2 个答案:

答案 0 :(得分:1)

我会将一个continuation lambda传递给触发web方法调用的方法 - 然后在调用成功完成后执行continuation:

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

您可以将此Action存储在MobileViewModel类中。

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

最后在您的Web服务完成后执行它:

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

答案 1 :(得分:0)

你当然可以做到这一点。几点建议:

1)使用ICommand并将其绑定到您的按钮而不是后面的代码。这将逻辑放在您所属的viewmodel中。 Here's one example of how to do thatAnd another

2)在viewmodel中拥有该逻辑后,您可以使用您的连接状态编排导航,而无需将消息传递回视图。类似的东西:

 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);            
}