Hi I'm trying to implement a camera capture using XLabs. I've follows the examples but there seems to only be part of the code necessary (unless there's a complete solution I'm missing on Github!?)
I have the camera showing and it seems to populate MyImageSource with something, however it never appears on my screen.
Any help would be greatly appreciated as this is for a commercial app due on Thursday
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"
xmlns:controls="clr-namespace:MPL.StockCheck.MobileUI.Controls;assembly=MPL.StockCheck.MobileUI"
xmlns:mobileUi="clr-namespace:MPL.StockCheck.MobileUI.Helpers;assembly=MPL.StockCheck.MobileUI"
xmlns:mobileUi1="clr-namespace:MPL.StockCheck.MobileUI;assembly=MPL.StockCheck.MobileUI"
x:Class="MPL.StockCheck.MobileUI.Views.FormDetailView" x:Name="Page"
Title="{Binding Title}">
<ContentPage.Resources>
<ResourceDictionary>
<mobileUi1:PageStorage x:Key="PageStorage" Page="{x:Reference Page}"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolbarItems>
<!--<ToolbarItem Name="Sync Data" Order="Primary" Priority="0" Icon="refresh.png" />-->
</ContentPage.ToolbarItems>
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" IsEnabled="{Binding EnableModifications}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Orientation="Vertical" Spacing="5" Padding="5" HorizontalOptions="FillAndExpand">
<Entry Text="" Placeholder="Some Text" HorizontalOptions="FillAndExpand" />
<Picker x:Name="Picker1" HorizontalOptions="FillAndExpand" SelectedIndex="1">
<Picker.Items>
<x:String>1</x:String>
<x:String>2</x:String>
<x:String>3</x:String>
<x:String>4</x:String>
<x:String>5</x:String>
</Picker.Items>
</Picker>
<Switch IsToggled="True" HorizontalOptions="FillAndExpand" />
<Button Text="Take Picture" IsEnabled="True" BackgroundColor="{x:Static mobileUi:Color.XfBlue}" TextColor="White" HorizontalOptions="FillAndExpand" Command="{Binding TakePictureCommand}"/>
<Image Source="{Binding MyImageSource}" VerticalOptions="CenterAndExpand" IsVisible="True" Aspect="AspectFit" HorizontalOptions="FillAndExpand" />
</StackLayout>
<ActivityIndicator Grid.Row="0" Grid.RowSpan="2" IsRunning="{Binding IsBusy}" HorizontalOptions="Center" VerticalOptions="Center" Color="Default" />
<StackLayout Grid.Row="1" Orientation="Vertical" Spacing="5" Padding="5" HorizontalOptions="FillAndExpand">
<Button Text="Complete" IsEnabled="False" BackgroundColor="{x:Static mobileUi:Color.XfBlue}" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
</StackLayout>
</Grid>
</ContentPage>
Model:
private readonly TaskScheduler _scheduler = TaskScheduler.FromCurrentSynchronizationContext();
private IMediaPicker _mediaPicker;
private ImageSource _imageSource;
private Command _takePictureCommand;
private string _status;
public string Status
{
get { return _status; }
private set { _status = value; }
}
///<summary>
///Gets or sets the image source.
///</summary>
///<value>The image source.</value>
public ImageSource MyImageSource
{
get
{
return _imageSource;
}
set
{
_imageSource = value;
}
}
/// <summary>
/// Setups this instance.
/// </summary>
private void Setup()
{
if (_mediaPicker != null)
{
return;
}
var device = Resolver.Resolve<IDevice>();
////RM: hack for working on windows phone?
_mediaPicker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
}
/// <summary>
/// Takes the picture.
/// </summary>
/// <returns>Take Picture Task.</returns>
private async Task<MediaFile> TakePicture()
{
Setup();
MyImageSource = null;
return await _mediaPicker.TakePhotoAsync(new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Rear, PercentQuality = 100, SaveMediaOnCapture = true }).ContinueWith(t =>
{
if (t.IsFaulted)
{
Status = t.Exception.InnerException.ToString();
}
else if (t.IsCanceled)
{
Status = "Canceled";
}
else
{
var mediaFile = t.Result;
MyImageSource = ImageSource.FromStream(() => mediaFile.Source);
return mediaFile;
}
return null;
}, _scheduler);
}
/// <summary>
/// Gets the take picture command.
/// </summary>
/// <value>The take picture command.</value>
public Command TakePictureCommand
{
get
{
return _takePictureCommand ?? (_takePictureCommand = new Command(
async () => await TakePicture(),
() => true));
}
}