Xamarin.Forms
Xamarin Plugin
サーチ…
共有プラグイン
メッセージやリンクを共有したり、テキストをクリップボードにコピーしたり、XamarinやWindowsアプリケーションでブラウザを開いたりする簡単な方法。
NuGetで利用可能: https : //www.nuget.org/packages/Plugin.Share/
XAML
<StackLayout Padding="20" Spacing="20">
<Button StyleId="Text" Text="Share Text" Clicked="Button_OnClicked"/>
<Button StyleId="Link" Text="Share Link" Clicked="Button_OnClicked"/>
<Button StyleId="Browser" Text="Open Browser" Clicked="Button_OnClicked"/>
<Label Text=""/>
</StackLayout>
C#
async void Button_OnClicked(object sender, EventArgs e)
{
switch (((Button)sender).StyleId)
{
case "Text":
await CrossShare.Current.Share("Follow @JamesMontemagno on Twitter", "Share");
break;
case "Link":
await CrossShare.Current.ShareLink("http://motzcod.es", "Checkout my blog", "MotzCod.es");
break;
case "Browser":
await CrossShare.Current.OpenBrowser("http://motzcod.es");
break;
}
}
ExternalMaps
外部地図プラグイン外部地図を開いて、特定の地理情報や住所に移動します。 iOS上でナビゲーションオプションを使用して起動するオプション。
NuGetで利用可能:[ https://www.nuget.org/packages/Xam.Plugin.ExternalMaps/] [1 ]
XAML
<StackLayout Spacing="10" Padding="10">
<Button x:Name="navigateAddress" Text="Navigate to Address"/>
<Button x:Name="navigateLatLong" Text="Navigate to Lat|Long"/>
<Label Text=""/>
</StackLayout>
コード
namespace PluginDemo
{
public partial class ExternalMaps : ContentPage
{
public ExternalMaps()
{
InitializeComponent();
navigateLatLong.Clicked += (sender, args) =>
{
CrossExternalMaps.Current.NavigateTo("Space Needle", 47.6204, -122.3491);
};
navigateAddress.Clicked += (sender, args) =>
{
CrossExternalMaps.Current.NavigateTo("Xamarin", "394 pacific ave.", "San Francisco", "CA", "94111", "USA", "USA");
};
}
}
}
ジオロケータープラグイン
Xamarin.iOS、Xamarin.Android、Windows上のジオロケーションに簡単にアクセスできます。
利用可能なナゲット:[ https://www.nuget.org/packages/Xam.Plugin.Geolocator/] [1 ]
XAML
<StackLayout Spacing="10" Padding="10">
<Button x:Name="buttonGetGPS" Text="Get GPS"/>
<Label x:Name="labelGPS"/>
<Button x:Name="buttonTrack" Text="Track Movements"/>
<Label x:Name="labelGPSTrack"/>
<Label Text=""/>
</StackLayout>
コード
namespace PluginDemo
{
public partial class GeolocatorPage : ContentPage
{
public GeolocatorPage()
{
InitializeComponent();
buttonGetGPS.Clicked += async (sender, args) =>
{
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 1000;
labelGPS.Text = "Getting gps";
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
if (position == null)
{
labelGPS.Text = "null gps :(";
return;
}
labelGPS.Text = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
position.Timestamp, position.Latitude, position.Longitude,
position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);
}
catch //(Exception ex)
{
// Xamarin.Insights.Report(ex);
// await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured it in Xamarin Insights! Thanks.", "OK");
}
};
buttonTrack.Clicked += async (object sender, EventArgs e) =>
{
try
{
if (CrossGeolocator.Current.IsListening)
{
await CrossGeolocator.Current.StopListeningAsync();
labelGPSTrack.Text = "Stopped tracking";
buttonTrack.Text = "Stop Tracking";
}
else
{
if (await CrossGeolocator.Current.StartListeningAsync(30000, 0))
{
labelGPSTrack.Text = "Started tracking";
buttonTrack.Text = "Track Movements";
}
}
}
catch //(Exception ex)
{
//Xamarin.Insights.Report(ex);
// await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured it in Xamarin Insights! Thanks.", "OK");
}
};
}
protected override void OnAppearing()
{
base.OnAppearing();
try
{
CrossGeolocator.Current.PositionChanged += CrossGeolocator_Current_PositionChanged;
CrossGeolocator.Current.PositionError += CrossGeolocator_Current_PositionError;
}
catch
{
}
}
void CrossGeolocator_Current_PositionError(object sender, Plugin.Geolocator.Abstractions.PositionErrorEventArgs e)
{
labelGPSTrack.Text = "Location error: " + e.Error.ToString();
}
void CrossGeolocator_Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
{
var position = e.Position;
labelGPSTrack.Text = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
position.Timestamp, position.Latitude, position.Longitude,
position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
try
{
CrossGeolocator.Current.PositionChanged -= CrossGeolocator_Current_PositionChanged;
CrossGeolocator.Current.PositionError -= CrossGeolocator_Current_PositionError;
}
catch
{
}
}
}
}
メディアプラグイン
クロスプラットフォームAPIから写真や動画を取得するか、選択します。
利用可能なナゲット:[ https://www.nuget.org/packages/Xam.Plugin.Media/] [1 ]
XAML
<StackLayout Spacing="10" Padding="10">
<Button x:Name="takePhoto" Text="Take Photo"/>
<Button x:Name="pickPhoto" Text="Pick Photo"/>
<Button x:Name="takeVideo" Text="Take Video"/>
<Button x:Name="pickVideo" Text="Pick Video"/>
<Label Text="Save to Gallery"/>
<Switch x:Name="saveToGallery" IsToggled="false" HorizontalOptions="Center"/>
<Label Text="Image will show here"/>
<Image x:Name="image"/>
<Label Text=""/>
</StackLayout>
コード
namespace PluginDemo
{
public partial class MediaPage : ContentPage
{
public MediaPage()
{
InitializeComponent();
takePhoto.Clicked += async (sender, args) =>
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
try
{
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
SaveToAlbum = saveToGallery.IsToggled
});
if (file == null)
return;
await DisplayAlert("File Location", (saveToGallery.IsToggled ? file.AlbumPath : file.Path), "OK");
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
}
catch //(Exception ex)
{
// Xamarin.Insights.Report(ex);
// await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured it in Xamarin Insights! Thanks.", "OK");
}
};
pickPhoto.Clicked += async (sender, args) =>
{
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
return;
}
try
{
Stream stream = null;
var file = await CrossMedia.Current.PickPhotoAsync().ConfigureAwait(true);
if (file == null)
return;
stream = file.GetStream();
file.Dispose();
image.Source = ImageSource.FromStream(() => stream);
}
catch //(Exception ex)
{
// Xamarin.Insights.Report(ex);
// await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured it in Xamarin Insights! Thanks.", "OK");
}
};
takeVideo.Clicked += async (sender, args) =>
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported)
{
await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
try
{
var file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions
{
Name = "video.mp4",
Directory = "DefaultVideos",
SaveToAlbum = saveToGallery.IsToggled
});
if (file == null)
return;
await DisplayAlert("Video Recorded", "Location: " + (saveToGallery.IsToggled ? file.AlbumPath : file.Path), "OK");
file.Dispose();
}
catch //(Exception ex)
{
// Xamarin.Insights.Report(ex);
// await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured it in Xamarin Insights! Thanks.", "OK");
}
};
pickVideo.Clicked += async (sender, args) =>
{
if (!CrossMedia.Current.IsPickVideoSupported)
{
await DisplayAlert("Videos Not Supported", ":( Permission not granted to videos.", "OK");
return;
}
try
{
var file = await CrossMedia.Current.PickVideoAsync();
if (file == null)
return;
await DisplayAlert("Video Selected", "Location: " + file.Path, "OK");
file.Dispose();
}
catch //(Exception ex)
{
//Xamarin.Insights.Report(ex);
//await DisplayAlert("Uh oh", "Something went wrong, but don't worry we captured it in Xamarin Insights! Thanks.", "OK");
}
};
}
}
}
メッセージングプラグイン
XamarinとWindowsのためのメッセージングプラグインで、電話をかけたり、SMSを送信したり、さまざまなモバイルプラットフォームのデフォルトのメッセージングアプリケーションを使って電子メールを送信したりできます。
利用可能なナゲット:[ https://www.nuget.org/packages/Xam.Plugins.Messaging/] [1 ]
XAML
<StackLayout Spacing="10" Padding="10">
<Entry Placeholder="Phone Number" x:Name="phone"/>
<Button x:Name="buttonSms" Text="Send SMS"/>
<Button x:Name="buttonCall" Text="Call Phone Number"/>
<Entry Placeholder="E-mail Address" x:Name="email"/>
<Button x:Name="buttonEmail" Text="Send E-mail"/>
<Label Text=""/>
</StackLayout>
コード
namespace PluginDemo
{
public partial class MessagingPage : ContentPage
{
public MessagingPage()
{
InitializeComponent();
buttonCall.Clicked += async (sender, e) =>
{
try
{
// Make Phone Call
var phoneCallTask = MessagingPlugin.PhoneDialer;
if (phoneCallTask.CanMakePhoneCall)
phoneCallTask.MakePhoneCall(phone.Text);
else
await DisplayAlert("Error", "This device can't place calls", "OK");
}
catch
{
// await DisplayAlert("Error", "Unable to perform action", "OK");
}
};
buttonSms.Clicked += async (sender, e) =>
{
try
{
var smsTask = MessagingPlugin.SmsMessenger;
if (smsTask.CanSendSms)
smsTask.SendSms(phone.Text, "Hello World");
else
await DisplayAlert("Error", "This device can't send sms", "OK");
}
catch
{
// await DisplayAlert("Error", "Unable to perform action", "OK");
}
};
buttonEmail.Clicked += async (sender, e) =>
{
try
{
var emailTask = MessagingPlugin.EmailMessenger;
if (emailTask.CanSendEmail)
emailTask.SendEmail(email.Text, "Hello there!", "This was sent from the Xamrain Messaging Plugin from shared code!");
else
await DisplayAlert("Error", "This device can't send emails", "OK");
}
catch
{
//await DisplayAlert("Error", "Unable to perform action", "OK");
}
};
}
}
}
パーミッションプラグイン
ユーザーがiOSとAndroidの一般的な権限グループの権限を許可または拒否しているかどうかを確認します。
さらに、単純なクロスプラットフォーム非同期/待機APIを使用してアクセス権を要求することもできます。
利用可能なNuget: https://www.nuget.org/packages/Plugin.Permissions ここにリンクの説明を入力します XAML
XAML
<StackLayout Padding="30" Spacing="10">
<Button Text="Get Location" Clicked="Button_OnClicked"></Button>
<Label x:Name="LabelGeolocation"></Label>
<Button Text="Calendar" StyleId="Calendar" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Camera" StyleId="Camera" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Contacts" StyleId="Contacts" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Microphone" StyleId="Microphone" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Phone" StyleId="Phone" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Photos" StyleId="Photos" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Reminders" StyleId="Reminders" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Sensors" StyleId="Sensors" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Sms" StyleId="Sms" Clicked="ButtonPermission_OnClicked"></Button>
<Button Text="Storage" StyleId="Storage" Clicked="ButtonPermission_OnClicked"></Button>
<Label Text=""/>
</StackLayout>
コード
bool busy;
async void ButtonPermission_OnClicked(object sender, EventArgs e)
{
if (busy)
return;
busy = true;
((Button)sender).IsEnabled = false;
var status = PermissionStatus.Unknown;
switch (((Button)sender).StyleId)
{
case "Calendar":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Calendar);
break;
case "Camera":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
break;
case "Contacts":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Contacts);
break;
case "Microphone":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Microphone);
break;
case "Phone":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Phone);
break;
case "Photos":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Photos);
break;
case "Reminders":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Reminders);
break;
case "Sensors":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Sensors);
break;
case "Sms":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Sms);
break;
case "Storage":
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
break;
}
await DisplayAlert("Results", status.ToString(), "OK");
if (status != PermissionStatus.Granted)
{
switch (((Button)sender).StyleId)
{
case "Calendar":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Calendar))[Permission.Calendar];
break;
case "Camera":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Camera))[Permission.Camera];
break;
case "Contacts":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Contacts))[Permission.Contacts];
break;
case "Microphone":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Microphone))[Permission.Microphone];
break;
case "Phone":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Phone))[Permission.Phone];
break;
case "Photos":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Photos))[Permission.Photos];
break;
case "Reminders":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Reminders))[Permission.Reminders];
break;
case "Sensors":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Sensors))[Permission.Sensors];
break;
case "Sms":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Sms))[Permission.Sms];
break;
case "Storage":
status = (await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage))[Permission.Storage];
break;
}
await DisplayAlert("Results", status.ToString(), "OK");
}
busy = false;
((Button)sender).IsEnabled = true;
}
async void Button_OnClicked(object sender, EventArgs e)
{
if (busy)
return;
busy = true;
((Button)sender).IsEnabled = false;
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
status = results[Permission.Location];
}
if (status == PermissionStatus.Granted)
{
var results = await CrossGeolocator.Current.GetPositionAsync(10000);
LabelGeolocation.Text = "Lat: " + results.Latitude + " Long: " + results.Longitude;
}
else if (status != PermissionStatus.Unknown)
{
await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
}
}
catch (Exception ex)
{
LabelGeolocation.Text = "Error: " + ex;
}
((Button)sender).IsEnabled = true;
busy = false;
}