MeasureOverride does not work on Canvas - xaml

I need to change Rectangle size placed on Canvas, so I inherited Canvas class to use MeasureOverride:
public class CustomCanvas : Canvas
{
public CustomCanvas() : base(){}
protected override Size MeasureOverride(Size availableSize)
{
Size canvasDesiredSize = new Size();
foreach (UIElement child in Children)
{
child.Measure(new Size(300, 300));
canvasDesiredSize = child.DesiredSize;
}
return canvasDesiredSize;
}
}
And used it:
<local:CustomCanvas Width="500" Height="800" Background="Gray">
<Rectangle Fill="AliceBlue" Height="100" Width="100"/>
</local:CustomCanvas>
But after execution the rectangle still 100,100 size (as specified in XAML). When child.Measure(new Size(300, 300)); executed child.DesiredSize shows 0,0. Something strange happens there.
I used this as reference.

The short answer is that Width and Height override any size given in Measure().
Microsoft's documentation lacks important information regarding the data flow between Measure() and MeasureOverride().
The container calls Child.Measure()
Measure() calls MeasureCore()
MeasureCore() calls MeasureOverride()
Measure is a method of UIElement.
MeasureCore is a method of FrameworkElement, which inherits from UIElement. MeasureOverride is a method of FrameworkElement. MeasureOverride gets overridden by the child.
UIElement.Measure(avialableSize) does the following before calling MeasurementCore():
throws exception if avialableSize IsNan, both for width and height
if UIElement is collapsed, MeasureCore will not get called
if the availableSize has not changed, MeasureCore will not get called, unless InvalidateMeasure was called before
call InvalidateArrange
FrameworkElement.MeasurementCore(availableSize) does the following before calling MeasureOverride():
removes Margin from availableSize
enforces height, minHeight and maxHeight over availableSize and the same for the witdh properties
after calling MeasureOverride(), MeasurementCore() adjusts the values returned by MeasureOverride() to calculate DesiredSize:
DesiredSize must be within MinWidth and MaxWidth
add margin to DesiredSize
after calling FrameworkElement.MeasurementCore(), UIElement.Measure() executes:
throws exception if return width or height is Infinite
throws exception if return width or height isNaN

Related

Xamarin Forms Image with 100% of parent container width and auto Height to maintain aspect ratio

Is it possible to have an Image take 100% width of its parent container while actually fitting the whole image within this container without clipping, and while having height automatically adjusted to preserve aspect ratio?
I have read similar questions both on SO and Xamarin Forums but apparently this cannot be done without implementing custom renderers or manually calculating correct sizes in code. But to calculate this in code you would need either image dimensions or aspect ratio. For applications where neither of these are known before head, this is a problem.
In terms of CSS, the solution I am looking for is similar to having
width:100%; height:auto;
Implementing a custom renderer for such a trivial task is an overkill and a huge embarrassment for Xamarin in my opinion; unless I am understanding something wrong.
I have searched for an answer to this problem for a long time. As others have noted, I did not find a Xaml-only solution. I choose a different route and present my solution for those who might find it instructive and as the seed for changes to FFImageLoading to solve this correctly.
I created a subclass of CachedImage and overrided OnMeasure. In order for CachedImage.OnMeasure to work in the aspect modes (not fill mode), it has to return a SizeRequest which matches the ratio of the underlying image. So the solution I chose was simply to use the provided widthConstraint and calculate the height as widthConstraint / ratio. This description only addresses one of the many cases: where the widthConstraint is not infinity and the desire is to answer the specific question posed here (width of 100% and auto height).
A subclass which implements this case:
public class CachedImage2 : CachedImage
{
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
var sr = base.OnMeasure(widthConstraint, heightConstraint);
if (sr.Request.IsZero)
return sr;
var ratioWH = sr.Request.Width / sr.Request.Height;
var sr2 = new SizeRequest(new Size(widthConstraint, widthConstraint / ratioWH));
return sr2;
}
}
I was unable to find a pure XAML solution to this problem and therefore decided to use ffimageloading's Success event to find out the original width and height of loaded image and get aspect ratio from these values, store it, and use it in SizeAllocated event to maintain aspect ratio of the image while making sure its width is 100% of the parent container.
Sample code:
private void testImage_OnSuccess(object sender, CachedImageEvents.SuccessEventArgs e)
{
var image = sender as CachedImage;
double width = e.ImageInformation.OriginalWidth;
double height = e.ImageInformation.OriginalHeight;
ratio = width / height; // store it in a variable
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
if (this.Width > 0 && ratio != 0) // width of the parent container
testImage.HeightRequest = this.Width / ratio;
}
Presuming when you say:
and while having height automatically adjusted..
You mean the height of the container.
Yes this is completely possible in Xamarin.Forms.
Let's imagine I have a Grid as my parent container. Here is how I would do it.
<!-- remove all the padding & margin & spacing on Grid -->
<Grid RowSpacing="0"
ColumnSpacing="0"
Margin="0"
Padding="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <!-- the containers height will now adjust -->
<RowDefinition Height="56"/> <!-- This one is for the other content in your view etc -->
</Grid.RowDefinitions>
<!-- Put your image inside your parent container and apply properties -->
<Image Source="some_source.png"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
</Grid>
The Horizontal and vertical options are as if you are setting width:100% and height: 100% in CSS.
Put you Image in a frame. And then set the height and width of image accprding to the frame.
<Frame>
<Image></Image>
</Frame>
You could change the width and height according to your frame via binding Value Converters.
Binding Value Converters: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
Set the name to your Frame first.
<Frame
…………
x:Name="frame"/>
Create the MyConverter. You could change the percentage of value in Convert method.
MyConverter.cs
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Set the StaticResource.
<ContentPage.Resources>
<ResourceDictionary>
<local:MyConverter x:Key="MyConverter" />
</ResourceDictionary>
Binding to your image.
<Image WidthRequest="{Binding Source={x:Reference frame},Path=Width,Converter={StaticResource MyConverter}}"
HeightRequest="{Binding Source={x:Reference frame},Path=Height,Converter={StaticResource MyConverter}}"></Image>

