一箫一剑平生意,负尽狂名十五年。这篇文章主要讲述c#开发移动APP-Xamarin入门相关的知识,希望能为你提供帮助。
原文:c#开发移动APP-Xamarin入门如果您在.net环境下做开发,并且对WPF技术有一定了解及应用,同时也想进入移动App开发领域,推荐使用Xamarin开发移动应用
关于Xamarin不做介绍,网上很多,这里主要涉及的是一些来自微软官网文章的翻译,希望能够帮助有需要的小伙伴,有疑问或文章有错误处,还请及时联系
最终效果
文章图片
创建Phoneword 应用程序步骤如下:
文章图片
文章图片
在新窗口中点击 Cross-Platform,选中Mobile App (Xamarin.Forms) ,指定名称及路径
文章图片
在 New Cross Platform App对话框中点击Blank App,在Code Sharing Strategy选中.NET Standard
文章图片
打开MainPage.xaml可看到如下代码
文章图片
用下述替换上述代码
< ?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="https://www.songbingjia.com/android/20, 40, 20, 20" /> < On Platform="android, UWP" Value="https://www.songbingjia.com/android/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方法会在对应按钮点击时执行
解决方案邮件添加-》新项
文章图片
选择新建类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选项卡
文章图片
在Required permissions部分使能CALL_PHONE权限
文章图片
在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
文章图片
对话框中选中Universal Windows > Extensions > Windows Mobile Extensions for UWP
在Phoneword.UWP下双击Package.appxmanifest
文章图片
在 Capabilities 页下使能 Phone Call 能力
文章图片
以上完成后:Build > Build Solution
右键 Phoneword.UWP选中Set as StartUp Project:作为启动项目
然后运行项目
【c#开发移动APP-Xamarin入门】上面是对Xamarin开发应用程序的基本了解,下节深入剖析该程序
推荐阅读
- FunctorApplicative 和 Monad x
- Android studio 不能创建Activity等文件
- adb.Android手机可以通过adb执行按键操作
- 第七篇-列表式App(ListActivity及ListView)
- 第三篇-以LinearLayout进行Android界面设计
- Android------------UI的控件学习内容
- TensorFlow教程入门介绍详细解释
- Unity UI文字介绍和用法示例
- Unity-材质和着色器详细介绍