如何通过单击列表视图显示标签
How to display a label with click on listview
我想在列表视图中单击我的项目时显示标签。
真正的问题是我不知道如何 link 我的视图模型和我的视图
我想在视图模型中修改我的标签,但我不知道目前是否可行。
我的xaml:
<StackLayout>
<Label x:Name="labelperso"
Text="{Binding newProduct}"
IsVisible="{Binding Addproduct}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
BackgroundColor="#000000"
FontSize="20"
Opacity="0"/>
<ListView ItemsSource="{Binding Products}" CachingStrategy="RecycleElement" RowHeight="50" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding CodeReferenceLibelle}" TextColor="Black"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemSelected" Command="{Binding
SelectCommand}" Converter="{StaticResource SelectedItemConverter}"/>
</ListView.Behaviors>
我的视图模型:
#region labelperso property
private string _newProduct;
public string newProduct
{
get { return _newProduct; }
set { SetProperty(ref _newProduct, value); }
}
#endregion
#region Addproduct property
private bool _Addproduct;
public bool Addproduct
{
get { return _Addproduct; }
set { SetProperty(ref _Addproduct, value); }
}
#endregion
当我点击我的项目时:
async Task Select()
{
newProduct = "Produit ajouté !";
basketManager.AddProductSkuAsync(sku);
newProduct = "";
await Task.Run(() => ShowText());
}
//I have tried this but I can't use my label in my view
async Task ShowText()
{
await labelperso.FadeTo(1);
await Task.Delay(1000);
await labelperso.FadeTo(0);
}
您需要添加 SelectedProduct
属性 到您的 VM。
private string _SelectedProduct;
public string SelectedProduct
{
get { return _SelectedProduct; }
set { SetProperty(ref _SelectedProduct, value); }
}
然后您可以将 ListView 的 SelectedItem
绑定到它
<ListView ItemsSource="{Binding Products}"
SelectedItem="{Binding SelectedProduct}"
CachingStrategy="RecycleElement"
RowHeight="50" >
然后,您可以通过 "nullToVisibility" 转换器绑定到 SelectedProduct 或使用触发器等来控制标签的可见性。
为什么要在 VM 中使用标签 "labelperso"?您可以在 xaml.cs
中使用它。
您只需要像这样添加事件 ItemSelected
:
<ListView ItemsSource="{Binding Products}" ItemSelected="OnSelection">
在xaml.cs
void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
//suppose the binding Object is Product
Product product = (Product)e.SelectedItem;
//labelperso.Text = "name = " + product.Name;
labelperso.FadeTo(1);
Task.Delay(1000);
labelperso.FadeTo(0);
}
通常情况下,VM 与 Xaml 无关,我们不应该从 VM 获取标签。
如果必须,我们不推荐 it.But,您可以像这样从 xaml.cs 文件中传递标签:
你可以在你的页面定义一个变量。xaml.cs:
public Label pageLabel;
首字母是这样的:
pageLabel = labelperso;
BindingContext = new YourViewmodel(this);
在YourViewmodel.cs中:
public Label ss;
public YourViewmodel(ContentPage parentPage)
{// here HomePage is your contentPage name of the page`
ss = ((HomePage)parentPage).pageLabel;//after this you can use it
}
您应该尝试使用 MVVM 模式,而不是使用隐藏代码进行黑客攻击。
使用 MVVM,您可以将 Visible 属性 添加到视图模型并将标签的 IsVisible 属性 绑定到它。
代码将更易于阅读和维护。
我想在列表视图中单击我的项目时显示标签。 真正的问题是我不知道如何 link 我的视图模型和我的视图
我想在视图模型中修改我的标签,但我不知道目前是否可行。
我的xaml:
<StackLayout>
<Label x:Name="labelperso"
Text="{Binding newProduct}"
IsVisible="{Binding Addproduct}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
BackgroundColor="#000000"
FontSize="20"
Opacity="0"/>
<ListView ItemsSource="{Binding Products}" CachingStrategy="RecycleElement" RowHeight="50" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding CodeReferenceLibelle}" TextColor="Black"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemSelected" Command="{Binding
SelectCommand}" Converter="{StaticResource SelectedItemConverter}"/>
</ListView.Behaviors>
我的视图模型:
#region labelperso property
private string _newProduct;
public string newProduct
{
get { return _newProduct; }
set { SetProperty(ref _newProduct, value); }
}
#endregion
#region Addproduct property
private bool _Addproduct;
public bool Addproduct
{
get { return _Addproduct; }
set { SetProperty(ref _Addproduct, value); }
}
#endregion
当我点击我的项目时:
async Task Select()
{
newProduct = "Produit ajouté !";
basketManager.AddProductSkuAsync(sku);
newProduct = "";
await Task.Run(() => ShowText());
}
//I have tried this but I can't use my label in my view
async Task ShowText()
{
await labelperso.FadeTo(1);
await Task.Delay(1000);
await labelperso.FadeTo(0);
}
您需要添加 SelectedProduct
属性 到您的 VM。
private string _SelectedProduct;
public string SelectedProduct
{
get { return _SelectedProduct; }
set { SetProperty(ref _SelectedProduct, value); }
}
然后您可以将 ListView 的 SelectedItem
绑定到它
<ListView ItemsSource="{Binding Products}"
SelectedItem="{Binding SelectedProduct}"
CachingStrategy="RecycleElement"
RowHeight="50" >
然后,您可以通过 "nullToVisibility" 转换器绑定到 SelectedProduct 或使用触发器等来控制标签的可见性。
为什么要在 VM 中使用标签 "labelperso"?您可以在 xaml.cs
中使用它。
您只需要像这样添加事件 ItemSelected
:
<ListView ItemsSource="{Binding Products}" ItemSelected="OnSelection">
在xaml.cs
void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
//suppose the binding Object is Product
Product product = (Product)e.SelectedItem;
//labelperso.Text = "name = " + product.Name;
labelperso.FadeTo(1);
Task.Delay(1000);
labelperso.FadeTo(0);
}
通常情况下,VM 与 Xaml 无关,我们不应该从 VM 获取标签。 如果必须,我们不推荐 it.But,您可以像这样从 xaml.cs 文件中传递标签: 你可以在你的页面定义一个变量。xaml.cs:
public Label pageLabel;
首字母是这样的:
pageLabel = labelperso;
BindingContext = new YourViewmodel(this);
在YourViewmodel.cs中:
public Label ss;
public YourViewmodel(ContentPage parentPage)
{// here HomePage is your contentPage name of the page`
ss = ((HomePage)parentPage).pageLabel;//after this you can use it
}
您应该尝试使用 MVVM 模式,而不是使用隐藏代码进行黑客攻击。
使用 MVVM,您可以将 Visible 属性 添加到视图模型并将标签的 IsVisible 属性 绑定到它。 代码将更易于阅读和维护。