如何将值从依赖项服务传递到共享代码

时间:2019-06-10 09:32:32

标签: xamarin.forms

我试图在我的xamarin.forms应用程序上获得最后通话时间。在Android部分,我正在使用依赖项服务。我可以获得通话时间。如何将持续时间传递回共享代码?

我在Android上的实现

class Dialer : ICallerDialer
    {
        public void GetCallLogs()
        {
            string queryFilter = String.Format("{0}={1}", CallLog.Calls.Type, (int)CallType.Outgoing);
            string querySorter = String.Format("{0} desc ", CallLog.Calls.Date);
            ICursor queryData1 = Android.App.Application.Context.ContentResolver.Query(CallLog.Calls.ContentUri, null, queryFilter ,null, querySorter);
            int number = queryData1.GetColumnIndex(CallLog.Calls.Number);
            int duration1 = queryData1.GetColumnIndex(CallLog.Calls.Duration);
            if (queryData1.MoveToFirst() == true)
            {
                String phNumber = queryData1.GetString(number);
                String callDuration = queryData1.GetString(duration1);  

                How to pass this to Shared code back?
            }
           return;
        }
    }

我的界面

public interface ICallerDialer
    {
        void GetCallLogs(); 
    }

单击按钮时的依赖关系调用

  async void btnCall_Clicked(object sender, System.EventArgs e)
        {         
            DependencyService.Get<ICallerDialer>().GetCallLogs();
           //How to get duration here?
        }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

只需将方法的返回类型更改为字符串类型。

class Dialer : ICallerDialer
    {
        public string GetCallLogs()
        {
            string queryFilter = String.Format("{0}={1}", CallLog.Calls.Type, (int)CallType.Outgoing);
            string querySorter = String.Format("{0} desc ", CallLog.Calls.Date);
            ICursor queryData1 = Android.App.Application.Context.ContentResolver.Query(CallLog.Calls.ContentUri, null, queryFilter ,null, querySorter);
            int number = queryData1.GetColumnIndex(CallLog.Calls.Number);
            int duration1 = queryData1.GetColumnIndex(CallLog.Calls.Duration);
            if (queryData1.MoveToFirst() == true)
            {
                String phNumber = queryData1.GetString(number);
                String callDuration = queryData1.GetString(duration1);  

                return callDuration;
            }
           return string.Empty;
        }
    }

界面

public interface ICallerDialer
    {
        string GetCallLogs(); 
    }

单击按钮时的依赖关系调用

async void btnCall_Clicked(object sender, System.EventArgs e)
        {         
           var duration = DependencyService.Get<ICallerDialer>().GetCallLogs();

        }