Xamarin.Forms、XAML、绑定和 isVisible [英] Xamarin.Forms, XAML, Bindings and isVisible
问题描述
我(显然,关于这个问题)对此很陌生,所以在此先感谢您的帮助!由于我现在在这方面努力了几个小时,我希望有人可以加快我的学习曲线 :)
I am (obviously, regarding the question) new to this, so thanks in advance for any help! As I am trying my current best on this for hours now, I hope, someone can accelerate my learning curve :)
附录:我也在 Xamarin 论坛上发帖,但是关于问题的答案数量,这似乎已经死了.所以我希望没有人冒犯.
Addendum: I also posted on the Xamarin Forum, however this seems to be rather dead regarding the amount of answers to questions. Therefore I hope no one takes offense.
所以,我有我的 XAML MainPage:
So, I have my XAML MainPage:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="cant post links"
xmlns:x="cant post links"
xmlns:local="clr-namespace:App.ViewModels"
x:Class="App.ViewModels.MainPage">
<ContentPage.BindingContext>
<local:User/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="Test"/>
<ListView ItemsSource="{Binding Changes}"
x:Name="ChangesList"
HasUnevenRows="True"
Margin="40,80"
ItemTapped="ListView_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"/>
<Button Text="Details"
IsVisible="{Binding IsVisible}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Text="Press to add" x:Name="addItem" Clicked="addItem_Clicked"/>
</StackLayout>
</ContentPage>
我有一个带有相应属性的数据模型.当我离开 MainPage 类构造函数时,如下所示:
I have a datamodel with the correponding attributes behind. When I leave the MainPage class constructor like the following:
public MainPage()
{
InitializeComponent();
}
我看不到任何列表项.
那么,第一个问题:我想,基于绑定,应用程序会自动生成一个 User 实例并将其分配到列表中?
正如我在调试器中看到的那样,实际上生成了一个 User 实例并将其分配给了 BindingContext,但 ChangesList.ItemsSource 仍然为空.
As I can see with the debugger, a User instance actually is generated and also assigned to the BindingContext, but the ChangesList.ItemsSource remains null.
因此,我更改了构造函数的代码:
Therefore, I changed the code of the constructor:
public MainPage()
{
InitializeComponent();
var bc = BindingContext;
var list = ChangesList.ItemsSource;
ChangesList.ItemsSource = (bc as User).Changes;
}
这让我可以在编译和运行应用程序时看到一个包含项目的列表.此外,我可以通过addItem"按钮后面的代码将新项目添加到列表中.但是:当我点击一个项目时,没有任何反应.代码如下所示:
This lets me see a list with items when I compile and run the app. Also, I can add new items to the list via the code behind the 'addItem' Button. However: Nothing happens, when I tap an item. The code looks like follows:
private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
var change = e.Item as Change;
change.IsVisible = !change.IsVisible;
}
Data 方法如下所示:
and the Data methods look as follows:
public class Change : INotifyPropertyChanged
{
public string Name { get; set; }
bool isVisible = false;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsVisible
{
get
{
return isVisible;
}
set
{
isVisible = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Change-IsVisible"));
}
}
}
因此第二个问题:为什么 Button 不改变其可见性状态并变为可见?
在此先非常感谢您!
狼
添加了完整的更改"类代码.
Added the complete "Change" Class Code.
编辑 2:用户代码.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.ComponentModel;
using Xamarin.Forms;
using System.Diagnostics;
namespace App.ViewModels
{
public class User : INotifyPropertyChanged
{
public string Username { get; set; }
public ObservableCollection<Change> Changes = null;
public event PropertyChangedEventHandler PropertyChanged;
public User()
{
Changes = new ObservableCollection<Change>();
addChange("Test 1");
addChange("Test 2");
}
public void addChange(string text)
{
Change c = new Change
{
Name = text,
IsVisible = false
};
Changes.Add(c);
refresh();
}
public void refresh()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Changes"));
}
}
}
推荐答案
对于第二个问题 - 尝试:
For the 2nd question - try:
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsVisible"));
对于第一个问题 - 您能否分享您的用户源代码
For the first question - can you please share your User source code
这篇关于Xamarin.Forms、XAML、绑定和 isVisible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!