XAML Xamarin窗体,将ViewModel参数传递给转换器

6uxekuva  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(168)

我正在尝试将视图模型内部定义的变量传递给自定义转换器。
这是xaml文件:

<ContentPage.Resources>
    <ResourceDictionary>
        <converter:IsLastItemConverter x:Key="lastItemConverter" Collection="{Binding Selfies}" />
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
    ....
    <local:Selfie IsLastItem="{Binding ID, Converter={StaticResource lastItemConverter}}" />
    ....
</ContentPage.Content>

这是xaml.cs背后的代码

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelfieWall : BaseContentPage
{
    public SelfieWall()
    {
        InitializeComponent();
        this.BindingContext = new SelfieWallViewModel();
    }
}

这是视图模型

public class SelfieWallViewModel : INotifyPropertyChanged
{
   ....
   public List<Model.Selfie> Selfies { get; set; }
   ....
}

这是转换器

public class IsLastItemConverter : IValueConverter
{
    public List<Selfie> Collection { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return true;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        return true;
    }
}

当我尝试加载上面xaml中定义的视图时,我得到了错误

System.NullReferenceException: Object reference not set to an instance of an object.

如果删除,错误将消失

Collection="{Binding Selfies}"

从XAML中删除。
有关如何将ViewModel的变量传递给转换器的任何提示。

wgeznvg7

wgeznvg71#

如果我没理解错的话,你可能想知道你在Selfies中的项目是否是最后一个。我认为你不能用IValueConverter来做这件事。我建议你在Model.Selfie中使用一个属性,每次你从集合中添加/删除一个项目时都要设置这个属性。

k3fezbri

k3fezbri2#

最新版本的Xamarin.Forms不支持转换器参数与ViewModel属性绑定。它只接受直接从UI/Xaml传递的值。
这里有一个解决方法。

public class BindablePropertyObject
{
    /// <summary>
    /// Populate converter parameter with this object.
    /// Pass element reference as SOURCE and binding PROPERTY NAME.
    /// Gets the binding context of SOURCE and property with the same name as PROPERTY NAME from the binding context.
    /// Gets value of the property fetched from binding context.
    /// Ex: Write in element ConverterParameter={utils:BindablePropertyObject PropertyName=property name, Source= {x:Reference element name}}
    /// </summary>
    /// <returns>Returns value of the property</returns>
    public object GetValueFromPropertyType()
    {
        if (Source.IsNotNull() && Source is View view && view.BindingContext.IsNotNull())
        {
            var property = view.BindingContext.GetType().GetProperty(PropertyName);
            if (property.IsNotNull())
            {
                return property.GetValue(view.BindingContext);
            }
        }
        return null;
    }

    /// <summary>
    /// Name of property which is binded to view 
    /// </summary>
    public string PropertyName { get; set; }

    /// <summary>
    /// XF element which will be used to get binding context.
    /// </summary>
    public object Source { get; set; }
}

使用它你可以在Xaml文件中创建一个对象,并传递Source和Property Name。GetValueFromPropertyType()然后将从Source.BindingContext中获取属性和它的值,因为Source的类型是XF. View。

相关问题