c#开发移动APP-Xamarin入门1

如果您在.net环境下做开发,并且对WPF技术有一定了解及应用,同时也想进入移动App开发领域,推荐使用Xamarin开发移动应用

关于Xamarin不做介绍,网上很多,这里主要涉及的是一些来自微软官网文章的翻译,希望能够帮助有需要的小伙伴,有疑问或文章有错误处,还请及时联系

创建Phoneword 应用程序步骤如下:

c#开发移动APP-Xamarin入门1

c#开发移动APP-Xamarin入门1

在新窗口中点击 Cross-Platform,选中Mobile App (Xamarin.Forms) ,指定名称及路径

c#开发移动APP-Xamarin入门1

 

在 New Cross Platform App对话框中点击Blank App,在Code Sharing Strategy选中.NET Standard 

c#开发移动APP-Xamarin入门1


打开MainPage.xaml可看到如下代码

c#开发移动APP-Xamarin入门1


用下述替换上述代码

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   x:Class="Phoneword.MainPage">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness">
            <On Platform="iOS" Value="20, 40, 20, 20" />
            <On Platform="Android, UWP" Value="20" />
        </OnPlatform>
    </ContentPage.Padding>
    <StackLayout>
      <Label Text="Enter a Phoneword:" />
      <Entry x:Name="phoneNumberText" Text="1-855-XAMARIN" />
      <Button x:Name="translateButon" Text="Translate" Clicked="OnTranslate" />
      <Button x:Name="callButton" Text="Call" IsEnabled="false" Clicked="OnCall" />
    </StackLayout>
</ContentPage>

保存并关闭文件

在MainPage.xaml.cs文件中用如下代码替换模板代码

using System;
using Xamarin.Forms;
namespace Phoneword
{
    public partial class MainPage : ContentPage
    {
        string translatedNumber;

        public MainPage ()
        {
            InitializeComponent ();
        }

        void OnTranslate (object sender, EventArgs e)
        {
            translatedNumber = PhonewordTranslator.ToNumber (phoneNumberText.Text);
            if (!string.IsNullOrWhiteSpace (translatedNumber)) {
                callButton.IsEnabled = true;
                callButton.Text = "Call " + translatedNumber;
            } else {
                callButton.IsEnabled = false;
                callButton.Text = "Call";
            }
        }

        async void OnCall (object sender, EventArgs e)
        {
            if (await this.DisplayAlert (
                    "Dial a Number",
                    "Would you like to call " + translatedNumber + "?",
                    "Yes",
                    "No")) {
                var dialer = DependencyService.Get<IDialer> ();
                if (dialer != null)
                    dialer.Dial (translatedNumber);
            }
        }
    }
} 

OnTranslate 和 OnCall方法会在对应按钮点击时执行
解决方案邮件添加-》新项
c#开发移动APP-Xamarin入门1

 

选择新建类PhoneTranslator
PhoneTranslator.cs代码如下

using System.Text;
namespace Phoneword
{
    public static class PhonewordTranslator
    {
        public static string ToNumber(string raw)
        {
            if (string.IsNullOrWhiteSpace(raw))
                return null;

            raw = raw.ToUpperInvariant();

            var newNumber = new StringBuilder();
            foreach (var c in raw)
            {
                if (" -0123456789".Contains(c))
                    newNumber.Append(c);
                else
                {
                    var result = TranslateToNumber(c);
                    if (result != null)
                        newNumber.Append(result);
                    // Bad character?
                    else
                        return null;
                }
            }
            return newNumber.ToString();
        }

        static bool Contains(this string keyString, char c)
        {
            return keyString.IndexOf(c) >= 0;
        }

        static readonly string[] digits = {
            "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"
        };

        static int? TranslateToNumber(char c)
        {
            for (int i = 0; i < digits.Length; i++)
            {
                if (digits[i].Contains(c))
                    return 2 + i;
            }
            return null;
        }
    }
} 

 

新建接口IDialer
IDialer.cs中定义一个Dail方法,该方法必须在每一个平台上实现

namespace Phoneword
{
    public interface IDialer
    {
        bool Dial(string number);
    }
}

