How to set Height Request for frame in code behind? - xamarin

I am trying to dynamically create a frame in xamarin and wondering if it is possible to set the height request.
this is the code I have tried so far
Frame imageFrame = new Frame();
Frame.HeightRequestProperty = 70 ;
It gives this error:
A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)

HeightRequestProperty is BindableProperty which you can't set value on it directly .
Modify your code as below
Frame imageFrame = new Frame();
imageFrame.HeightRequest = 70;
Or create binding
Frame imageFrame = new Frame();
imageFrame.SetBinding(Frame.HeightRequestProperty, "Height");

Related

How to show ActivityIndicator in the middle of the screen?

I've created an activity indicator and added it to StackLayout and when I make it running, in the emulator it shows in the top right corner Android 4.4 and in iOS no show and in Android 6 phone, it don't show.
var indicator = new ActivityIndicator()
{
Color = Color.Blue,
};
indicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy", BindingMode.OneWay);
indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsBusy", BindingMode.OneWay);
AbsoluteLayout.SetLayoutFlags(indicator, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(indicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
mainLayout.Children.Add(indicator);
I want to show the activity indicator to the center of the screen because the operation takes time to complete.
The indicator that you are seeing in the status bar is the default behavior of the IsBusy property of the base page class. The reason your code isn't working is because you are attempting to bind visibility of your ActivityIndicator to that property - but you aren't specifying a binding source. If you look in your debugger's application output log then you will probably see messages along the lines of "Property 'IsBusy' not found on type 'Object'".
To fix it, you simply need to point the Binding Context of each binding to the form. Give this a try:
public partial class App : Application
{
public App ()
{
var mainLayout = new AbsoluteLayout ();
MainPage = new ContentPage {
Content = mainLayout
};
var containerPage = Application.Current.MainPage;
var indicator = new ActivityIndicator() {
Color = Color.Blue,
};
indicator.SetBinding(VisualElement.IsVisibleProperty, new Binding("IsBusy", BindingMode.OneWay, source: containerPage));
indicator.SetBinding(ActivityIndicator.IsRunningProperty, new Binding("IsBusy", BindingMode.OneWay, source: containerPage));
AbsoluteLayout.SetLayoutFlags(indicator, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(indicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
mainLayout.Children.Add(indicator);
containerPage.IsBusy = true;
}
}
You add your activity indicator to stack layout, but you are setting Absolute layout LayoutFlags, they won't work.
to be able to achieve what you want, you need suck structure
AbsoluteLayout
StackLayout
ActivityIndicator
mainLayout should be AbsoluteLayout, all content should be contained in nested StackLayout.

Xamarin Forms vertical align image at bottom programmatically

In the (relatively) new Xamarin Forms I'm trying to vertically align an image to the bottom of a scrollview.
This is my code, this does exactly what I want (for larger images it scrolls). But when I have an image which is smaller than the height of the device, it should align to the bottom of the screen, instead of the center (probably default) of the screen. Since the documentation is (still) lacking, how can I achieve this in code?
return new ContentPage {
Content = new ScrollView {
Orientation = ScrollOrientation.Vertical,
BackgroundColor = Color.Black,
Content = new Image {
Source = ImageSource.FromFile (image)
}
}
};
I've tried it with this, but it gives an error that the call is ambiguous between the following methods or properties...
RelativeLayout rl = new RelativeLayout ();
rl.Children.Add (new ScrollView {
Orientation = ScrollOrientation.Vertical,
BackgroundColor = Color.Black,
Content = new Image {
Source = ImageSource.FromFile (image)
}
});
return new ContentPage {
Content = rl
};
From the Assembly Browser in Xamarin Studio, your options for Children.Add are:
void Add(T view, Expression<Func<Rectangle>> bounds);
void Add(T view, Expression<Func<double>> x = null, Expression<Func<double>> y = null, Expression<Func<double>> width = null, Expression<Func<double>> height = null);
void Add(T view, Constraint xConstraint = null, Constraint yConstraint = null, Constraint widthConstraint = null, Constraint heightConstraint = null);
Where Expression is the type from System.Linq.Expresssions.
You received an ambiguous call error because all these overloads have default values for all parameters besides view.
In order to use the object initializer for Children, you'll need to pass in your expressions or constraints like:
rl.Children.Add (
{
new ScrollView {
Orientation = ScrollOrientation.Vertical,
BackgroundColor = Color.Black,
Content = new Image {
Source = ImageSource.FromFile (image)
}
},
Constraint.Constant(0),
Constraint.Constant(0)
}
)
Constraint.RelativeToParent and Constraint.RelativeToView are useful in simple cases and Expression trees will solve arbitrary layout problems.

Updating Canvas Image dynamically using PaperJs

I have an image element with src pointing to an Handler (asp.net), and this image being used as source for Raster (PaperJs) object defined as global object and updated during window.load.
var raster;
paper.install(window);
function WindowOnLoad() {
raster = new paper.Raster('MainContent_imageData');
raster.position = paper.view.center;
window.paper.view.draw();
}
The above code is loading image onto the canvas on first load then the image element is getting updated through a button click which is associated with callback control (ComponentArt callback control) but the canvas is not, it displays blank.
Then I created an handler that is being called after the callback is complete but it also didn't work.
function CallBackCompleted(sender,eventArgs) {
var imgData = document.getElementById('MainContent_imageData');
raster.image = imgData ;
raster.position = window.paper.view.center;
window.paper.view.draw();
}
The following code fixed the issue. The raster object is added as a child object on layer[0] removed all the other objects except children[0], which refers to the raster object.
function CallBackCompleted(sender,eventArgs) {
if(window.paper.project.layers[0].hasChildren())
window.paper.project.layers[0].removeChildren(1);
var imageObj = new Image();
imageObj.src = document.getElementById('MainContent_imageData').src;
imageObj.onload = function () {
window.paper.project.layers[0].children[0].setImage(imageObj);
window.paper.project.layers[0].children[0].position = window.paper.view.center;
window.paper.view.draw();
}
}

make slide show wp7

I have to do a slide show off images stored in my isolated storage.. but i am beginner in windows phone and i have some dificulties.. i already know how to present the images, or show the images in the screen.. but i want to present the images 2 seconds each one.. theres some funcionalty to define the time to reproduce? Any example?
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(name_image, FileMode.Open, myIsolatedStorage);
var image = new BitmapImage();
image.SetSource(stream);
image1.Source = image;
This is how i open the image. I have a foreach with 5 name of images then i open each one.. but i want to see the images 2 seconds..
You could make the current thread sleep for 2 seconds:
System.Threading.Thread.Sleep(2000);
As the last sentence in the foreach body. It is not very neat, but it will do the job.
A better way is to use Reactive Extension.
First take a look at my answer in this post. It tells you what dlls you will need as well as some useful links.
Basically you need to store you images in a collection and then use Rx (GenerateWithTime) to create a observable sequence with time dimension based on the collection. Finally you call a method to add one image and subscribe it to the observable sequence.
Here is one working example,
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// create a collection to store your 5 images
var images = new List<Image>
{
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 }
};
// create a time dimension (2 seconds) to the generated sequence
IObservable<Image> getImages = Observable.GenerateWithTime(0, i => i <= images.Count - 1, i => images[i], _ => TimeSpan.FromSeconds(2), i => ++i);
// subscribe the DisplayOneImage handler to the sequence
getImages.ObserveOnDispatcher().Subscribe(DisplayOneImage);
}
private void DisplayOneImage(Image image)
{
// MyItems is an ItemsControl on the UI
this.MyItems.Items.Add(image);
}
Hope this helps. :)

Can't animate a custom view

I'm trying to animate the movement of a custom view of mine using the following code (my best translation from objective-c from Apples own documents)
var animDict = new NSMutableDictionary ();
animDict [NSViewAnimation.TargetKey] = this.View;
animDict [NSViewAnimation.StartFrameKey] = NSValue.FromRectangleF (this.View.Frame);
animDict [NSViewAnimation.EndFrameKey] = NSValue.FromRectangleF (new RectangleF (0, 150 * index, 400, 150));
var theAnim = new NSViewAnimation ();
theAnim.SetValuesForKeysWithDictionary (animDict);
theAnim.Duration = 2;
theAnim.StartAnimation ();
However I get the following run-time error when running the app: 2011-05-08 00:53:30.827 EpisodeNext[61715:613] [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key NSViewAnimationTargetKey.
Any idea what I'm doing wrong? Thanx!
You cannot use the method SetValuesForKeysWithDictionary to pass the animation keys to the NSViewAnimation instance.
You must use the NSViewAnimation(NSDictionary[] viewAnimations) constructor to pass the animation dictionary.
// Create the attributes dictionary for the first view.
NSMutableDictionary firstViewDict = new NSMutableDictionary ();
//var firstViewRect = new NSDictionary[] { };
firstViewDict [NSViewAnimation.TargetKey] = animateWindow;
firstViewDict [NSViewAnimation.StartFrameKey] = NSValue.FromRectangleF (new RectangleF (1000, 300 , this.Frame.Width - 40, 0));
firstViewDict [NSViewAnimation.EndFrameKey] = NSValue.FromRectangleF (SettingNSWindow.Frame);
//var NSDict = new NSDictionary[]{ };
NSDictionary[] NSDict = new NSDictionary[]{firstViewDict};
//NSDict.Cast(NSMutableDictionary = (NSDictionary[])firstViewDict;
NSViewAnimation theAnim = new NSViewAnimation(NSDict);
//theAnim.SetValuesForKeysWithDictionary (firstViewDict);
theAnim.Duration = 2;
theAnim.StartAnimation ();

Resources