Xamarin.Forms;在设备的屏幕上显示我的 SIM 卡的电话号码 [英] Xamarin.Forms; display the phone number of my SIM Card at device on the screen

查看:37
本文介绍了Xamarin.Forms;在设备的屏幕上显示我的 SIM 卡的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕上显示我设备的电话号码.

在 Xamarin.Android 中,代码有效.但我想在 Xamarin.Forms 中使用代码.我已经搜索过,但没有找到任何结果.

Android.Telephony.TelephonyManager tMgr = (Android.Telephony.TelephonyManager)this.GetSystemService(Android.Content.Context.TelephonyService);字符串 mPhoneNumber = tMgr.Line1Number;

-我授予权限:READ_PHONE_STATE

 <Button FontSize="Large" Text="Telefon Numarasını Al" BackgroundColor="Blue" x:Name="btnNumaraAl" Clicked="btnNumaraAl_Clicked"></Button><Label FontSize="Large" BackgroundColor="Red" x:Name="txtPhone" VerticalOptions="Center" Horizo​​ntalOptions="Center"></Label></StackLayout>

当我点击 btnNumaraAl 时,txtPhone.Text 可以是我的设备电话号码.

资源:获取手机号码 Xamarin.Android?https://developer.xamarin.com/api/type/Android.Telephony.TelephonyManager/

解决方案

  1. 定义抽象,即 Xamarin.Forms 项目中的接口.

    命名空间 YourApp{公共接口 IDeviceInfo{字符串 GetPhoneNumber();}}

  2. 然后,需要在各个平台上实现.Android 实现应如下所示.

    使用Android.Telephony;使用 TodoApp;使用 Xamarin.Forms;[程序集:Xamarin.Forms.Dependency(typeof(YourApp.Droid.DeviceInfo))]命名空间 YourApp.Droid{公共类 DeviceInfo:IDeviceInfo{公共字符串 GetPhoneNumber(){var tMgr = (TelephonyManager)Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.TelephonyService);返回 tMgr.Line1Number;}}}

  3. 最后,您可以使用 DependencyService 在 Xamarin.Forms 项目中使用.

     var deviceInfo = Xamarin.Forms.DependencyService.Get();var number = deviceInfo.GetPhoneNumber();

<块引用>

只是提一下,在 iOS 上,由于安全限制,您无法获得所有者的电话号码.你可以查看这个问题Programmatically get own phone number in iOS >

基于此,您可能需要检查您的应用是在 Android 还是 iOS 上运行.

switch(Device.RuntimePlatform){案例安卓"://你可以休息;案例iOS"//你不能休息;}

I want to display the phone number of my device on the screen.

In Xamarin.Android the code is work. But I want to use a code in Xamarin.Forms. I've searched but I did not find any results.

Android.Telephony.TelephonyManager tMgr = (Android.Telephony.TelephonyManager)this.GetSystemService(Android.Content.Context.TelephonyService);
string mPhoneNumber = tMgr.Line1Number;

-I gave permission : READ_PHONE_STATE

   <StackLayout>
        <Button FontSize="Large" Text="Telefon Numarasını Al" BackgroundColor="Blue" x:Name="btnNumaraAl" Clicked="btnNumaraAl_Clicked"></Button>
        <Label FontSize="Large" BackgroundColor="Red" x:Name="txtPhone" VerticalOptions="Center" HorizontalOptions="Center"></Label>
    </StackLayout>

When I click btnNumaraAl, txtPhone.Text can be my device phone number.

Resources : Getting the number of the phone Xamarin.Android? https://developer.xamarin.com/api/type/Android.Telephony.TelephonyManager/

解决方案

  1. Define your abstraction, an interface in your Xamarin.Forms project.

    namespace YourApp
    {
      public interface IDeviceInfo
      {
        string GetPhoneNumber();
      }
    }
    

  2. Then, you need to implement in each platform. Android implementation should look like this.

    using Android.Telephony;
    using TodoApp;
    using Xamarin.Forms;
    [assembly:Xamarin.Forms.Dependency(typeof(YourApp.Droid.DeviceInfo))]
    namespace YourApp.Droid
    {
        public class DeviceInfo: IDeviceInfo
        {
            public string GetPhoneNumber()
            {
                var tMgr = (TelephonyManager)Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.TelephonyService);
                return tMgr.Line1Number;
            }
        }
    }
    

  3. And finally, you can use in your Xamarin.Forms project using the DependencyService.

     var deviceInfo = Xamarin.Forms.DependencyService.Get<TodoApp.IDeviceInfo>();
     var number = deviceInfo.GetPhoneNumber();
    

Just to mention that on iOS you cannot get the owner phone number due to security restrictions. You can review this question Programmatically get own phone number in iOS

Based on that you may need to check if your app is on Android or iOS.

switch(Device.RuntimePlatform){
  case "Android":
     //you can
     break;
  case "iOS"
     //You can't
     break;
}

这篇关于Xamarin.Forms;在设备的屏幕上显示我的 SIM 卡的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