Created
February 14, 2021 18:17
-
-
Save dimonovdd/fddef6f5ba20d873a3b6b104318aca34 to your computer and use it in GitHub Desktop.
ImageFromScrollView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Grid RowDefinitions="300,10,50,10,50" RowSpacing="0" Margin="0" Padding="0"> | |
<ScrollView x:Name="scroll" | |
Margin="0" Padding="0"> | |
<StackLayout x:Name="stack" | |
Margin="0" Padding="0" Spacing="0"> | |
<BoxView HeightRequest="200" Background="Violet"/> | |
<BoxView HeightRequest="200" Background="Green"/> | |
<BoxView HeightRequest="200" Background="Blue"/> | |
</StackLayout> | |
</ScrollView> | |
<Button Grid.Row="2" Text="Capture ScrollView" Background="#990000ff" | |
Command="{Binding CaptureCommand}" CommandParameter="{x:Reference scroll}"/> | |
<Button Grid.Row="4" Text="Capture StackLayout" Background="#990000ff" | |
Command="{Binding CaptureCommand}" CommandParameter="{x:Reference stack}"/> | |
</Grid> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainViewModel : BaseNotifier | |
{ | |
public MainViewModel() | |
=> CaptureCommand = new Command<VisualElement>(OnCapture); | |
public ICommand CaptureCommand { get; set; } | |
async void OnCapture(VisualElement element) | |
{ | |
try | |
{ | |
using var stream = await element.CaptureImageAsync(); | |
var dir = FileSystem.CacheDirectory; | |
var filepath = Path.Combine(dir, "image.jpg"); | |
if (File.Exists(filepath)) | |
File.Delete(filepath); | |
using var fileStream = File.OpenWrite(filepath); | |
stream.CopyTo(fileStream); | |
fileStream.Close(); | |
await Share.RequestAsync(new ShareFileRequest(new ShareFile(filepath))); | |
} | |
catch (Exception ex) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment