简体   繁体   中英

WP8 How to manage delegates

I'm new at programming on WP8 and I don't know how to call an asynchronous method and update my UI. Basically this is my example:

public class MyClassGetFinishedEventArgs : EventArgs
{
    public MyClass SomeObject{ get; set; }
    public List<MyClass> SomeList{ get; set; }
}

public static class MyClassManager
{

    private static MyClassService service = new MyClassService();
    public static event EventHandler GetFinished = delegate { };
    static MyClassManager()
    {
        service = new MyClassService();
    }

    public static void Get() 
    {
        var ea = new MyClassGetFinishedEventArgs();

        service.Get(
            delegate {
                ea.SomeList = service.SomeList;
                GetFinished(null, ea);
            }
            );
    }
}

On Android I make the call like this:

protected override void OnCreate(Bundle bundle)
   {
   base.OnCreate(bundle);
   SetContentView(Resource.Layout.frmContent);

   MyClassManager.GetFinished += HandleFinishedGet;
   MyClassManager.Get();
   }

   void HandleFinishedGet(object sender, EventArgs ea)
   {
               var args = ea as GetFinishedEventArgs;
               if (args != null)
               {

                   if (args.Success)
                   {
                           act.RunOnUiThread(() =>
                           {
                     //Do something to update UI

                           }
                           );
                       }
                   }
          }

Is there a way to do the same in Windows Phone?

Have you tried

Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
       //update UI?
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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