我有此代码:
<ContentPage.Content>
<StackLayout HorizontalOptions="Fill" Padding="15">
<Frame IsClippedToBounds="True" HeightRequest="45" CornerRadius="5" Padding="0" BackgroundColor="Transparent">
<Entry Placeholder="Search" ReturnType="Done" PlaceholderColor="Gray" x:Name="txtSearch" Margin="5,0,0,0" TextColor="White" />
</Frame>
<CollectionView ItemsSource="{Binding sourceList}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="2" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<ff:CachedImage
Source="{Binding Source}"
VerticalOptions="Center"
HorizontalOptions="Fill"
x:Name="TemplateImage" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage.Content>
现在我希望我能做:
public TemplateList()
{
InitializeComponent();
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
var width = mainDisplayInfo.Width;
var density = mainDisplayInfo.Density;
var ScaledWidth = width / density;
var WidthHeight = (ScaledWidth - (3 * 10)) / 2;
SizeChanged += (s, a) =>
{
TemplateImage.HeightRequest = WidthHeight;
TemplateImage.WidthRequest = WidthHeight;
};
}
但是我得到一个错误:
名称
TemplateImage
在当前上下文中不存在
现在我已经在我的xaml中用此行声明了它:
<ff:CachedImage
Source="{Binding Source}"
VerticalOptions="Center"
HorizontalOptions="Fill"
x:Name="TemplateImage" />
现在如何在C#中更改CachedImage的大小?
HeightRequest
的CachedImage
属性绑定到XAML中]
<ff:CachedImage
Source="{Binding Source}"
HeightRequest="{Binding Source=MyViewModel Path=wishedHeight}"
VerticalOptions="Center"
HorizontalOptions="Fill" />
其中wishedHeight
是视图模型中的属性。注意,您不能直接使用
HeightRequest="{Binding wishedHeight}"
因为wishedHeight不是您的CachedImage绑定到的TemplateSource
类的一部分!