到这一步,常规代码已经完成,下面会实现指定平台拨号代码作为 DependencyService.

解决方案下右键Phoneword.iOS 项目选择添加新类PhoneDialer.cs,

using Foundation;
using Phoneword.iOS;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(PhoneDialer))]
namespace Phoneword.iOS
{
    public class PhoneDialer : IDialer
    {
        public bool Dial(string number)
        {
            return UIApplication.SharedApplication.OpenUrl (
                new NSUrl ("tel:" + number));
        }
    }
}

 

在Phoneword.Android项目右键添加PhoneDialer.cs

using Android.Content;
using Android.Telephony;
using Phoneword.Droid;
using System.Linq;
using Xamarin.Forms;
using Uri = Android.Net.Uri;

[assembly: Dependency(typeof(PhoneDialer))]
namespace Phoneword.Droid
{
    public class PhoneDialer : IDialer
    {
        public bool Dial(string number)
        {
            var context = MainActivity.Instance;
            if (context == null)
                return false;

            var intent = new Intent (Intent.ActionDial);
            intent.SetData (Uri.Parse ("tel:" + number));

            if (IsIntentAvailable (context, intent)) {
                context.StartActivity (intent);
                return true;
            }

            return false;
        }

        public static bool IsIntentAvailable(Context context, Intent intent)
        {
            var packageManager = context.PackageManager;

            var list = packageManager.QueryIntentServices (intent, 0)
                .Union (packageManager.QueryIntentActivities (intent, 0));

            if (list.Any ())
                return true;

            var manager = TelephonyManager.FromContext (context);
            return manager.PhoneType != PhoneType.None;
        }
    }
}

 

在Phoneword.Android项目双击MainActivity.cs,并用一下代码替换

using Android.App;
using Android.Content.PM;
using Android.OS;

namespace Phoneword.Droid
{
    [Activity(Label = "Phoneword", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        internal static MainActivity Instance { get; private set; }

        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
            Instance = this;
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
}

右键Phoneword.Android双击Properties,选中Android Manifest选项卡

c#开发移动APP-Xamarin入门1

在Required permissions部分使能CALL_PHONE权限

c#开发移动APP-Xamarin入门1

在Phoneword.UWP右键新建类PhoneDialer.cs,

using Phoneword.UWP;
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Calls;
using Windows.UI.Popups;
using Xamarin.Forms;

[assembly: Dependency(typeof(PhoneDialer))]
namespace Phoneword.UWP
{
    public class PhoneDialer : IDialer
    {
        bool dialled = false;

        public bool Dial(string number)
        {
            DialNumber(number);
            return dialled;
        }
        async Task DialNumber(string number)
        {
            var phoneLine = await GetDefaultPhoneLineAsync();
            if (phoneLine != null)
            {
                phoneLine.Dial(number, number);
                dialled = true;
            }
            else
            {
                var dialog = new MessageDialog("No line found to place the call");
                await dialog.ShowAsync();
                dialled = false;
            }
        }
        async Task<PhoneLine> GetDefaultPhoneLineAsync()
        {
            var phoneCallStore = await PhoneCallManager.RequestStoreAsync();
            var lineId = await phoneCallStore.GetDefaultLineAsync();
            return await PhoneLine.FromIdAsync(lineId);
        }
    }
}

 

 Phoneword.UWP项目右键References,添加References

c#开发移动APP-Xamarin入门1

 



对话框中选中Universal Windows > Extensions > Windows Mobile Extensions for UWP
在Phoneword.UWP下双击Package.appxmanifest

c#开发移动APP-Xamarin入门1

 

在 Capabilities 页下使能 Phone Call 能力

 c#开发移动APP-Xamarin入门1

以上完成后:Build > Build Solution
右键 Phoneword.UWP选中Set as StartUp Project:作为启动项目
然后运行项目

上面是对Xamarin开发应用程序的基本了解,下节深入剖析该程序

c#开发移动APP-Xamarin入门1

上一篇:vue-cli 全局组件的引入和 App.vue局部组件的引入方法区别


下一篇:Android websock 应用