如何将绑定属性xaml修复为代码?

时间:2019-05-07 10:05:16

标签: c# xaml xamarin.forms

我正在尝试使用绑定来显示参展商的所有类别,这些类别存储在列表中(ExtendedExhibitor.categories)。

我在ViewModel中创建了一个属性,以遍历所有类别并打印它们的名称。从调试看来,程序没有达到属性,所以也许我在绑定中犯了一个错误。

以下是重要的代码段:

DetailPage.xaml

<?php

$x= true and false;

var_export($x);

?>

DetailPageViewModel.cs

<StackLayout Orientation="Horizontal" IsVisible="{Binding DetailPageViewModel.ExtExhSector, Converter={StaticResource StringNullOrEmptyBoolConverter}}">
  <Label Text="Settore: " TextColor="Blue"/>
  <Label Text="{Binding DetailPageViewModel.ExtExhSector}"/>
</StackLayout>

我希望看到带有类别名称的串联字符串。

现在我只看到“ Settore:”,绑定的结果是空白。

您可以在照片中看到结果: Result

1 个答案:

答案 0 :(得分:0)

根据您的代码,您想要实现两个主要功能

  • 将属性xml绑定到代码。

  • 值转换器:如果ExtExhSector为空或为空,则StackLayout将设置为不可见。

首先,我们实现了将属性xml绑定到代码

1。创建一个模块ExtendedExhibitor

public class ExtendedExhibitor
{
  //  string[] categories;you can add any more attributes
    public string[] categories { get; set; }
}

2。创建viewModule DetailPageViewModel,我注意到您直接使用了ExtExhSector,因此ExtExhSector无法初始化,您将获得空值。

   public class DetailPageViewModel:INotifyPropertyChanged
{

    string extExhSector;
    private ExtendedExhibitor extendedExhibitor;

    public DetailPageViewModel(ExtendedExhibitor extendedExhibitor)
    {
        this.extendedExhibitor = extendedExhibitor;
    }

    public string ExtExhSector
    {
        get
        {
            for (int i = 0; i < extendedExhibitor.categories.Length; i++)
                extExhSector += extendedExhibitor.categories[i].ToString(); ;
            return extExhSector;
        }
        set
        {
            extExhSector = value;
            PropertyChanged?.Invoke(this, new 
         PropertyChangedEventArgs(nameof(ExtExhSector)));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

3。创建xaml布局。

 <StackLayout Orientation="Horizontal" IsVisible="{Binding ExtExhSector, Converter= 
 {StaticResource nullToVisibilityConverter}}">
    <Label Text="Settore: " TextColor="Blue"/>
    <Label Text="{Binding ExtExhSector}"/>
 </StackLayout>

4。初始化模块并将视图模块绑定到xml。

    public partial class MainPage : ContentPage
   {
    public MainPage()
    {
        InitializeComponent();


        var extendedExhibitor=new ExtendedExhibitor();
        string[] test = {"abc","def","hij","k" };
       // string[] test = { };
        extendedExhibitor.categories = test;

        BindingContext = new DetailPageViewModel(extendedExhibitor);
    }
}

然后,我们实现了价值转换器

1。创建一个转换器类,并在转换器中添加要处理的逻辑

  public class StringNullOrEmptyBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {


            return !string.IsNullOrEmpty($"{value}");


    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
            throw new NotImplementedException();
    }
}
  1. 将转换器添加到您的XAML。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:GirdViewDemo"
             x:Class="GirdViewDemo.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:StringNullOrEmptyBoolConverter x:Key="nullToVisibilityConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
  
    <StackLayout Orientation="Horizontal" IsVisible="{Binding ExtExhSector, Converter={StaticResource nullToVisibilityConverter}}">
        <Label Text="Settore: " TextColor="Blue"/>
        <Label Text="{Binding ExtExhSector}"/>
    </StackLayout>

</ContentPage>

有正在运行的屏幕截图。

enter image description here

如果您不熟悉值转换器,可以参考此链接。

https://xamgirl.com/understanding-converters-in-xamarin-forms/

有我的演示

https://github.com/851265601/DataBindingAndValueConverter