xamarin - 在 Xamarin MVVM 中单击按钮更改标签值

标签 xamarin xamarin.forms

我在 Xamarin 表单 Mvvm 中遇到了一个问题。我有 2 种不同的布局,比如 Layout1 和 Layout2,它们与一个公共(public) ViewModel 绑定(bind)。 Layout1 包含多个标签,我在 xaml.cs 文件中使用 for 循环动态生成这些标签,并使用 SetBinding 绑定(bind)每个标签的文本属性。 Layout2 包含一个按钮。

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

布局1.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";
        });

    }



}

这方面的任何帮助还是我需要遵循其他模式......??

最佳答案

我的第一个想法是添加每个 Label你添加到 List<Label>您可以从两种布局访问的某个地方......您的 View 模型似乎是合乎逻辑的地方。然后当你点击你的按钮时,你可以找到特定的 Label您要更改其文本并更改它。然后您可能必须重新加载您的列表。

但是我认为更好的方法是使用 ListView而不是 StackLayout .然后你可以有一个ItemTemplate对于 ListView其中包括一个标签。然后您可以设置 ObservableCollection<T>用作 ListView.ItemsSource 的对象.您可能想要制作一些具有 Text 属性的自定义对象,或者您想要调用的任何包含标签文本的属性。最好为 T 使用一个对象在ObservableCollection<T>而不是使用 ObservableCollection<string>因为对字符串类型的更改不会反射(reflect)在 ListView 中项,但对对象属性的更改(当然假设您将其设为可绑定(bind)属性)将反射(reflect)在绑定(bind)到该属性的那些控件中。所以简而言之,类似于(在您的 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只是一个只有 BindableProperty 的简单类称为 Text .

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

关于xamarin - 在 Xamarin MVVM 中单击按钮更改标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37699805/

相关文章:

web-services - 在 Xamarin 中使用 New Relic 跟踪 Web 服务请求

xamarin - Xamarin Forms 中的 Google 钱包/Android Pay 集成

button - Xamarin Forms Button TextColor 在 Android 上的表现与在 iOS 上不同

Xamarin 表单阻止用户按下返回键

Xamarin 形成图像的固定大小

android - Xamarin #如果 Android 不工作

c# - Xamarin - 尝试在 Project 上安装所需的 Android 组件时发生错误

azure - 无法在 azure 通知中心上传 '.p12' 证书

ios - Xamarin iOS : Refresh page from viewModel

c# - 应用程序的 Xamarin UWP 图标 Assets