Why does easeljs stage.getBounds() return null?

In this example:
var canvas = document.getElementById("testCanvas");
var stage = new createjs.Stage(canvas);
function drawRectangle(){
var rect = new createjs.Shape();
rect.graphics.beginFill("#000").drawRect(10, 10, 100, 100);
stage.addChild(rect);
}
function drawShapes(){
drawRectangle();
stage.update();
}
drawShapes();
// Why does this call log null?
console.log(stage.getBounds());
<script src="https://cdnjs.cloudflare.com/ajax/libs/EaselJS/0.8.0/easeljs.min.js"></script>
<canvas id="testCanvas" width="600" height="150"></canvas>
stage.getBounds(); is returning null. Why does it return null? According to the docs, it's supposed to return a rectangle object representing the dimensions of the bounding box of the stage.
If this usage is incorrect, what is the correct usage to get the dimensions of the object?
It looks like the stage object can't calculate their own bounds.
In the same getBounds() documentation it appears the following:
Not all display objects can calculate their own bounds (ex. Shape). For these objects, you can use setBounds so that they are included when calculating Container bounds.
If it's the case, as the documentation says, you can use the setBounds() method (documentation) to set manually the bounds of the object:
setBounds(x,y,width,height)
In the case of your code:
stage.setBounds(0,0,600,150); // (0,0), at the same size of the canvas

Creating a flexible UI contianer for an image and a label

I thought this would be like pretty simple task to do, but now I have tried for hours and cant figure out how to get around this.
I have a list of friends which should be displayed in a scrollable list. Each friend have a profile image and a name associated to him, so each item in the list should display the image and the name.
The problem is that I cant figure out how to make a flexible container that contains both the image and the name label. I want to be able to change the width and height dynamically so that the image and the text will scale and move accordingly.
I am using Unity 5 and Unity UI.
I want to achieve the following for the container:
The width and height of the container should be flexible
The image is a child of the container and should be left aligned, the height should fill the container height and should keep its aspect ratio.
The name label is a child of the contianer and should be left aligned to the image with 15 px left padding. The width of the text should fill the rest of the space in the container.
Hope this is illustrated well in the following attached image:
I asked the same question here on Unity Answers, but no answers so far. Is it really possible that such a simple task is not doable in Unity UI without using code?
Thanks a lot for your time!
Looks like can be achieved with layout components.
The image is a child of the container and should be left aligned, the height should fill the container height and should keep its aspect ratio.
For this try to add Aspect Ratio Fitter Component with Aspect mode - Width Controls Height
The name label is a child of the container and should be left aligned to the image with 15 px left padding. The width of the text should fill the rest of the space in the container.
For this you can simply anchor and stretch your label to the container size and use BestFit option on the Text component
We never found a way to do this without code. I am very unsatisfied that such a simple task cannot be done in the current UI system.
We did create the following layout script that does the trick (tanks to Angry Ant for helping us out). The script is attached to the text label:
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent (typeof (RectTransform))]
public class IndentByHeightFitter : UIBehaviour, UnityEngine.UI.ILayoutSelfController
{
public enum Edge
{
Left,
Right
}
[SerializeField] Edge m_Edge = Edge.Left;
[SerializeField] float border;
public virtual void SetLayoutHorizontal ()
{
UpdateRect ();
}
public virtual void SetLayoutVertical() {}
#if UNITY_EDITOR
protected override void OnValidate ()
{
UpdateRect ();
}
#endif
protected override void OnRectTransformDimensionsChange ()
{
UpdateRect ();
}
Vector2 GetParentSize ()
{
RectTransform parent = transform.parent as RectTransform;
return parent == null ? Vector2.zero : parent.rect.size;
}
RectTransform.Edge IndentEdgeToRectEdge (Edge edge)
{
return edge == Edge.Left ? RectTransform.Edge.Left : RectTransform.Edge.Right;
}
void UpdateRect ()
{
RectTransform rect = (RectTransform)transform;
Vector2 parentSize = GetParentSize ();
rect.SetInsetAndSizeFromParentEdge (IndentEdgeToRectEdge (m_Edge), parentSize.y + border, parentSize.x - parentSize.y);
}
}

