Xamarin表单自定义视图,可绑定的可观察集合抛出“特定类型的转换无效”

问题描述 投票:0回答:1

我正在xamarin Forms中创建具有一组Bindable属性的自定义视图,包括我自己的模型的Bindable observable collections类型。这些可观察的集合用作自定义视图的数据源,并实时更新。一切都很好,直到添加了不同类型的模型并开始了代码重构。

自定义视图支持的模型集很少,并且在任何时间点,任何模型类型只能有一个可观察的集合。最初,我为每种类型使用不同的属性进行管理,然后为了使其更通用,我尝试使用单个属性进行处理。那是我替换的时间

在CustomView.cs中

ObservableCollection<ModelA> ADataSource ObservableCollection<ModelB> BDataSource

with

ObservableCollection<object> DataSource

在ViewModel中,我有

ObservableCollection<ModelA> AList

现在,当应用程序运行时,自定义视图会为通用的可观察集合抛出错误“特定类型的转换无效”。在Property_Changing事件处理程序中进行调试时,我可以通过快速监视将newValue投射到Observable Collection。

如何处理此问题并使通用的可观察集合属性支持多类型模型。

预先感谢

generics xamarin.forms observablecollection custom-view bindableproperty
1个回答
0
投票

如果您想在ContentView中使用ObservableCollection modelbs可绑定属性,然后在ContentPage中使用此自定义控件,我做了一个示例,您可以看一下:

ContentView:

<ContentView
x:Class="demo3.contentview.View2"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="customlistview"
mc:Ignorable="d">
<ContentView.Content>
    <StackLayout>
        <ListView x:Name="listview1" ItemsSource="{Binding Items, Source={x:Reference customlistview}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding name}" />
                            <Label Text="{Binding age}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentView.Content>

 public partial class View2 : ContentView
{
    public static  BindableProperty ItemsProperty = BindableProperty.Create("ItemsSource", typeof(ObservableCollection<object>), typeof(View2),null,BindingMode.TwoWay,propertyChanged: (bindable, oldValue, newValue) => OnItemsSourceChanged(bindable, oldValue, newValue));

    private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control =(View2)bindable;
        control.listview1.ItemsSource =(ObservableCollection<object>) newValue;
    }
    /// <summary>
    /// Accessors
    /// </summary>
    public ObservableCollection<object> Items
    {
        get { return (ObservableCollection<object>)GetValue(ItemsProperty); }
        set
        {
            SetValue(ItemsProperty, value);
        }
    }
    public View2()
    {
        InitializeComponent();

    }
}

ContentPage:

 <ContentPage.Content>
    <StackLayout>
        <local:View2 Items="{Binding modelas}" />

    </StackLayout>
</ContentPage.Content>

public partial class Page26 : ContentPage
{
    public ObservableCollection<object> modelas {get;set;}
    public ObservableCollection<object> modelbs { get; set; }
    public Page26()
    {
        InitializeComponent();
        modelas = new ObservableCollection<object>()
        {
            new modela(){name="cherry",age=12},
            new modela(){name="barry",age=20}
        };

        modelbs = new ObservableCollection<object>()
        {
            new modelb(){firstname="aaa",age=12},
            new modelb(){firstname="bbb",age=27}
        };
        this.BindingContext = this;

    }
}

public class modela
{
    public string name { get; set; }
    public int age { get; set; }
}

public class modelb
{
    public string firstname { get; set; }
    public int age { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.