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

In Xamarin form, is it possible to do a databinding outside a listview, like just in a label?

抱歉,标题有点混乱。

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

我说的有道理吗?

谢谢。

更新:我想做类似列表视图的事情

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

But i cant seem to do it outside the listview. I just want a simple label to show the specific info from the DB. When I try to add "itemsource" to a stacklayout" It says Its not in its property.

根据你的描述,我猜你想在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 的​​类,用于在列表视图选定索引更改时更新选定项值。