C# 为什么赢了';表单ListView数据绑定是否反映在其他视图中?
我的C# 为什么赢了';表单ListView数据绑定是否反映在其他视图中?,c#,xaml,listview,xamarin.forms,binding,C#,Xaml,Listview,Xamarin.forms,Binding,我的MainPage.xaml页面绑定到ClientsViewModel.cs。此页面有一个绑定到ObservableCollection属性的ListView NewClient.xaml页面和条目字段也绑定到ClientsViewModel.cs 当我使用NewClient.xaml表单保存新客户端并导航回MainPage.xaml(使用导航返回箭头)时,我希望在MainPage.xamlListView中看到新添加的客户端,但是我没有看到此更改 为什么MainPage.xaml中的List
MainPage.xaml
页面绑定到ClientsViewModel.cs
。此页面有一个绑定到ObservableCollection
属性的ListView
NewClient.xaml
页面和条目字段也绑定到ClientsViewModel.cs
当我使用NewClient.xaml
表单保存新客户端并导航回MainPage.xaml
(使用导航返回箭头)时,我希望在MainPage.xaml
ListView
中看到新添加的客户端,但是我没有看到此更改
为什么MainPage.xaml
中的ListView
没有显示最新更新的记录?我哪里做错了
值得一提的是,我的实际项目将使用SQLite,因此,ObseravbleCollection
最终将直接从SQLite数据库中获取记录,因此对此提供的任何帮助或建议也将不胜感激
请参阅下面的代码,或从我的GitHub存储库中克隆
(型号)Client.cs
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ClientViewModel : BaseViewModel
{
private ObservableCollection<Client> clients;
public ObservableCollection<Client> Clients
{
get { return clients; }
set
{
clients = value;
OnPropertyChanged();
}
}
public Command SaveClientCommand { get; }
public ClientViewModel()
{
this.Clients = new ObservableCollection<Client>();
SaveClientCommand = new Command(()=> {
Client client = new Client()
{
Name = Name,
Phone = Phone
};
Clients.Add(client);
OnPropertyChanged(nameof(Clients));
});
}
private int id;
public int Id
{
get { return id; }
set
{
id = value;
OnPropertyChanged();
}
}
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
private string phone;
public string Phone
{
get { return phone; }
set
{
phone = value;
OnPropertyChanged();
}
}
}
(ViewModel)BaseViewModel.cs
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ClientViewModel : BaseViewModel
{
private ObservableCollection<Client> clients;
public ObservableCollection<Client> Clients
{
get { return clients; }
set
{
clients = value;
OnPropertyChanged();
}
}
public Command SaveClientCommand { get; }
public ClientViewModel()
{
this.Clients = new ObservableCollection<Client>();
SaveClientCommand = new Command(()=> {
Client client = new Client()
{
Name = Name,
Phone = Phone
};
Clients.Add(client);
OnPropertyChanged(nameof(Clients));
});
}
private int id;
public int Id
{
get { return id; }
set
{
id = value;
OnPropertyChanged();
}
}
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
private string phone;
public string Phone
{
get { return phone; }
set
{
phone = value;
OnPropertyChanged();
}
}
}
(视图模型)ClientViewModel.cs
public class Client
{
public int Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ClientViewModel : BaseViewModel
{
private ObservableCollection<Client> clients;
public ObservableCollection<Client> Clients
{
get { return clients; }
set
{
clients = value;
OnPropertyChanged();
}
}
public Command SaveClientCommand { get; }
public ClientViewModel()
{
this.Clients = new ObservableCollection<Client>();
SaveClientCommand = new Command(()=> {
Client client = new Client()
{
Name = Name,
Phone = Phone
};
Clients.Add(client);
OnPropertyChanged(nameof(Clients));
});
}
private int id;
public int Id
{
get { return id; }
set
{
id = value;
OnPropertyChanged();
}
}
private string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
private string phone;
public string Phone
{
get { return phone; }
set
{
phone = value;
OnPropertyChanged();
}
}
}
公共类ClientViewModel:BaseViewModel
{
私人可观测收集客户;
公共可观测收集客户端
{
获取{返回客户端;}
设置
{
客户=价值;
OnPropertyChanged();
}
}
公共命令SaveClientCommand{get;}
公共ClientViewModel()
{
this.Clients=新的ObservableCollection();
SaveClientCommand=新命令(()=>{
客户端=新客户端()
{
Name=Name,
电话
};
客户。添加(客户);
OnPropertyChanged(客户名称);
});
}
私有int-id;
公共整数Id
{
获取{return id;}
设置
{
id=值;
OnPropertyChanged();
}
}
私有字符串名称;
公共字符串名
{
获取{返回名称;}
设置
{
名称=值;
OnPropertyChanged();
}
}
私人电话;
公用串电话
{
获取{返回电话;}
设置
{
电话=价值;
OnPropertyChanged();
}
}
}
(查看)主页.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:DataBinding.ViewModels"
x:Class="DataBinding.MainPage">
<ContentPage.BindingContext>
<viewModels:ClientViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="Client List"></Label>
<ListView ItemsSource="{Binding Clients}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Name}"/>
<Label Text="{Binding Phone}"/>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="Add Client"
Clicked="AddClientButton_Clicked"/>
</StackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:DataBinding.ViewModels"
x:Class="DataBinding.Views.NewClient">
<ContentPage.BindingContext>
<viewModels:ClientViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Label Text="Add New Client" />
<Label Text="Name"/>
<Entry Text="{Binding Name}"/>
<Label Text="Phone"/>
<Entry Text="{Binding Phone}"/>
<Button Text="Save"
Command="{Binding SaveClientCommand}"/>
<!-- Added ListView -->
<ListView ItemsSource="{Binding Clients}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"/>
<Label Text="{Binding Phone}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
(查看)NewClient.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:DataBinding.ViewModels"
x:Class="DataBinding.MainPage">
<ContentPage.BindingContext>
<viewModels:ClientViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="Client List"></Label>
<ListView ItemsSource="{Binding Clients}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Name}"/>
<Label Text="{Binding Phone}"/>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="Add Client"
Clicked="AddClientButton_Clicked"/>
</StackLayout>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:DataBinding.ViewModels"
x:Class="DataBinding.Views.NewClient">
<ContentPage.BindingContext>
<viewModels:ClientViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Label Text="Add New Client" />
<Label Text="Name"/>
<Entry Text="{Binding Name}"/>
<Label Text="Phone"/>
<Entry Text="{Binding Phone}"/>
<Button Text="Save"
Command="{Binding SaveClientCommand}"/>
<!-- Added ListView -->
<ListView ItemsSource="{Binding Clients}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"/>
<Label Text="{Binding Phone}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
我从回购协议下载了您的代码,我认为其中有一个很大的缺陷导致了这一点。您正在两个页面上用XAML设置
BindingContext
。如果在ClientViewModel
的构造函数中设置断点,您会注意到它会被调用两次:一次是在应用程序启动时,一次是在您单击“添加客户端”时
这意味着您正在查看该类的两个独立实例,因此您的客户机
处于错误的实例中。您需要确保查看的是同一视图模型
更重要的是,您甚至可能希望通过创建一个额外的,即:CreateClientViewModel
来更好地分离关注点,它只负责创建客户机并将该对象返回给ClientViewModel
,然后将该对象添加到集合中
希望这有帮助 我从repo下载了你的代码,我认为其中有一个很大的缺陷导致了这一点。您正在两个页面上用XAML设置
BindingContext
。如果在ClientViewModel
的构造函数中设置断点,您会注意到它会被调用两次:一次是在应用程序启动时,一次是在您单击“添加客户端”时
这意味着您正在查看该类的两个独立实例,因此您的客户机
处于错误的实例中。您需要确保查看的是同一视图模型
更重要的是,您甚至可能希望通过创建一个额外的,即:CreateClientViewModel
来更好地分离关注点,它只负责创建客户机并将该对象返回给ClientViewModel
,然后将该对象添加到集合中
希望这有帮助 根据您的描述,您希望在页面之间导航时传递数据,我建议您可以使用 主页:
<StackLayout>
<Label Text="Client List" />
<ListView ItemsSource="{Binding Clients}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}" />
<Label Text="{Binding Phone}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Command="{Binding SaveClientCommand}" Text="Add Client" />
</StackLayout>
public partial class Page9 : ContentPage
{
private ClientViewModel _clientmodel;
public ClientViewModel clientmodel
{
get { return _clientmodel; }
set
{
_clientmodel = value;
}
}
public Page9()
{
InitializeComponent();
this.BindingContext = new ClientViewModel(this.Navigation);
}
}
public class ClientViewModel
{
public ObservableCollection<Client> Clients { get; set; }
public Command SaveClientCommand { get; }
private INavigation _navigation;
public ClientViewModel(INavigation navitation)
{
Clients = new ObservableCollection<Client>();
Clients.Add(new Client() { Name = "client1", Phone = "123" });
_navigation = navitation;
SaveClientCommand = new Command(async() => {
await _navigation.PushAsync(new NewClient());
});
MessagingCenter.Subscribe<string, string[]>("test", "Add", (sender, values) =>
{
Client client = new Client() { Name=values[0],Phone=values[1]};
Clients.Add(client);
});
}
}
公共部分类第9页:内容页
{
PrivateClientViewModel\u clientmodel;
公共客户端视图模型客户端模型
{
获取{return\u clientmodel;}
设置
{
_clientmodel=价值;
}
}
公共页9()
{
初始化组件();
this.BindingContext=新的ClientViewModel(this.Navigation);
}
}
公共类ClientViewModel
{