Xamarin.Forms
자 마린 플러그인
수색…
플러그인 공유
메시지 또는 링크를 공유하거나, 텍스트를 클립 보드로 복사하거나, 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>
기음#
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;
}
}
외부 맵
외부지도 플러그인 외부지도를 열어 특정 위치 정보 또는 주소로 이동합니다. 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");
};
}
}
}
Geolocator 플러그인
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;
}