How to binding more than one string in a frame, using ObservableCollection

lynxAi

Member
Joined
Dec 7, 2019
Messages
15
Location
SiChuan China
Programming Experience
Beginner
model:

model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PasswordSaver.Models
{
    public class info
    {
        public static string[] Text { get; set; } = new string[2];
    }
}

ViewModel:

ViewModel:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace PasswordSaver.ViewModels
{
    public partial class MainPageModel : ObservableObject
    {
        public MainPageModel()
        {
            Items = new ObservableCollection<string[]>();
        }

        public string Website
        {
            get => Models.info.Text[0];
            set
            {
                Models.info.Text[0] = value;
            }
        }
        public string Password
        {
            get => Models.info.Text[1];
            set
            {
                Models.info.Text[1] = value;
            }
        }
        [ObservableProperty]
        ObservableCollection<string[]> items;

        [RelayCommand]
        void Add()
        {

            if (string.IsNullOrWhiteSpace(Website) || string.IsNullOrWhiteSpace(Password))
            {
                return;
            }         
            Items.Add(Models.info.Text);
        }
        [RelayCommand]
        void Delete(string[] x)
        {
            if (Items.Contains(x))
            {
                Items.Remove(x);
            }
        }
    }
}

View:
View:
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PasswordSaver.MainPage"
       x:DataType="viewmodels:MainPageModel"
       xmlns:viewmodels ="clr-namespace:PasswordSaver.ViewModels"
       xmlns:models ="clr-namespace:PasswordSaver.Models"
       Shell.NavBarIsVisible="False">

    <TabBar>
        <ContentPage Title="主页" >
            <ScrollView>
                <StackLayout Padding="10" >
                    <Grid RowDefinitions="*,*,*,*" RowSpacing ="10">
                        <Label Text="Demo_PasswordSS" HorizontalOptions="Center" FontSize="Title"/>
                        <Entry Text="{Binding Website}" Grid.Row="1" BackgroundColor="Orange"/>
                        <Entry Text="{Binding Password}" Grid.Row="2" BackgroundColor="Orange" />
                        <Button Text="添加" Grid.Row="3" Command="{Binding AddCommand}" />
                    </Grid>
                    <CollectionView ItemsSource="{Binding Items}" >
                        <CollectionView.ItemTemplate>
                            <DataTemplate x:DataType="models:info">
                                <SwipeView >
                                    <SwipeView.RightItems>
                                        <SwipeItems >
                                            <SwipeItem Text="删除" BackgroundColor="Red" />
                                        </SwipeItems>
                                    </SwipeView.RightItems>
                                    <Frame Background="gray">
                                        <Grid RowDefinitions="*,*" ColumnDefinitions="*,*" Padding="10" >
                                            <Label Text="websites" FontSize="Body" TextColor="Blue" Grid.Row="0" Grid.Column="0" />
                                            <Label Text="password" FontSize="Body" TextColor="Black" Grid.Row="0" Grid.Column="3" />
                                            <Label x:Name="web" Text="{Binding Text[0]}"  FontSize="Body" Grid.Row="1" Grid.Column="0" />
                                            <Label x:Name="Pwd" Text="{Binding Text[1]}"  FontSize="Body" Grid.Row="1" Grid.Column="3"  />
                                        </Grid>
                                    </Frame>
                                </SwipeView>
                            </DataTemplate>
                    </CollectionView.ItemTemplate>
                    </CollectionView>
                </StackLayout>
            </ScrollView>
        </ContentPage>
        <ContentPage Title="设置">
            <Grid>
                <Label Text="keep continue" HorizontalOptions="Center" VerticalOptions="Center" FontSize="30"/>
            </Grid>
        </ContentPage>
    </TabBar>
</Shell>

捕获.PNG
 
Why not simply bind to Website and Password so you already have those exposed as public. Why break "The Law of Demeter" to go rooting around in an array? What value does the array give you?
 
Why not simply bind to Website and Password so you already have those exposed as public. Why break "The Law of Demeter" to go rooting around in an array? What value does the array give you?
xaml:
<Label x:Name="web" Text="{Binding Website}"  FontSize="Body" Grid.Row="1" Grid.Column="0" />
<Label x:Name="Pwd" Text="{Binding Password}"  FontSize="Body" Grid.Row="1" Grid.Column="3"  />

The two labels still don't show up, although I'm sure when debugging that they are indeed getting data.
 
Is the getter being called for those?
 

Latest posts

Back
Top Bottom