无法将 Lambda 表达式转换为类型“System.Type”,因为它不是委托类型。

3

我正在使用Xamarin.Forms,尝试创建一个包含ListView的弹出窗口。

我正在尝试使用以下结构:

var PopUp = new StackLayout
{
 BackgroundColor = Color.Black, // for Android and WP
 Orientation = StackOrientation.Vertical,
 Children =
 {
    PLZOrt_Label, // my Label on top
    SearchBarPLZOrt, // my SearchBar to the ListView
    LVPLZOrt, // The Listview (all Cities/Zip-Codes in the Datasource -> List)
 }
};

取自这篇指南第13页。

然而,当我添加一个列表视图(如此处所述),

new Func<object> (delegate {
    ListView listView = new ListView {
    // Source of data items.
    ItemsSource = devices,

    ItemTemplate = new DataTemplate(() =>
    {
            // Create views with bindings for displaying each property.
            Label nameLabel = new Label();
            nameLabel.SetBinding(
            Label.TextProperty, "{Binding Name, Converter={StaticResource strConverter}}"
            );

            Label IDLabel = new Label();
            IDLabel.SetBinding(
                Label.TextProperty, "{Binding Path=ID, Converter={StaticResource guidConverter}}"
                );

                return new ViewCell
                    {
                        View = new StackLayout
                    {
    })
});

"ItemTemplate"这行代码报错:"无法将 Lambda 表达式转换为类型 'System.Type',因为它不是委托类型"
在其他一些类似的问题中,解决方法似乎是添加一个new action(() => {})结构,但由于这是 Xamarin 批准的方法,所以我不确定为什么需要实现这个方法?
谢谢!
编辑: 添加了顶部func行之后,我现在得到了一个错误,该错误引发在该行上:Error CS1643: Not all code paths return a value in anonymous method of type 'System.Func<object>'

如果你把lambda表达式放在new Func<object>(...)里面会怎样? - cbr
@cubrr 好的,我已经用 new Func<object> (delegate { ...//所有我的代码//...}) 包装了我的所有 ListView 代码,并且已经解决了那个错误,但现在出现了一个 Not all code paths return a value in anonymous method of type 'System.Func<object>' 错误? - George Edwards
请在您的问题中更新代码。 - cbr
@cubrr 请参见上文 - George Edwards
啊,抱歉,我的意思是如果您能够添加当前导致错误的实际代码。 - cbr
我更新了我的回答,加入了我想表达的意思。 - cbr
1个回答

2
这里的构造函数可以看出,你需要传递一个Func<object>Type

你需要在语句块中返回一些内容,以便将其转换为Func<object>。由于你没有返回语句,lambda表达式没有被转换,编译器认为你尝试使用错误的参数来使用DataTemplate(Type)构造函数。我不知道为什么C#编译器会选择这个构造函数-我猜测它是找到的第一个构造函数。
文档页面上的示例ListView可以正常工作,因为它返回一个新的ViewCell
// ...
ItemTemplate = new DataTemplate(() =>
{
    // Create views with bindings for displaying each property.
    Label nameLabel = new Label();
    nameLabel.SetBinding(Label.TextProperty, "Name");

    Label birthdayLabel = new Label();
    birthdayLabel.SetBinding(Label.TextProperty,
        new Binding("Birthday", BindingMode.OneWay, 
                    null, null, "Born {0:d}"));

    BoxView boxView = new BoxView();
    boxView.SetBinding(BoxView.ColorProperty, "FavoriteColor");

    // Return an assembled ViewCell.  <<--------------------------------------
    return new ViewCell
    {
        View = new StackLayout
        {
            Padding = new Thickness(0, 5),
            Orientation = StackOrientation.Horizontal,
            Children = 
            {
                boxView,
                new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Spacing = 0,
                    Children = 
                    {
                        nameLabel,
                        birthdayLabel
                    }
                }
            }
        }
    };
})
// ...

编辑:我所指的是将lambda表达式放在一个new Func<object>(...)中。
ListView listView = new ListView
{
    // Source of data items.
    ItemsSource = devices,

    ItemTemplate = new DataTemplate(new Func<object>(() =>
    {
        // Create views with bindings for displaying each property.
        Label nameLabel = new Label();
        nameLabel.SetBinding(
            Label.TextProperty, "{Binding Name, Converter={StaticResource strConverter}}"
        );

        Label IDLabel = new Label();
        IDLabel.SetBinding(
            Label.TextProperty, "{Binding Path=ID, Converter={StaticResource guidConverter}}"
        );

        return new ViewCell
        {
            View = new StackLayout
        };
    }));
}

我的代码出现了这个错误,但它仍然返回一个视图单元,我将编辑我的问题以包含它。 - George Edwards

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接