How to programatically scroll a scrollviewer and retain the animation - windows-phone-7

I have a page whose contents are horizontally wider than the phone, sort of like a Panorama but without all the parallax stuff, and I want to let the user flick to scroll, and also press a button to scroll to specified offsets within the scrollviewer. ScrollToHorizontalOffset works fine for the button, except that it loses the animation, and I want to retain the animation.

I am doing a very similar thing. I have created WizardControl and WizardPage user controls. The user can flick between pages or use next and previous buttons.
I have created a slide effect animation to move between pages. Here is the code:
Storyboard sb = SlideEffect(MainPanel, (-_pageWidth * pagesToMove));
sb.Completed += Slide_Completed;
sb.Begin();
private Storyboard SlideEffect(UIElement controlToAnimate, double positionToMove)
{
//Get position of stackpanel
GeneralTransform gt = controlToAnimate.TransformToVisual(MainGrid);
Point p = gt.Transform(new Point(0, 0));
//add new storyboard and animation
Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation { To = p.X + positionToMove };
Storyboard.SetTarget(da, controlToAnimate);
Storyboard.SetTargetProperty(da, new PropertyPath("(controlToAnimate.RenderTransform).(TransformTranslate.X)"));
ExponentialEase ee = new ExponentialEase { Exponent = 6.0, EasingMode = EasingMode.EaseOut };
da.EasingFunction = ee;
sb.Children.Add(da);
return sb;
}
MainPanel is a horizontal stack panel. Wizard pages are added to it.
_pageWidth matters as this works in both portrait and landscape mode.
pagestoMove is used as I have a button to return directly to the first page.
UPDATE - added Wizard main panel def and add page method
<StackPanel
x:Name="MainPanel"
Grid.Row="1"
Orientation="Horizontal"
RenderTransformOrigin="0.5,0.5"
Margin="0,0,0,10"
Width="480">
<StackPanel.RenderTransform>
<TranslateTransform
X="0" />
</StackPanel.RenderTransform>
</StackPanel>
public void AddPage(WizardPageControl page)
{
MainPanel.Width += _pageWidth;
page.PageWidth = _pageWidth;
double newRight = (MainPanel.Width - _pageWidth) * -1;
MainPanel.Margin = new Thickness(0, 0, newRight, 10);
MainPanel.Children.Add(page);
_wizardPageList.Add(page);
TotalPages++;
}

Related

How to merge two image in Xamarin Forms?

I'm developing a Xamarin Forms iOS app. In the xaml file, there is a grid.
<Grid x:Name="QrCodeSite" HeightRequest="300" Margin="37, 37, 37, 0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
</Grid>
In the related .cs file, I use ZXing.Net.Mobile.Forms to generate a QR code and place it in the grid. And I put my logo in the same grid, which will finally appear at the center of the QR code.
var barcode = new ZXingBarcodeImageView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
barcode.BarcodeOptions.Width = 650;
barcode.BarcodeOptions.Height = 650;
barcode.BarcodeOptions.Margin = 1;
barcode.BarcodeValue = value;
var img = new Image
{
Source = "logo.png",
WidthRequest = 70,
HeightRequest = 70,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
};
QrCodeSite.Children.Clear();
QrCodeSite.Children.Add(barcode);
QrCodeSite.Children.Add(img);
The problem is, maybe my phone (iPhone 6s plus) is too slow, sometimes the logo appears first and after a lag (around 1 second) the QR code is shown. How can I merge the QR code and the logo into one image and then add it to the grid?
You can using SkiaSharp to display Image or merge images . Having a look at how to Display SkiaSharp bitmaps to download sample project to research at it.
Based on Drawing on existing bitmaps reference , you can modify it as follow :
public partial class MonkeyMoustachePage : ContentPage
{
SKBitmap monkeyBitmap;
public MonkeyMoustachePage()
{
Title = "Monkey Moustache";
monkeyBitmap = BitmapExtensions.LoadBitmapResource(GetType(),
"SkiaSharpFormsDemos.Media.MonkeyFace.png");
SKBitmap iconImage = BitmapExtensions.LoadBitmapResource(GetType(),
"SkiaSharpFormsDemos.Media.GooglePlaylogo.png");
int offset = monkeyBitmap.Width / 2 - iconImage.Width / 2;
int offsetTop = monkeyBitmap.Height / 2 - iconImage.Height / 2;
// Create canvas based on bitmap
using (SKCanvas canvas = new SKCanvas(monkeyBitmap))
{
canvas.DrawBitmap(iconImage, SKRect.Create(offset, offsetTop, iconImage.Width, iconImage.Height));
}
// Create SKCanvasView to view result
SKCanvasView canvasView = new SKCanvasView();
canvasView.PaintSurface += OnCanvasViewPaintSurface;
Content = canvasView;
//save the new image
using (MemoryStream memStream = new MemoryStream())
using (SKManagedWStream wstream = new SKManagedWStream(memStream))
{
monkeyBitmap.Encode(wstream, imageFormat, quality);
byte[] data = memStream.ToArray();
// Check the data array for content!
bool success = await DependencyService.Get<IPhotoLibrary>().SavePhotoAsync(data, folder, filename);
// Check return value for success!
}
}
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKImageInfo info = args.Info;
SKSurface surface = args.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear();
canvas.DrawBitmap(monkeyBitmap, info.Rect, BitmapStretch.Uniform);
}
}
Then you will see a logo icon will display in original image :
If you want to save SkiaSharp bitmaps to files , have a look at this :https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/bitmaps/saving#exploring-the-image-formats
Note: BitmapExtensions.cs file is from sample project .By the way , when adding image to project , you need to set Build ACtion of image be Embedded resource .As follow :

xamarin forms zoomable image (Cross Platform)

Is there a way to use pinch to zoom in shared Xamarin Forms, I only found an implementation for each platform.
You can use the Pan Gesture to implement it.
There is a good sample of wrapping an image in PanContainer available here - Adding a Pan Gesture Recognizer.
The pan gesture is used for detecting dragging and is implemented with
the PanGestureRecognizer class. A common scenario for the pan gesture
is to horizontally and vertically drag an image, so that all of the
image content can be viewed when it's being displayed in a viewport
smaller than the image dimensions. This is accomplished by moving the
image within the viewport.
Simple Example :
<Image Source="MonoMonkey.jpg">
<Image.GestureRecognizers>
<PanGestureRecognizer PanUpdated="OnPanUpdated" />
</Image.GestureRecognizers>
</Image>
Pan Container example XAML:
<AbsoluteLayout>
<local:PanContainer>
<Image Source="MonoMonkey.jpg" WidthRequest="1024" HeightRequest="768" />
</local:PanContainer>
</AbsoluteLayout>
Code behind :
public class PanContainer : ContentView
{
double x, y;
public PanContainer ()
{
// Set PanGestureRecognizer.TouchPoints to control the
// number of touch points needed to pan
var panGesture = new PanGestureRecognizer ();
panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add (panGesture);
}
void OnPanUpdated (object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType) {
case GestureStatus.Running:
// Translate and ensure we don't pan beyond the wrapped user interface element bounds.
Content.TranslationX =
Math.Max (Math.Min (0, x + e.TotalX), -Math.Abs (Content.Width - App.ScreenWidth));
Content.TranslationY =
Math.Max (Math.Min (0, y + e.TotalY), -Math.Abs (Content.Height - App.ScreenHeight));
break;
case GestureStatus.Completed:
// Store the translation applied during the pan
x = Content.TranslationX;
y = Content.TranslationY;
break;
}
}
}
You can try this modification I made and verified on .NET Maui to TBertuzzi/Xamarin.Forms.PinchZoomImage to:
Adjust image boundaries for not offsetting it while pan gesture.
Sleep for .2 seconds after zoom for pan prevent.
Do not zoom over 4 times.
And it can be used as container like this after importing from appropriate namespace:
<? xml version = "1.0" encoding = "utf-8" ?>
< ContentPage xmlns = "http://schemas.microsoft.com/dotnet/2021/maui"
xmlns: x = "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns: collects = "clr-namespace:Mab.Collects"
x: Class = "N3ema.Secondpageimageshow" >
< ContentPage.Content >
< collects:Zoom >
< collects:Zoom.Content >
< StackLayout >
< Image Source = "shahid.jpeg"
HorizontalOptions = "Fill"
Aspect = "Fill"
VerticalOptions = "FillAndExpand" />
</ StackLayout >
</ collects:Zoom.Content >
</ collects:Zoom >
</ ContentPage.Content >
</ ContentPage >

Xamarin Forms Resize Image inside Listview

How can I set the size of the image inside of a list view. Currently I have several lists that have icons but I don't see any options for changing the aspect ratio or size of the image so it just gets blown up to the height of the list item.
all the images are from the Drawable folder
var cell = new DataTemplate(typeof(MenuTextCell));
cell.SetBinding(TextCell.TextProperty, "Title");
cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
cell.SetValue(BackgroundColorProperty, Color.Transparent);
cell.SetValue(TextCell.TextColorProperty, Color.FromHex("262626"));
I am using a custom renderer
public class MenuTextCellRenderer : ImageCellRenderer
{
protected override View GetCellCore (Cell item, View convertView, ViewGroup parent, Context context)
{
var cell = (LinearLayout)base.GetCellCore (item, convertView, parent, context);
cell.SetPadding(20, 10, 0, 10);
cell.DividerPadding = 50;
var div = new ShapeDrawable();
div.SetIntrinsicHeight(1);
div.Paint.Set(new Paint { Color = Color.FromHex("b7b7b7").ToAndroid() });
if (parent is ListView)
{
((ListView)parent).Divider = div;
((ListView)parent).DividerHeight = 1;
}
var icon = (ImageView)cell.GetChildAt(0);
var label = (TextView)((LinearLayout)cell.GetChildAt(1)).GetChildAt(0);
label.SetTextColor(Color.FromHex("262626").ToAndroid());
label.TextSize = Font.SystemFontOfSize(NamedSize.Large).ToScaledPixel();
label.TextAlignment = TextAlignment.Center;
label.Text = label.Text.ToUpper();
var secondaryLabel = (TextView)((LinearLayout)cell.GetChildAt(1)).GetChildAt(1);
secondaryLabel.SetTextColor(Color.FromHex("262626").ToAndroid());
secondaryLabel.TextSize = Font.SystemFontOfSize(NamedSize.Large).ToScaledPixel();
label.TextAlignment = TextAlignment.Center;
return cell;
}
You are now dealing with an Android ImageView.
You have your reference via var icon = (ImageView)cell.GetChildAt(0).
You can now customize it via methods such as .setScaleType() for aspect ratio related, and use .layout() to change the position / size.
Try something like this on your ListView.ItemTemplate, instead of using the imagecell. You can also write your custom cell.
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="10">
<Image Aspect="AspectFill" HeightRequest ="20" WidthRequest="20" Source="{Binding IconSource}" />
<Label Text="{Binding Title}" YAlign="Center" Font="Medium" />
</StackLayout>
</ViewCell>

How to add a click handler to content page in xamarin forms

I am new at Xamarin.Forms and are trying to add a click event to my content page. I want an event to start when the user clicks on the page, no matter where.
I've created similar functionality in a WinPhone app, where I could solve my problem with OnLeftMouseButtonDown which was available on PhoneApplicationPage, but I could not find a suitable counterpart in the ContentPage. Any suggestions?
In order to get this working you have to add a Layout to the ContentPage, as you will want to specify some content, and set the HorizontalOptions and VerticalOptions to LayoutOptions.FillAndExpand.
This is not enough though to handle the taps correctly though.
You also need to specify a BackgroundColor for the Layout. I set mine to Color.Transparent. If you try without specifying a Color it does not work.
You then have to attach a TapGestureRecognizer to the ContentPage in order to catch the clicks that are made.
Although this works well with Labels and Buttons in my test below, still receiving the TapGestures for WindowsPhone on both types, along with the Button click event firing - this does not fully work with Android - as the Button click will prevent the TapGesture event from firing.
The other alternative is to try putting an 'invisible' Grid over the top of everything. However the issue with this approach is that you will loose the Click event handler from firing with WindowsPhone and also loose the Click event handler from firing with Android. The good part though - is that you can detect a click anywhere, although not pass this on. It just depends what your trying to achieve at the end of the day.
StackLayout objStackLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Transparent
};
//
Label objLabel1 = new Label();
objLabel1.BackgroundColor = Color.Red;
objLabel1.Text = "label1";
objLabel1.HorizontalOptions = LayoutOptions.Start;
objLabel1.VerticalOptions = LayoutOptions.Start;
objLabel1.WidthRequest = 100;
objLabel1.HeightRequest = 300;
objStackLayout.Children.Add(objLabel1);
//
Label objLabel2 = new Label();
objLabel2.BackgroundColor = Color.Green;
objLabel2.Text = "label2";
objLabel2.Font = Font.OfSize("Arial", 48);
objLabel2.WidthRequest = 100;
objLabel2.HeightRequest = 300;
objStackLayout.Children.Add(objLabel2);
//
Button objButton1 = new Button();
objButton1.Text = "Click Me";
objButton1.WidthRequest = 300;
objButton1.HeightRequest = 300;
objStackLayout.Children.Add(objButton1);
//
this.Content = objStackLayout;
TapGestureRecognizer objTapGestureRecognizer1 = new TapGestureRecognizer();
objTapGestureRecognizer1.Tapped += ((o2, e2) =>
{
System.Diagnostics.Debug.WriteLine("Clicked!");
});
this.Content.GestureRecognizers.Add(objTapGestureRecognizer1);

Windows Phone 7 ScrollView?

Is there a scrollView in phone7?
I have this code
private void button8_Click(object sender, RoutedEventArgs e)
{
for (int i=0; i<23; i++) {
Button btn = new Button() {
Content="newbutton "+i,
HorizontalAlignment =HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, 20+(i*60), 0, 0),
};
btn.Click += new RoutedEventHandler(btn_click);
ContentPanel.Children.Add(btn);
}
}
to add 23 buttons to my screen, what is the way to scrolling down the page to show all of the 23 buttons?
I'm assuming ContentPanel is a StackPanel.
In XAML:
<ScrollViewer>
<StackPanel x:Name="ContentPanel" />
</ScrollViewer>
You can use the ScrollViewer.ScrollToVerticalOffset method to scroll to the end of the page.
However, if you have other UIElements above the ScrollViewer those will still occupy the top of your screen with only the section occupied by the ScrollViewer being scrolled. To prevent that you'll have to have all the UIElements, including ContentPanel, be placed in the ScrollViewer.

Resources