【问题标题】:Xamarin : Adding condition inside isvisible in xamlXamarin:在 xaml 中添加条件是可见的
【发布时间】:2017-11-17 05:37:47
【问题描述】:

我是 xamarin 的新手,我在我的 xamarin 表单项目中遇到问题。 我有一个内部 listview-viewcell,宽度和高度为 250。 有时 mediaUrl 的值为空。我想隐藏空 mediaUrl 值的图像并使其对其他值可见。 我的问题是如果 mediaUrl 的值为 null,则在 UI 中显示空白。在 isVisible 属性中,我该如何应用这个条件? 我的代码如下:

    <StackLayout>
        <ListView>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                    <StackLayout>
                        <Image
                            WidthRequest="250"
                            HeightRequest="250"
                            Source="{Binding mediaUrl}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>           
    </StackLayout>

请任何人提出一个带有工作代码的解决方案。 在此先感谢

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    您可以使用值转换器实现此目的

    创建一个这样的转换器

    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();
        }
    }
    

    如果值为 null,则返回 false

    像这样在您的页面中注册它

    <ContentPage
       xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:yourNameSpaceToWhereYourConverteClassIs"
       x:Class="yourNameSpace.Views.MyPage">
    <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>
    

    【讨论】:

    • 当我在 xaml 中添加 时,显示错误“本地”是一个未声明的前缀。我在 listview 的上方添加了
    【解决方案2】:

    您也可以为此使用触发器

    <Image.Triggers>
         <DataTrigger TargetType="Image" Binding="{Binding isMediaUrlNull}" Value="True">
            <Setter Property="IsVisible" Value="False" />
          </DataTrigger>
     </Image.Triggers>
    

    编辑

    您也可以在模型isMediaUrlNull 中添加属性并尝试上面的代码

    public bool isMediaUrlNull {get {return !string.IsNullOrEmpty(mediaUrl);}}
    
    <Image WidthRequest="250" HeightRequest="250" IsVisible="{Binding mediaUrl}" Source="{Binding mediaUrl}"/>
    

    【讨论】:

    • 应用触发器,出现错误:严重性代码描述项目文件行抑制状态错误位置 91:98。在 xmlns schemas.microsoft.com/winfx/2009/xaml SmartTweet F:\BackUp - Copy\SmartTweet\SmartTweet\Pages\DashBoardPage.xaml 91 中找不到类型 NULL
    • 当我添加“public bool isMediaUrlNull { get { string.IsNullOrEmpty(mediaUrl)} }”代码来建模时,并非所有代码路径都返回值错误。
    • 是 mediaUrl 字符串类型吗?
    • 抱歉我的错误忘记放回。查看更新代码
    • 我忘记在 之前添加 现在空格被隐藏,但如果 mediaurl 值不为空,则不会加载图像
    【解决方案3】:

    Steve Chadbourne 的解决方案很好。

    你应该这样声明转换器:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2016-12-12
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多