微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Xamarin 形式中,是否可以在列表视图之外进行数据绑定,就像在标签中一样?

如何解决在 Xamarin 形式中,是否可以在列表视图之外进行数据绑定,就像在标签中一样?

抱歉标题有点混乱。

我只想从带有模型的数据库获取我的信息。我知道如何在列表视图中执行此操作,但我似乎无法找到有关在列表视图之外执行此操作的信息。我知道你可以将东西绑定到标签,但它只是我的视图模型中的内容

我有任何意义吗?

谢谢。

更新:我想做这个列表视图

<ListView
        BackgroundColor="Transparent"
        CachingStrategy="RecycleElement"
        HasUnevenRows="True"
        IsPullToRefreshEnabled="True"
        IsRefreshing="{Binding IsBusy,Mode=OneWay}"
        ItemsSource="{Binding MyProfile}"
        RefreshCommand="{Binding RefreshCommand}"
        RefreshControlColor="Red"
        SelectionMode="None"
        SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="model:MyProfile">
                    <ViewCell>
                        <Grid Padding="10">
                            <Frame CornerRadius="20" HasShadow="True">
                                <StackLayout Orientation="Horizontal">

                                    <StackLayout VerticalOptions="Center">
                                        <Label
                                        FontSize="Large"
                                        Text="{Binding Name}"
                                        VerticalOptions="Center" />
                                        <Label
                                        FontSize="Large"
                                        Text="{Binding id}"
                                        VerticalOptions="Center" />
                                        <Label
                                        FontSize="Small"
                                        Text="{Binding PhoneNumber}"
                                        VerticalOptions="Center" />
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

在只有标签的堆栈布局中:

        <StackLayout BindableLayout.ItemsSource="{Binding MyProfile}"
             Orientation="Horizontal">
            <BindableLayout.ItemTemplate>
                <DataTemplate x:DataType="model:MyProfile">
                    <controls:CircleImage Source="{Binding}"
                                  Aspect="AspectFill"
                                  WidthRequest="44"
                                  HeightRequest="44"
                                  ... />
                    <Label Text="Enter profile"/>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>

我只想使用数据绑定更新标签而不是包含来自数据库的信息的列表。

解决方法

如果你想显示单个对象的数据,你可以这样做

                         <Frame CornerRadius="20" HasShadow="True">
                            <StackLayout Orientation="Horizontal">

                                <StackLayout VerticalOptions="Center">
                                    <Label
                                    FontSize="Large"
                                    Text="{Binding Name}"
                                    VerticalOptions="Center" />
                                    <Label
                                    FontSize="Large"
                                    Text="{Binding id}"
                                    VerticalOptions="Center" />
                                    <Label
                                    FontSize="Small"
                                    Text="{Binding PhoneNumber}"
                                    VerticalOptions="Center" />
                                </StackLayout>
                            </StackLayout>
                        </Frame>

并在您的页面背后的代码中

// where MyViewModel is a single instance of the model you want to display
BindingContext = MyViewModel;
,

但我似乎无法在列表视图之外执行此操作。我只想要一个简单的标签来显示来自数据库的特定信息。当我尝试将“itemsource”添加到 stacklayout 时,它说它不在它的属性中。

根据您的描述,我猜您想在ListView之外显示特定的一条记录。

ListView 是用于呈现数据列表的视图,尤其是需要滚动的长列表。

我做了一个示例,显示列表视图之外的列表视图选择的一条记录。

 <StackLayout>
        <StackLayout>
            <Image Source="{Binding selecteditem.source}" />
            <Label Text="{Binding selecteditem.Name}" />
        </StackLayout>
        <ListView
            HasUnevenRows="True"
            ItemsSource="{Binding MyProfile}"
            SelectedItem="{Binding selecteditem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Frame CornerRadius="20" HasShadow="True">
                                <StackLayout Orientation="Horizontal">

                                    <StackLayout VerticalOptions="Center">
                                        <Label
                                            FontSize="Large"
                                            Text="{Binding Name}"
                                            VerticalOptions="Center" />
                                        <Label
                                            FontSize="Large"
                                            Text="{Binding id}"
                                            VerticalOptions="Center" />
                                        <Label
                                            FontSize="Small"
                                            Text="{Binding PhoneNumber}"
                                            VerticalOptions="Center" />
                                    </StackLayout>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

 public partial class Page7 : ContentPage
{     
    public Page7()
    {
        InitializeComponent();                     
        this.BindingContext = new Profileviewmodel();
    }
           
}
public class Profileviewmodel:ViewModelBase
{
    public ObservableCollection<Profile> MyProfile { get; set; }
    private Profile _selecteditem;
    public Profile selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            RaisePropertyChanged("selecteditem");
        }
    }
    public Profileviewmodel()
    {
        MyProfile = new ObservableCollection<Profile>();
        getdata();
    }
    private void getdata()
    {
        for (int i = 0; i < 10; i++)
        {
            Profile profile = new Profile();
            profile.id = i.ToString();
            profile.Name = "the " + i + " profile";
            profile.PhoneNumber = "test " + i;
            profile.source = "c1.png";
            MyProfile.Add(profile);
        }
    }
}
public class Profile
{
    public string Name { get; set; }
    public string id { get; set; }
    public string PhoneNumber { get; set; }
    public string source { get; set; }
}

ViewModelBase 是实现 INotifyPropertyChanged 的​​类,用于在列表视图选择索引更改时更新 selecteditem 值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。