Warm tip: This article is reproduced from serverfault.com, please click

c#-找不到Xamarin绑定属性

(c# - Xamarin Binding property not found)

发布于 2020-11-28 20:50:25

我有这样定义的集合视图:

<RefreshView 
        Command="{Binding RefreshCommand}"
        IsRefreshing="{Binding IsRefreshing}">
        <CollectionView
            ItemsSource="{Binding Menus}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="32" />
                            <RowDefinition Height="128" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Image 
                            Source="{Binding ImageURI}"
                            HorizontalOptions="FillAndExpand"
                            VerticalOptions="FillAndExpand" 
                            Aspect="AspectFill"
                            Opacity="0.64"
                            Grid.RowSpan="2"
                            Grid.ColumnSpan="3"/>

                        <Label 
                            Text="{Binding Name}"
                            FontAttributes="Bold"
                            FontSize="Large"
                            TextColor="Black"
                            Grid.Row="0"
                            Grid.ColumnSpan="3"/>

                        <Label 
                            Text="{Binding Description}"
                            FontSize="Body" 
                            TextColor="Black"
                            Grid.Row="1"
                            Grid.ColumnSpan="3"/>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView>

但是,构建后,视图中什么都没有出现,并且控制台显示以下错误:

[0:] Binding: 'ImageURI' property not found on '<model>', target property: 'Xamarin.Forms.Image.Source'
[0:] Binding: 'Name' property not found on '<model>', target property: 'Xamarin.Forms.Label.Text'
[0:] Binding: 'Description' property not found on '<model>', target property: 'Xamarin.Forms.Label.Text'

当我将视图更改为:

        <Label Text="{Binding Menus.Count}"
               TextColor="Black"/>

        <Label Text="{Binding Menus[0]}"
               TextColor="Black"/>

        <Label Text="{Binding Menus[0].Name}"
               TextColor="Black"/>

结果看起来像这样,但仍然会产生相同的错误消息。

[0:] Binding: 'Name' property not found on 'hollywood.Models.MenuHandle', target property: 'Xamarin.Forms.Label.Text'

截屏

视图模型和模型分别如下所示:

public class MenuListViewModel : BaseViewModel
    {
        public MenuListViewModel()
        {
            //RefreshMenus();
            Menus.Add(new MenuHandle { Name = "Test", Description="test2"});
            Title = "Menu";
        }

        public async Task RefreshMenus()
        {
            IsRefreshing = true;
            TimeSpan age = DateTime.Now - MenusAge;
            if (age.TotalSeconds > 1)
            {
                try
                {
                    Menus = await App.ApiConnection.GetMenusAsync();
                    MenusAge = DateTime.Now;
                }
                catch { }
            }
            IsRefreshing = false;
        }

        ObservableCollection<MenuHandle> menus = new ObservableCollection<MenuHandle>();
        public ObservableCollection<MenuHandle> Menus
        {
            get { return menus; }
            private set { SetProperty(ref menus, value); }
        }

        bool isRefreshing;
        public bool IsRefreshing
        {
            get { return isRefreshing; }
            private set { SetProperty(ref isRefreshing, value); }
        }

        DateTime MenusAge = DateTime.MinValue;

        public ICommand RefreshCommand => new Command(async () => await RefreshMenus());
    }
}

型号类别:

public class MenuHandle
    {
        [JsonProperty("name")]
        public string Name;
        [JsonProperty("url_name")]
        public string URLName;
        [JsonProperty("description")]
        public string Description;
        [JsonProperty("image")]
        public Uri ImageURI;
    }

任何建议将不胜感激。

Questioner
Jex__y
Viewed
0
Cfun 2020-11-29 06:01:02

你需要将变量声明为属性,因为绑定适用于属性:

public class MenuHandle
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("url_name")]
        public string URLName  { get; set; }
        [JsonProperty("description")]
        public string Description  { get; set; }
        [JsonProperty("image")]
        public Uri ImageURI  { get; set; }
    }