【问题标题】:Cannot Convert Lambda expression to type 'System.Type' because it is not a Delegate Type无法将 Lambda 表达式转换为类型“System.Type”,因为它不是委托类型
【发布时间】:2015-08-29 10:46:00
【问题描述】:

我在 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(() =&gt; {}) 结构,但由于这是 Xamarin 批准的方法,我不确定为什么需要实现它?

谢谢!

编辑:添加顶部 func 行后,我现在在该行上出现错误,Error CS1643: Not all code paths return a value in anonymous method of type 'System.Func&lt;object&gt;'

【问题讨论】:

  • 如果你把你的 lambda 放在 new Func&lt;object&gt;( ... ) 里面会怎样?
  • @cubrr 好的,我已将我的所有 ListView 代码包装在 new Func&lt;object&gt; (delegate { ...//all my code//...}) 中,并已从该错误中继续,但现在收到 Not all code paths return a value in anonymous method of type 'System.Func&lt;object&gt;' 错误?
  • 用代码更新你的问题。
  • @cubrr 请见上文
  • 啊,对不起,我的意思是,如果你可以添加你现在拥有的实际代码,就会给你错误。

标签: c# lambda xamarin


【解决方案1】:

DataTemplatehere 的构造函数中可以看出,您需要传递Func&lt;object&gt;Type

您需要在 statement block 中返回一些内容,以便将其转换为 Func&lt;object&gt;。由于您没有 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&lt;object&gt;(...): 的意思

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
        };
    }));
}

【讨论】:

  • 我的代码出现此错误也返回一个视图单元格,我将编辑我的问题以包含
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多