Getting ActualWidth in Windows Phone8.1

I'd like to get the actual width of an image in my WP8.1 app. The width cannot be determined (i.e. is zero) until the page has been rendered, and other solutions suggest handling this in the page loaded event as in the basic example below. But even here, img.ActualWidth is zero.
How can I retrieve img.ActualWidth once as soon as the page is rendered?
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e) {
Debug.WriteLine(img.ActualWidth);
}
}
and
<Page
x:Class="Page_Loaded.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Page_Loaded"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Image x:Name="img" Source="/image.jpg" />
</Grid>
</Page>
Use SizeChanged event handler. It occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement.
ActualWidth is a calculated property. The calculations are a result of a layout pass, where the object is sized in layout according to the logic of its successive layout parents.
The default of ActualWidth is 0. The default might be encountered if the object has not been loaded and hasn't yet been involved in a layout pass that renders the UI. (This is happening in your case)
ActualWidth can have multiple or incremental reported changes to the value because of operations by the layout system. If you get the value while layout is still iterating, the layout system might still be calculating the required measure of space for child objects, constraints by the parent object, and so on. Because the value is based on an actual rendering pass, it may lag slightly behind the set value of properties like Width, which can be the basis of the input change.
For purposes of ElementName binding, ActualWidth does not post updates when it changes (due to its asynchronous and run-time calculated nature). Do not attempt to use ActualWidth as a binding source for an ElementName binding. If you have a scenario that requires updates based on ActualWidth, use a SizeChanged handler.
SizeChanged fires whenever the size (either ActualHeight or ActualWidth) has changed on the object, which is after the Measure and Arrange passes are complete. One reason to handle the SizeChanged event is to see whether the ratio of an element's ActualHeight versus ActualWidth have changed, because of a new layout.

JavaFX - SplitPane, resize & proportions

How can I achieve proportional resize of whole SplitPane?
public class JfxSplitPaneTest extends Application {
#Override
public void start(Stage stage) throws Exception {
SplitPane split = new SplitPane();
StackPane child1 = new StackPane();
child1.setStyle("-fx-background-color: #0000AA;");
StackPane child2 = new StackPane();
child2.setStyle("-fx-background-color: #00AA00;");
StackPane child3 = new StackPane();
child3.setStyle("-fx-background-color: #AA0000;");
split.getItems().addAll(child1, child2, child3);
//split.setDividerPositions(0.1f, 0.6f, 0.9f)
stage.setScene(new Scene(split, 400, 300));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Start the program:
Set dividers how I like them:
Make window width really small (I continued and made it even smaller, no pic):
Resize back and observe that dividers are neither how I set them nor how they were when program started.
Doing this makes it closer to what I'd expect:
child1.setMinWidth(Region.USE_PREF_SIZE)
child1.setPrefWidth(100)
child2.setMinWidth(Region.USE_PREF_SIZE)
child2.setPrefWidth(200)
child3.setMinWidth(Region.USE_PREF_SIZE)
child3.setPrefWidth(300)
except that dividers are initally at wrong position (until I resize SplitPane, 1px is enough) and when shrinking window width, dividers are inside components.
How can I make this work please?
Try to set the divisors like AS3Boyan said
split.setDividerPositions(0.1f, 0.6f, 0.9f)
and add this:
How can I avoid a SplitPane to resize one of the panes when the window resizes?
Try to add Change Listener to stage.widthProperty() and stage.heightProperty(). And call this:
split.setDividerPositions(0.1f, 0.6f, 0.9f)
The rule of thumb is to use setDividerPositions with Platform.runLater when the resize event is triggered.
You probably want to start with a default ratio, let the user change that ratio, and maintain this ratio whatever it is when there's a resize event. Let's consider a 25/75 vertical FX splitPane in some stage:
splitPane.setDividerPositions(0.25f, 0.75f);
stage.heightProperty().addListener((obs, oldVal, newVal) -> {
double[] positions = splitPane.getDividerPositions(); // reccord the current ratio
Platform.runLater(() -> splitPane.setDividerPositions(positions)); // apply the now former ratio
});