【问题标题】:Change Label Value on button click in Xamarin MVVM在 Xamarin MVVM 中单击按钮时更改标签值
【发布时间】:2016-10-08 13:30:13
【问题描述】:

我在 Xamarin 表单 Mvvm 中遇到问题。我有 2 个不同的布局,比如 Layout1 和 Layout2,它们与一个通用的 ViewModel 绑定。 Layout1 包含多个标签,我在 xaml.cs 文件中使用 for 循环动态生成这些标签,并使用 SetBinding 绑定每个 Label'sTextProperty。 Layout2 包含一个按钮。

现在我想在单击按钮时更改特定标签的文本。

Layout1.xaml

<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Layout1">

<StackLayout x:Name="ParentStack">

 // dynamic Labels to be added here..

</StackLayout>           
</StackLayout>

Layout1.xaml.cs

 public partial class Layout1: StackLayout
{
    public Label dummyLabel;
    public Layout1()
    {
        InitializeComponent();
        for (int i = 0; i < 3; i++)
         {
         dummyLabel= new Label
         {
           Text = " ",  
         };
         dummyLabel.SetBinding (Label.TextProperty,"PhaseValue");
         parentRowCells.Children.Add(dummyLabel);
         var tapGestureRecognizer_1 = new TapGestureRecognizer();                
            tapGestureRecognizer_1.SetBinding(TapGestureRecognizer.CommandProperty,"LabelClicked");
            tapGestureRecognizer_1.CommandParameter = dummyLabel;
            dummyLabel.GestureRecognizers.Add(tapGestureRecognizer_1);
             }

          }

        }

Layout2.Xaml

<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
 x:Class="Layout2">

<StackLayout x:Name="ParentStack">
<Button Command={Binding ButtonClickedCommand} Text="Click Me"  />
</StackLayout>           

</StackLayout>

ViewModel.cs

 class ViewModel
{

    public Label label = new Label();
    public string textstring = "new text string";
    ICommand _labelClicked;
    public ICommand LabelClicked
    {
        get
        {
            this._labelClicked= this._labelClicked?? new Command(s =>
            {

                label = s as Label;
             label.Text = "new text"; //this change the text of particular              label when clicked but i need it from button clicked event from another layout.
                // here I'm getting the instance of label which i clicked on label.

            });

            return this._labelClicked;
        }
    }

    public ICommand ButtonClickedCommand{ protected set; get; }



    public ViewModel()
    {

        this.ButtonClickCommand = new Command<Button>((key) =>
        {

        //here I want to change the value of label when button command is clicked.  
         aa.Text = "this is not changing the text";
        });

    }



}

在这方面有什么帮助还是我需要遵循其他模式..??

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我的第一个想法是将您添加的每个Label 添加到您可以从两种布局访问的某个位置的List&lt;Label&gt;...您的视图模型看起来是合乎逻辑的地方。然后,当您单击按钮时,您可以找到要更改其文本的特定 Label 并更改它。然后,您可能必须重新加载您的列表。

    但是我认为更好的方法是使用ListView 而不是StackLayout。然后,您可以为包含一个标签的ListView 提供ItemTemplate。然后,您可以设置对象的ObservableCollection&lt;T&gt; 以用作ListView.ItemsSource。您可能想要制作一些具有 Text 属性的自定义对象,或者任何您想要调用的属性来保存标签的文本。最好在ObservableCollection&lt;T&gt; 中使用T 的对象而不是使用ObservableCollection&lt;string&gt;,因为对字符串类型的更改不会反映在ListView 项中,但会更改对象的属性(假设当然,您将其设为可绑定属性)将反映在绑定到该属性的那些控件中。简而言之,类似于(在您的 ViewModel 中):

    // Class level variable
    ObservableCollection<CustomType> dummyLabelContents;
    
    // then either in the ViewModel constructor or somewhere else:
    dummyLabelContents = new ObservableCollection<CustomType>();
    
    CustomType dummyText;
    for (int i = 0; i < 3; i++)
    {
        dummyText = new CustomType
        {
            Text = " ",  
        };
    }
    dummyLabelContents.Add(dummyText);
    

    而您的 CustomType 将只是一个简单的类,只有一个名为 TextBindableProperty

    这样设置,您可以将您的ListView.ItemsSource 分配为dummyLabelContents ObservableCollection,然后每当您向ObservableCollection 添加项目时,ListView 将自动更新。此外,由于在 ObservableCollection 中使用具有可绑定文本属性的自定义类型,因此当该文本属性更改时,ListView 中的项目也应相应更新。

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多