所以我得到了五张图片,当你点击其中一张图片时,我希望那张图片获得完全不透明度,而另一张图片只获得一半,以显示它是所选图片。
我设法使用此方法执行此操作,但这不起作用,因为我不允许在MVVM中引用该视图。
我想我必须使用转换器来获得不透明度,并将图像作为命令参数发送?我之前使用过转换器,但从未自己做过,所以我不确定该做什么,第一次尝试使用Mvvm。
public void OnStatusTapped(object sender, EventArgs args)
{
statusUnResolved.Opacity = 0.5;
statusInProgress.Opacity = 0.5;
statusDone.Opacity = 0.5;
var image = (Image)sender;
image.Opacity = 1;
String[] buttons = new String[StatusValues.Count];
for (int n = 0; n < StatusValues.Count; ++n)
{
buttons[n] = StatusValues[n].Name;
}
if (image.Source is FileImageSource)
{
FileImageSource fileImageSource = (FileImageSource)image.Source;
string fileName = fileImageSource.File;
foreach (var item in StatusValues)
{
if (item.Name == fileName)
{
Issue.StatusEx = item.Value;
StatusChecker();
return;
}
}
}
}
private readonly ICommand onStatusTappedCommand = null;
public ICommand OnStatusTappedCommand
{
get { return onStatusTappedCommand ?? new Command(OnStatusTapped); }
}
<StackLayout Grid.Row="3" Grid.Column="1" Orientation="Horizontal" Spacing="0" >
<Image x:Name="statusUnResolved" Source="statusUnresolved.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="0.6">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnStatusTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<StackLayout Grid.Row="3" Grid.Column="2" Orientation="Horizontal" Spacing="4">
<Image x:Name="statusInProgress" Source="statusInProgress.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="0.6" >
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnStatusTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<StackLayout Grid.Row="3" Grid.Column="3" Orientation="Horizontal" Spacing="4" >
<Image x:Name="statusDone" Source="statusDone.png" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="40" Opacity="1">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnStatusTapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
</Grid>
答案 0 :(得分:1)
假设您将始终在屏幕上显示这5个特定图像,您可以在ViewModel中创建五个double
属性(每个图像一个),即:
public double StatusUnresolvedOpacity
{
get => _statusUnresolvedOpacity;
set
{
if (_statusUnresolvedOpacity != value)
{
_statusUnresolvedOpacity = value;
OnPropertyChanged(nameof(StatusUnresolvedOpacity));
}
}
}
并且还有另一种方法可以将每个的不透明度重置为0.5,即
public void ResetOpacities()
{
StatusUnresolvedOpacity = 0.5;
StatusInProgressOpacity = 0.5;
...
}
然后为每个图像提供一个轻击手势识别器,它将调用ResetOpacities()
,然后直接设置单击的按钮的视图模型属性为1.0。即:
private void OnStatusUnresolvedTapped(object sender, EventArgs e)
{
myViewModel.ResetOpacities();
myViewModel.StatusUnresolvedOpacity = 1.0;
}
如果您确实想要使用值转换器,我建议您创建五个bool
属性而不是double
属性,即:
public bool IsStatusUnresolvedActive { get ... set ... }
现在,您可以将活动按钮的属性设置为true
,而将不活动按钮的属性设置为false
,而不是重置/设置不透明度。到public class ActiveOpacityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? 1.0 : 0.5;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
。然后在你的价值转换器中:
<ContentPage x:converters="clr-namespace:MyProject.WhateverFolderValueConverersAreIn;assembly:MyProject" />
<ContentPage.Resources>
<ResourceDictionary>
<converters:ActiveOpacityConverter x:Key="activeOpacityConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Image Opacity={Binding IsStatusUnresolvedActive,
Converter={converters:activeOpacityConverter}}
</ContentPage>
并在你的xaml中使用:
input