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

时间:2015-08-29 10:46:01

标签: c# lambda xamarin

我正在使用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)
 }
};

取自this guide第13页。

但是,当我添加列表视图(详细here)时,

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批准的方法,我不确定为什么我需要实现它?

谢谢!

编辑:添加了顶部功能行,我现在收到了该行的错误Error CS1643: Not all code paths return a value in anonymous method of type 'System.Func<object>'

1 个答案:

答案 0 :(得分:2)

正如您从DataTemplate here的构造函数中看到的那样,您需要传递Func<object>Type

  

您需要在statement block内返回,以便将其转换为Func<object>。由于您没有return语句,因此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
        };
    }));
}