我使用的是一个显示不同图像的转盘控件,我希望能够在我的视图模型中获得转盘中使用的图像的大小,以便进行一些计算。我该怎么做?
主页.xaml
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Border Margin="0">
<controls:Carousel x:Name="CarouselControl"
InvertPositive="True"
ItemDepth="300"
ItemMargin="340"
ItemRotationX="0"
ItemRotationY="45"
ItemRotationZ ="0"
Orientation="Horizontal"
SelectedIndex="4">
<controls:Carousel.EasingFunction>
<CubicEase EasingMode="EaseOut" />
</controls:Carousel.EasingFunction>
<controls:Carousel.ItemTemplate>
<DataTemplate>
<Image
x:Name ="Images"
Width="200"
Height="200"
VerticalAlignment="Bottom"
Source="{Binding Thumbnail}"
Stretch="Uniform" />
</DataTemplate>
</controls:Carousel.ItemTemplate>
</controls:Carousel>
</Border>
如何访问x:Name="中的图像大小;图像";在该xaml页面的视图模型中?
如何访问x:Name="中的图像大小;图像";在该xaml页面的视图模型中?
所以你想得到你使用的所有图像的所有大小值,对吧?
你用来绑定的缩略图是什么?它是指向图像的Uri吗?如果它是一个Uri,要获取本地文件的大小,可以使用该Uri创建一个StorageFile对象,然后使用BitmapDecoder类获取Width和Height。
这里有一个代码示例,您可以参考:
StorageFile sf = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/london.png"));
BitmapDecoder dec = await BitmapDecoder.CreateAsync(await sf.OpenAsync(FileAccessMode.Read));
var width = dec.PixelWidth;
var height = dec.PixelHeight;
您应该将Carousel
的ItemsSource
绑定到视图模型的集合属性,该属性返回表示要在视图中显示的图像的项。
然后,您可以将视图中Image
元素的Width
和Height
属性绑定到此对象的相应源属性,就像当前将Source
目标属性绑定到Thumbnail
源属性一样。
因此,将Thumbnail
属性移动到表示图像或项的新类中,然后将Width
和Height
属性添加到同一类中,并在XAML标记中绑定到它们。