Is there any way to get the scale factor by which a ViewBox scales its content in a XAML Windows 8 Metro Style App
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The WinRTXAMLToolit has an extension for this.
public static double GetChildScaleX(this Viewbox viewbox)
{
if (viewbox.Child == null)
throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with no child.");
var fe = viewbox.Child as FrameworkElement;
if (fe == null)
throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with a child that is not a FrameworkElement.");
if (fe.ActualWidth == 0)
throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with a child that is not laid out.");
return viewbox.ActualWidth / fe.ActualWidth;
}
GetChildScaleY
is the same, but with Heights.
回答2:
You may try to add a grid view inside it and customize its' columns and rows from layout property up to your content. Set it width and height to auto, and let it stretch width and height. Also you should let your content stretch or set witdh and height to automatic again.
On the other way, you can try to experience blend tool...
回答3:
You can compare its ActualWidth/ActualHeight
to the ActualWidth/ActualHeight
of its child. Note that these might be different if its Stretch is set to Fill
.