【问题标题】:Navigating from xamarin android to PCL从 xamarin android 导航到 PCL
【发布时间】:2016-02-23 13:53:46
【问题描述】:

我目前正在使用带有 PCL 的 xamarin 表单来访问相机并扫描条形码并将其显示在 UserDialog 中。我可以使用依赖服务轻松做到这一点。我遇到的问题要回去了。我想通过按 userDialog 上的取消按钮返回 PCL。我正在使用消息中心返回 PCL 主页,消息确实返回,但 UI 保持不变,即相机屏幕保持不变。

下面是我的代码

void HandleScanResult(ZXing.Result result)
{
    if (result != null && !string.IsNullOrEmpty(result.Text))
    {
        CrossVibrate.Current.Vibration(500);
    }

    Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
    {
        resultText.Text = await SaveScannedRecord(result.Text);
        PromptResult promptResult = await UserDialogs.Instance.PromptAsync
        ("Hello Friends","Question","SCAN","CANCEL",resultText.Text,InputType.Name);     
        if (promptResult.Ok)
        {

        }
        else
        {
            //CODE TO GO BACK
            var home = new Home();
            RunOnUiThread(() => { xamarinForm.MessagingCenter.Send<Home>(home, "scannedResult"); });

        }
    });
}

【问题讨论】:

  • 向另一个页面发送消息只是发送消息:您需要在收到消息时执行一些操作以关闭扫描仪页面。你如何做到这一点取决于你首先如何显示它。您还将 RunOnUIThread 嵌套在 BeginInvokeOnMainThread 中,这是不必要的。最后,您应该可以直接从 PCL 调用 ZXIng。有关执行此操作的 Forms 示例应用程序,请参见 github 页面。
  • 如何关闭扫描仪页面。
  • 视情况而定 - 您还没有显示实际显示页面的代码。
  • 我使用依赖服务从 PCL 类(Home 类)调用它。 IBarcodeScanner 扫描器 = DependencyService.Get();扫描仪.Scan(config.ScannerKey, config.ServiceUrl);
  • 在示例中,它似乎会自动关闭扫描视图。我不确定你做错了什么。我会尝试等待 Scan 方法,或使用包的 Forms 版本。

标签: xamarin xamarin.forms


【解决方案1】:

在这种情况下,我真的很喜欢使用async/await 语法:

1) 在类TaskCompletionSource&lt;bool&gt; 变量中的某处定义

2) 当你调用你的方法时,初始化那个变量:

public async Task<bool> Scan()
{
   // init task variable
   tsc = new TaskCompletionSource<bool>();

   // call your method for scan (from ZXing lib)
   StartScan(); 

   // return task from source
   return tsc.Task;
}

3) 处理结果时,为任务设置结果:

void HandleScanResult(ZXing.Result result)
{
   Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
   {
       resultText.Text = await SaveScannedRecord(result.Text);
       PromptResult promptResult = await UserDialogs.Instance.PromptAsync("Hello Friends", "Question", "SCAN", "CANCEL", resultText.Text, InputType.Name);
       if (promptResult.Ok)
       {
         tsc.SetResult(true);
       }
       else
       {
         //CODE TO GO BACK
         tsc.SetResult(false);
       }
    });    
}

现在您可以在 PCL 中编写导航逻辑,如下所示:

var scanResult = await scanService.Scan();
if (!scanResult)
   // your navigation logic goes here
   Navigation.PopToRoot();

【讨论】:

  • 我试过你的代码。但它确实会回到 PCL 页面。
  • @maxspan “代码返回”部分出错。应该是 tsc.SetResult(false)。另外,我真的不明白你想要实现什么。我只是想指出有一种方法可以将导航逻辑移动到 PCL
【解决方案2】:

@Eugene 解决方案很棒,不过您仍然可以使用消息中心:

我相信问题就在这里:

xamarinForm.MessagingCenter.Send<Home>(home, "scannedResult"); });

//Solution:
MessagingCenter.Send<Home> (this, "scannedResult");
//Inside the PCL you will need:
MessagingCenter.Subscribe<Home> (this, "scannedResult", (sender) => {
    // do your thing.
});

【讨论】:

  • 我已经试过了,它不起作用。我也在使用连续扫描。
猜你喜欢
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 2018-11-23
  • 2017-08-22
  • 1970-01-01
相关资源
最近更新 更多