依赖服务事件

时间:2020-09-10 13:05:54

标签: xamarin.forms

我试图从droid端调用事件处理程序,但是不会被触发。其他所有人都可以工作,但是此事件不会被触发。我不确定我在做什么错。我在MainActivity注册了依赖项服务,并在服务中设置了依赖项。

public partial class MainPage : ContentPage
{
  DocumentResults results;
  IScanService scanService;
  
  public MainPage()
  {
      InitializeComponent();
      results = new DocumentResults();
      BindingContext = new MainPageViewModel();
      System.Diagnostics.Debug.WriteLine("test cw");
      Console.WriteLine("test cw");
         scanService = DependencyService.Get<IScanService>();
     scanService.ResultsParsedEvent += (s, ev) => { ResultsParsed(null, ev); };
  }

  void Button_Clicked(System.Object sender, System.EventArgs e)
  {
    scanService.ScanService();

    if (!String.IsNullOrWhiteSpace(test))
    {
      scanService.Parsing(test);
    }
  }

  private void ResultsParsed(DocumentResults results,EventArgs e)
  {
    Console.WriteLine("update ");
    testLbl.Text = results.Name;
  }
}

我的界面

public interface IScanService
{
  event EventHandler ResultsParsedEvent;

  string ScanService(); 

  void Parsing(string test);

  void resultsParsed(DocumentResults results, EventArgs e);
}

Droid实现

public class RegService : IScanService
{
  public event EventHandler ResultsParsedEvent;
  DocumentResults results;
  public string Test;
  
  public String ScanService()
  {
    Test = "scan";
    return Test;
  }

  public void Parsing(string test)
  {
    Test = "parsing";
    var results= new DocumentResults();
    results.Name = Test;
    Thread thread1 = new Thread(() => resultsParsed(results,null));

    System.Threading.Thread.Sleep(10);
    resultsParsed(results,null);
  }

  public void resultsParsed(DocumentResults results, EventArgs e)
  {
    ResultsParsedEvent?.Invoke(results, e);
  }
}

0 个答案:

没有答案