Image elements without dimensions defined in Xamarin forms will now have their dimensions defined by attempting to scale it to the bounding box.

This commit is contained in:
Troy Stanger 2018-11-14 09:18:22 -06:00
parent a75d44b573
commit 12c532fd3d
4 changed files with 54 additions and 3 deletions

View File

@ -29,6 +29,8 @@ namespace Ooui.Forms.Renderers
var imageView = new Ooui.Image ();
SetNativeControl (imageView);
this.Style.Overflow = "hidden";
Control.Loaded += OnLoad;
}
if (e.NewElement != null) {
@ -51,6 +53,30 @@ namespace Ooui.Forms.Renderers
SetAspect ();
}
void OnLoad(object sender, EventArgs eventArgs)
{
var args = (TargetEventArgs)eventArgs;
var b = Element.Bounds;
double scale = 1;
if (Math.Abs(b.Width) > 0)
{
scale = b.Width / args.ClientWidth;
Element.WidthRequest = b.Width;
Element.HeightRequest = scale * args.ClientHeight;
}
else if (Math.Abs(b.Height) > 0)
{
scale = b.Height / args.ClientHeight;
Element.WidthRequest = scale * args.ClientWidth;
Element.HeightRequest = b.Height;
}
else
{
// We can't really know what to do in this case
}
}
void SetAspect ()
{
if (_isDisposed || Element == null || Control == null) {

View File

@ -37,6 +37,10 @@ const inputEvents = {
keyup: true,
};
const elementEvents = {
load: true
}
function getSize () {
return {
height: window.innerHeight,
@ -304,6 +308,12 @@ function msgListen (m) {
offsetY: e.offsetY,
};
}
else if (elementEvents[m.k]) {
em.v = {
clientHeight: node.clientHeight,
clientWidth: node.clientWidth
}
}
const ems = JSON.stringify (em);
send (ems);
if (debug) console.log ("Event", em);

View File

@ -52,7 +52,12 @@ namespace Ooui
add => AddEventListener ("keyup", value);
remove => RemoveEventListener ("keyup", value);
}
public event TargetEventHandler Loaded {
add => AddEventListener ("load", value);
remove => RemoveEventListener ("load", value);
}
public event TargetEventHandler MouseDown {
add => AddEventListener ("mousedown", value);
remove => RemoveEventListener ("mousedown", value);

View File

@ -220,8 +220,16 @@ namespace Ooui
if (handlers != null) {
var args = new TargetEventArgs ();
if (message.Value is Newtonsoft.Json.Linq.JObject o) {
args.OffsetX = (double)o["offsetX"];
args.OffsetY = (double)o["offsetY"];
if (o["offsetX"] != null)
{
args.OffsetX = (double)o["offsetX"];
args.OffsetY = (double)o["offsetY"];
}
if (o["clientHeight"] != null)
{
args.ClientHeight = (double)o.GetValue("clientHeight");
args.ClientWidth = (double)o.GetValue("clientWidth");
}
}
foreach (var h in handlers) {
h.Invoke (this, args);
@ -257,5 +265,7 @@ namespace Ooui
{
public double OffsetX { get; set; }
public double OffsetY { get; set; }
public double ClientHeight { get; set; }
public double ClientWidth { get; set; }
}
}