Xamarin.forms Xamarin:在xaml中可以看到在内部添加条件
我是xamarin的新手,我在xamarin表单项目中面临一个问题。 我有一个内部listview viewcell,宽度和高度为250。 有时mediaUrl的值为空。我想隐藏空mediaUrl值的图像,并使其他值可见。 我的问题是,如果mediaUrl的值为null,则在UI中显示空白。在isVisible属性中如何应用此条件? 我的代码如下:Xamarin.forms Xamarin:在xaml中可以看到在内部添加条件,xamarin.forms,Xamarin.forms,我是xamarin的新手,我在xamarin表单项目中面临一个问题。 我有一个内部listview viewcell,宽度和高度为250。 有时mediaUrl的值为空。我想隐藏空mediaUrl值的图像,并使其他值可见。 我的问题是,如果mediaUrl的值为null,则在UI中显示空白。在isVisible属性中如何应用此条件? 我的代码如下: <StackLayout> <ListView> <ListView.
<StackLayout>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image
WidthRequest="250"
HeightRequest="250"
Source="{Binding mediaUrl}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
任何人请建议一个解决方案与工作代码。
提前感谢您可以使用值转换器实现这一点 创建一个这样的转换器
public class NullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<ContentPage.Resources>
<ResourceDictionary>
<local:NullToBoolConverter x:Key="NullToBoolConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image
WidthRequest="250"
HeightRequest="250"
Source="{Binding mediaUrl}"
IsVisible={Binding mediaUrl, Converter={StaticResource NullToBoolConverter}/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
如果该值为null,则返回false
像这样在页面中注册它
public class NullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<ContentPage.Resources>
<ResourceDictionary>
<local:NullToBoolConverter x:Key="NullToBoolConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image
WidthRequest="250"
HeightRequest="250"
Source="{Binding mediaUrl}"
IsVisible={Binding mediaUrl, Converter={StaticResource NullToBoolConverter}/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
然后像这样添加它
public class NullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<ContentPage.Resources>
<ResourceDictionary>
<local:NullToBoolConverter x:Key="NullToBoolConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image
WidthRequest="250"
HeightRequest="250"
Source="{Binding mediaUrl}"
IsVisible={Binding mediaUrl, Converter={StaticResource NullToBoolConverter}/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
您也可以使用触发器进行此操作
<Image.Triggers>
<DataTrigger TargetType="Image" Binding="{Binding isMediaUrlNull}" Value="True">
<Setter Property="IsVisible" Value="False" />
</DataTrigger>
</Image.Triggers>
史蒂夫·查德伯恩的解决方案很好 您应该将转换器声明为:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SAMPLE.Sample">
<!--RESOURCES-->
<ContentPage.Resources>
<ResourceDictionary>
<local:NullToBoolConverter x:Key="NullToBoolConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<!-- CONTENT -->
<ContentPage.Content>
<ListView >
Use Converter
</ListView>
</ContentPage.Content>
使用转换器
当我添加xaml时,显示错误“local”是一个未声明的前缀。我在上面添加了listview.Apply触发器,获取错误:严重性代码描述项目文件行抑制状态错误位置91:98。在xmlns SmartTweet F:\BackUp-Copy\SmartTweet\SmartTweet\Pages\DashBoardPage.xaml 91中找不到NULL类型。当我向模型添加“public bool isMediaUrlNull{get{string.IsNullOrEmpty(mediaUrl)}”代码时,获取并非所有代码路径都返回值错误。mediaUrl字符串类型正确吗?抱歉,我的错误忘记了返回值。请参阅更新代码我忘记添加以前的空白区域,但如果mediaurl值不为null,则不会加载图像