How to make a vertical scrollview Xamarin - c#

I am trying to make a vertical scrollview in Xamarin(for iOS). The scroll view scrolls up and down vertically how it is supposed to but when I populate it with buttons they are added horizontally. I am new to Xamarin and the only example they give is a horizontal scrollview it seems. Thanks in advance here is my code :
nfloat h = 50.0f;
nfloat w = 150.0f;
nfloat padding = 10.0f;
scrollView = new UIScrollView
{
Frame = new CGRect(0, 100, View.Frame.Width, View.Frame.Height),
ContentSize = new CGSize(View.Frame.Width,View.Frame.Height*3),
BackgroundColor = UIColor.White,
AutoresizingMask = UIViewAutoresizing.FlexibleHeight
};
//This is in a separate area and there are a few more of these blocks of code
var button = UIButton.FromType(UIButtonType.RoundedRect);
button.SetTitle(ln, UIControlState.Normal);
button.Frame = new CGRect(padding * (i + 1) + (i * w), padding, w, h);
button.TouchUpInside += Button2TouchUpInside;
scrollView.AddSubview(button);
buttons.Add(button);
//then finally
View.AddSubview(scrollView);

Related

How to add a Controls.Grid in a Canvas

I would like add a Grid in a Canvas and put inside a Rectangle.
Here my code
Grid gridForModules = new Grid();
Canvas.SetLeft(gridForModules, 600);
Canvas.SetTop(gridForModules, 80);
AddRowsOfGrid(gridForModules, 5);
AddColumnsOfGrid(gridForModules, 8);
gridForModules.ShowGridLines = true;
m_grid.RegisterName("ModulesGRID", gridForModules);
m_canvas.Children.Add(gridForModules);
Rectangle rect = new Rectangle();
Grid.SetColumn(rect, 2);
Grid.SetRow(rect, 2);
Grid.SetRowSpan(rect, 2);
Grid.SetColumnSpan(rect, 2);
rect.Fill = new SolidColorBrush(Colors.Coral);
rect.Name = "ModuloEsempio";
gridForModules.Children.Add(rect);
m_grid.RegisterName(rect.Name, rect);
Thanks
You specify that the columns/rows of the Grid should be equally wide/high by setting new GridLength(1.0, GridUnitType.Star) on both. So if you have 5 rows then the height of each row should be the height of the Grid divided by 5.
The problem in your code is that the Grid doesn't have a height or width because the Canvas is just a drawing board that doesn't size its contents.
To solve your problem you either have to set the size on the Grid using
gridForModules.Width = 300;
gridForModules.Height = 200;
or you have to set it in the Column/RowDefinitions
col.Width = new GridLength(30);
row.Height = new GridLength(30);
After that you should see your Grid and your Rectangle (if you look a bit far to the right).

How do I center content in a Panel?

I have a panel, inside which I am trying to center the controls. But apparently, panels don't like padding when they are docked to the edge of a control. Here is the current code for my panel:
buttonPanel = new Panel();
buttonPanel.Width = 300;
buttonPanel.Height = 50;
buttonPanel.Dock = DockStyle.Bottom;
buttonPanel.Padding = new Padding((this.ClientSize.Width - buttonPanel.Width) / 2, 0,
(this.ClientSize.Width - buttonPanel.Width) / 2, 0);
buttonPanel.BackColor = Color.Transparent;
this.Controls.Add(buttonPanel);
I have a single button placed inside the panel. What I would expect with the above code, is that the control is placed nicely to the left of a 300 wide "rectangle" centered in the panel. But the button is placed on the very left, as if the padding is ignored:
How can I center a collection of buttons to the center of my form?
Design Time Approach
At design time, put your button in your container and select your button. Then, use Center Horizontally and Center Vertically from Layout toolbar to put your button in center of panel. After, go to your buttons properties and remove every anchor from Anchor property.
Coding Approach
Panel panel = new Panel();
panel.Size = new Size(300, 100);
panel.Dock = DockStyle.Bottom;
Button button = new Button();
button.Size = new Size(70, 25);
button.Location = new Point((panel.Width - button.Width) / 2, (panel.Height - button.Height) / 2);
button.Anchor = AnchorStyles.None;
panel.Controls.Add(button);
this.Controls.Add(panel);
You have to align the button inside the panel by setting the button's position not the panel's padding:
button1.Left = (int)(panel1.Width * 0.5f - button1.Width * 0.5f);
button1.Top = (int)(panel1.Height * 0.5f - button1.Height * 0.5f);
or
button1.Location = new Point()
{
X = panel1.Width / 2 - button1.Width / 2,
Y = panel1.Height / 2 - button1.Height / 2
};
the result is the same.
If you override the OnResize method the button will stay centered also when you change the size of the form:
protected override void OnResize(EventArgs e)
{
button1.Location = new Point()
{
X = panel1.Width / 2 - button1.Width / 2,
Y = panel1.Height / 2 - button1.Height / 2
};
base.OnResize(e);
}

Toast Slide with Easing

I have a "Toast" WPF window that slides in from the bottom right corner of the screen - How can I add an
ease to the slide that bounces back a bit
var anim = new DoubleAnimation
{
Duration = TimeSpan.FromMilliseconds(280),
From = Left,
To = Left - (Width * 2),
};
this.BeginAnimation(Window.LeftProperty, anim);
var anim = new DoubleAnimation
{
Duration = TimeSpan.FromMilliseconds(280),
From = Left,
To = Left - (Width * 2),
EasingFunction = new BounceEase()
};
DoubleAnimation Class (System.Windows.Media.Animation) - MSDN
Easing Functions - MSDN

Printing Image from Silverlight

