Skip to content

Instantly share code, notes, and snippets.

@huxaiphaer
Last active April 21, 2019 12:28
Show Gist options
  • Save huxaiphaer/7ee4d4233857a20639b23647071cc722 to your computer and use it in GitHub Desktop.
Save huxaiphaer/7ee4d4233857a20639b23647071cc722 to your computer and use it in GitHub Desktop.
Can't bind
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Naxam.Controls.Mapbox.Forms;assembly=Naxam.Mapbox.Forms"
xmlns:forms="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
xmlns:controls1="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
x:Class="Via.Views.Location">
<ContentView.Content>
<AbsoluteLayout>
<ScrollView VerticalScrollBarVisibility="Never" Padding="15,8,15,8" AbsoluteLayout.LayoutBounds="0,0,1,1"
<StackLayout Orientation="Vertical">
<StackLayout Orientation="Vertical" BackgroundColor="Black">
<Button
Text="Take a Photo or Select from Library"
BackgroundColor="#00314B"
HorizontalOptions="FillAndExpand"
TextColor="White"
Clicked="Button_OnClicked"
CornerRadius="3" />
<controls1:FlowListView FlowColumnCount="3" x:Name="listItems" FlowItemsSource="{Binding Media}"
SeparatorVisibility="None"
HasUnevenRows="false" RowHeight="100" >
<controls1:FlowListView.FlowColumnTemplate>
<DataTemplate >
<Grid>
<forms:CachedImage DownsampleToViewSize="true" HeightRequest="100" Source="{Binding PreviewPath}" Aspect="AspectFill" HorizontalOptions="FillAndExpand">
</forms:CachedImage>
<Image Source="play" IsVisible="false" HorizontalOptions="End" VerticalOptions="End">
<Image.Triggers>
<DataTrigger TargetType="Image" Binding="{Binding Type}" Value="Video">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</Image.Triggers>
</Image>
</Grid>
</DataTemplate>
</controls1:FlowListView.FlowColumnTemplate>
</controls1:FlowListView>
</StackLayout>
</StackLayout>
</StackLayout>
</ScrollView>
</AbsoluteLayout>
</ContentView.Content>
</ContentView>
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using DLToolkit.Forms.Controls;
using Naxam.Controls.Mapbox.Forms;
using Newtonsoft.Json;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Via.Helpers;
using Via.Models;
using Via.Services;
using Via.utils;
using Via.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static Via.Models.Accident;
namespace Via.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Location : ContentView
{
public Location()
{
InitializeComponent();
BindingContext = new MainViewModel(DependencyService.Get<IMultiMediaPickerService>());
}
}
}
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.Content;
using Android.Views;
using Android.Widget;
using Com.Mapbox.Mapboxsdk;
using Com.Mapbox.Mapboxsdk.Annotations;
using Com.Mapbox.Mapboxsdk.Geometry;
using Com.Mapbox.Mapboxsdk.Maps;
using FFImageLoading.Forms.Platform;
using Plugin.InputKit.Platforms.Droid;
using Plugin.Permissions;
using Via.Droid.Services;
using Via.Helpers;
using Via.utils;
using Via.ViewModels;
using Via.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using static Com.Mapbox.Mapboxsdk.Maps.MapboxMap;
using Color = Android.Graphics.Color;
using Object = Java.Lang.Object;
using View = Android.Views.View;
namespace Via.Droid
{
[Activity(Label = "Via", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
CachedImageRenderer.Init(true);
Config.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
new GetMultimedia(MultiMediaPickerService.SharedInstance);
//Initialize mapbox in Android.
Mapbox.GetInstance(this, MapBoxService.AccessToken);
}
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Via.Helpers;
using Via.Models;
using Via.Services;
using Xamarin.Forms;
namespace Via.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
IMultiMediaPickerService _multiMediaPickerService;
public ObservableCollection<MediaFile> Media { get; set; }
public ICommand SelectImagesCommand { get; set; }
public ICommand SelectVideosCommand { get; set; }
public MainViewModel(IMultiMediaPickerService multiMediaPickerService)
{
_multiMediaPickerService = multiMediaPickerService;
SelectImagesCommand = new Command(async (obj) =>
{
var hasPermission = await CheckPermissionsAsync();
if (hasPermission)
{
Media = new ObservableCollection<MediaFile>();
await _multiMediaPickerService.PickPhotosAsync();
}
});
SelectVideosCommand = new Command(async (obj) =>
{
var hasPermission = await CheckPermissionsAsync();
if (hasPermission)
{
Media = new ObservableCollection<MediaFile>();
await _multiMediaPickerService.PickVideosAsync();
}
});
_multiMediaPickerService.OnMediaPicked += (s, a) =>
{
Device.BeginInvokeOnMainThread(() =>
{
Media.Add(a);
});
};
}
async Task<bool> CheckPermissionsAsync()
{
var retVal = false;
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Storage);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.Storage))
{
await App.Current.MainPage.DisplayAlert("Alert","Need Storage permission to access to your photos.","Ok");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Plugin.Permissions.Abstractions.Permission.Storage });
status = results[Plugin.Permissions.Abstractions.Permission.Storage];
}
if (status == PermissionStatus.Granted)
{
retVal = true;
}
else if (status != PermissionStatus.Unknown)
{
await App.Current.MainPage.DisplayAlert("Alert","Permission Denied. Can not continue, try again.","Ok");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
await App.Current.MainPage.DisplayAlert("Alert", "Error. Can not continue, try again.", "Ok");
}
return retVal;
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment