【问题标题】:Xamarin Forms Change Label from CollectionView in EventHandlerXamarin 表单从 EventHandler 中的 CollectionView 更改标签
【发布时间】:2020-01-31 10:16:06
【问题描述】:

我有问题。我在每行和一个按钮上创建了一个带有标签作为计数器的 CollectionView。我想要的是,当我按下按钮时,标签值将增加 +1,具体取决于您单击的行。我已经创建了这样的按钮事件:

private void btnPlus_Clicked(object sender, EventArgs e)
{
    int currentValue = Convert.ToInt32(txtCounter.Text);
    txtCounter.Text = (currentValue + 1).ToString();
}

这是我的 xaml:

<CollectionView ItemsSource="{Binding imageList}">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout HorizontalOptions="Center" 
                <Label Text="1" TextColor="Black" x:Name="txtCounter" FontAttributes="Bold" VerticalOptions="Center"/>
                <Button CornerRadius="13" WidthRequest="25" Text="+" Padding="0"
                        HeightRequest="25" VerticalOptions="Center" Clicked="btnPlus_Clicked" />
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

但是txtCounter 没有被识别!?

我做错了什么?

【问题讨论】:

  • DataTemplate 中的项目不能与 `x:Name 一起使用,您需要添加绑定以使其更容易
  • 问题解决了吗?

标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

由于您使用过 MVVM,您最好在 ViewModel 中处理逻辑。

在 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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Name="MyPage"  //set name of content page
             x:Class="xxx.MainPage">
<StackLayout HorizontalOptions="Center" 
      <Label Text="{Binding Count}" TextColor="Black" x:Name="txtCounter" FontAttributes="Bold" VerticalOptions="Center"/>
      <Button CornerRadius="13" WidthRequest="25" Text="+" Padding="0" HeightRequest="25" VerticalOptions="Center" Command="{Binding AddCommand}" CommandParameter="{Binding }"/>
</StackLayout>

在模型中

public class MyImage : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string count ;
        public string Count
        {
            set
            {
                if (count != value)
                {
                    count = value;
                    NotifyPropertyChanged("Count");
                }
            }
            get { return count; }
        }
    }

  // ... other property

在视图模型中

public VM_CounterList()
{
  
   imageList= new ObservableCollection<MyImage>() {

     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
                     
   };

   AddCommand = new Command((obj)=> {

      var myimage = obj as MyImage;

      int currentValue = Convert.ToInt32(myimage.Count);
      myimage.Count = (currentValue + 1).ToString();

   });
}

【讨论】:

    【解决方案2】:

    x:Name 不在您想要的上下文中。 要将模板中的属性绑定到 XAML.cs Back Code 上的某些内容,请使用:

    PropertyOnTemplate="{Binding Path=NameOfProperty, Source={x:Reference Name=NameOfFile}}
    

    然后,每当您更新属性 NameOfProperty 时,UI 都会相应更新

    由于您有一个列表,请保留一个计数器列表,以便您可以更新由列表中所选项目索引的正确计数器

    【讨论】:

    • 什么是文件名?因为我的ViewModel叫VM_CounterList,但是我想改成CounterList.xaml.cs。能否请您为我的情况举个例子,以便我理解您的意思?
    • 它可能看起来像这样PropertyOnTemplate="{Binding Path=txtCounter, Source={x:Reference Name=CounterListViewModel}}"@A.Vreeswijk 和@Andre-Motta
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多