I'm trying to print a Image in Landscape mode in Silverlight.
I found a great example here. Where most of the code comes from. The code worked perfectly as expected. When I changed the Line to an Image it failed.
Code
Canvas OuterCanvas = new Canvas();
/* a container for everything that will print */
Border OuterBorder = new Border()
{
BorderThickness = new Thickness(3),
BorderBrush = new SolidColorBrush(Colors.Red),
Margin = new Thickness(10)
};
double Width = e.PrintableArea.Width - OuterBorder.Margin.Left - OuterBorder.Margin.Right;
double Height = e.PrintableArea.Height - OuterBorder.Margin.Top - OuterBorder.Margin.Bottom;
/* NOTE: We're trying to force landscape, so swop the width and height */
OuterBorder.Width = Height;
OuterBorder.Height = Width;
/* on portrait, this line goes down (leave the printer settings, we're trying to force landscape) */
Line Line = new Line()
{
X1 = OuterBorder.Width / 2,
Y1 = 0,
X2 = OuterBorder.Width / 2,
Y2 = OuterBorder.Height,
Stroke = new SolidColorBrush(Colors.Blue),
StrokeThickness = 3
};
//
// Here is where I changed the Line to an Image
//
OuterBorder.Child = imageElementInXaml; //Line;
OuterCanvas.Children.Add(OuterBorder);
/* rotate 90 degrees, and move into place */
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new RotateTransform() { Angle = 90 });
transformGroup.Children.Add(new TranslateTransform() { X = e.PrintableArea.Width });
OuterBorder.RenderTransform = transformGroup;
e.PageVisual = OuterCanvas;
e.HasMorePages = false;
I know that a Border can only contain 1 element in which I have done so, and when I printed the image on its own without trying to make it landscape this worked too. So why wont it work when I simply replace the Line with the image Element
So since posting this I found some code (cant remember where now) that has helped me get the printing working. Its not as clean as I would have liked but it works.
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Image image = new Image();
image.Source = imgPlayer.Source;
//This is important
image.Stretch = Stretch.Uniform;
// Find the full size of the page
Size pageSize = new Size(e.PrintableArea.Width + e.PageMargins.Left + e.PageMargins.Right, e.PrintableArea.Height + e.PageMargins.Top + e.PageMargins.Bottom);
var MARGIN= 10;
// Get additional margins to bring the total to MARGIN (= 96)
Thickness additionalMargin = new Thickness
{
Left = Math.Max(0, MARGIN - e.PageMargins.Left),
Top = Math.Max(0, MARGIN - e.PageMargins.Top),
Right = Math.Max(0, MARGIN - e.PageMargins.Right),
Bottom = Math.Max(0, MARGIN - e.PageMargins.Bottom)
};
// Find the area for display purposes
Size displayArea = new Size(e.PrintableArea.Width - additionalMargin.Left - additionalMargin.Right, e.PrintableArea.Height - additionalMargin.Top - additionalMargin.Bottom);
bool pageIsLandscape = displayArea.Width > displayArea.Height;
bool imageIsLandscape = image.ActualWidth > image.ActualHeight;
double displayAspectRatio = displayArea.Width / displayArea.Height;
double imageAspectRatio = (double)image.ActualWidth / image.ActualHeight;
double scaleX = Math.Min(1, imageAspectRatio / displayAspectRatio);
double scaleY = Math.Min(1, displayAspectRatio / imageAspectRatio);
// Calculate the transform matrix
MatrixTransform transform = new MatrixTransform();
if (pageIsLandscape == imageIsLandscape)
{
// Pure scaling
transform.Matrix = new Matrix(scaleX, 0, 0, scaleY, 0, 0);
}
else
{
// Scaling with rotation
scaleX *= pageIsLandscape ? displayAspectRatio : 1 / displayAspectRatio;
scaleY *= pageIsLandscape ? displayAspectRatio : 1 / displayAspectRatio;
transform.Matrix = new Matrix(0, scaleX, -scaleY, 0, 0, 0);
}
Image image2 = new Image
{
Source = image.Source,
Stretch = Stretch.Fill,
Width = displayArea.Width,
Height = displayArea.Height,
RenderTransform = transform,
RenderTransformOrigin = new Point(0.5, 0.5),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Margin = additionalMargin,
};
Border border = new Border
{
Child = image,
};
e.PageVisual = border;
}

WinRT how do you dynamically add a Triangle to a canvas

I would like to dynamically add a Triangle to my canvas. I can do this but only in one very specific way that does not work for my application. What I would like to do is supply a point and a size and get back a triangle.
var poly = shape as Polygon;
Polygon p = new Polygon
{
//Width = size,
//Height = size,
Fill = new SolidColorBrush(Colors.Red),
ManipulationMode = ManipulationModes.All,
RenderTransform = new CompositeTransform()
};
int w = 200;
int h = 200;
Point start = new Point(400, 200);
var right = new Point(start.X + w, start.Y);
var top = new Point(start.X + (w / 2), start.Y - (h));
poly.Points.Add(point);
poly.Points.Add(right);
poly.Points.Add(top);
poly.Points.Add(point);
Then I add the shape to my canvas child controls and and set the X Y on the shapes RenderTransform. Nothing appears. If however I just do this:
Polygon p = new Polygon
{
//Width = size,
//Height = size,
Fill = new SolidColorBrush(Colors.Red),
ManipulationMode = ManipulationModes.All,
RenderTransform = new CompositeTransform()
};
p.Points.Add(new Point(300, 200));
p.Points.Add(new Point(400, 125));
p.Points.Add(new Point(400, 275));
p.Points.Add(new Point(300, 200));
It renders a Triangle just fine. However if you supply a width and height to the above code it will stop rendering.
Can I just create a triangle of a certain size, without having to set actual points on the canvas at first, much like you do an Ellipse or a Rectangle?

Categories