Saya telah membuat properti yang dapat diikat yang disebut Teks di TargetClass.cs. Properti Teks itu adalah properti yang dapat diikat bersarang. TargetClass.cs :

  public class TargetClass : BindableObject
{
    public static readonly BindableProperty TextProperty =
            BindableProperty.Create("Text", typeof(string), typeof(TargetClass), "Default", BindingMode.TwoWay, null,
                    OnTextChanged);

    private static void OnTextChanged(BindableObject bindable, object oldValue, object newValue)
    {

    }

    public string Text
    {
        get
        {
            return (string)GetValue(TextProperty);
        }

        set
        {
            SetValue(TextProperty, value);
        }
    }
}

ViewModel.cs :

public class ViewModel : INotifyPropertyChanged
{
    private string m_text = "New Value";
    public string TextValue
    {
        get { return m_text; }
        set
        {
            m_text = value;
            OnPropertyChanged("TextValue");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Kemudian saya telah membuat MyView.cs

 public class MyView : ContentView
{
    private TargetClass target;
    Label label;
    public TargetClass Target
    {
        get
        {
            return target;
        }
        set
        {
            target = value;
            label.Text = target.Text;
        }
    }
    public MyView()
    {
        label = new Label();
        label.FontSize = 50;
        Content = label;
    }
}

Halaman Utama.xaml :

<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"
         xmlns:local="clr-namespace:BindingDemo"
         x:Class="BindingDemo.MainPage">

<StackLayout>
    <local:MyView >
        <local:MyView.Target>
            <local:TargetClass Text="{Binding TextValue}" >
                <local:TargetClass.BindingContext>
                    <local:ViewModel />
                </local:TargetClass.BindingContext>
            </local:TargetClass>
        </local:MyView.Target>
    </local:MyView>
    <Button Text="Click" Clicked="Button_Clicked" />
</StackLayout>

Halaman Utama.xaml.cs:

 public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        (BindingContext as ViewModel).TextValue = "Latest Value";
    }
}

Properti TextValue diikat dengan properti Text bindble.

Saya telah mengubah nilai properti TextValue di klik tombol di MainPage.xaml.cs.

Jika saya mengklik tombol. itu harus menunjukkan nilai Terbaru. Tapi itu menunjukkan nilai baru, tidak berubah dari nilai baru ke nilai terbaru.

Apakah mungkin untuk mengubah nilai properti TextValue di Button click secara dinamis?

0
Sarath Kumar 23 April 2020, 07:47

1 menjawab

Jawaban Terbaik

Jika Anda ingin menggunakan metode di atas, Anda dapat memeriksa di bawah (Tapi itu tidak disarankan. Jika TargetClass tidak digunakan di tempat lain, kelas ini tampaknya berlebihan, Anda dapat menambahkan BindableProperty ke MyView secara langsung):

Di TargetClass.cs:

public class TargetClass : BindableObject
{
    public static readonly BindableProperty TextProperty =
            BindableProperty.Create("Text", typeof(string), typeof(TargetClass), "Default", BindingMode.TwoWay, propertyChanged: OnTextChanged);

    private static void OnTextChanged(BindableObject bindable, object oldValue, object newValue)
    {
        ((TargetClass)bindable).View.label.Text = (string)newValue;
    }

    public string Text
    {
        get
        {
            return (string)GetValue(TextProperty);
        }

        set
        {
            SetValue(TextProperty, value);

        }
    }

    public static readonly BindableProperty ViewProperty =
            BindableProperty.Create("View", typeof(MyView), typeof(TargetClass), null, BindingMode.TwoWay);
    public MyView View
    {
        get
        {
            return (MyView)GetValue(ViewProperty);
        }

        set
        {
            SetValue(ViewProperty, value);

        }
    }

}

Di halaman.xaml:

 <StackLayout >
    <local:MyView x:Name="MyView">
        <local:MyView.Target>
            <local:TargetClass x:Name="MyClass" View="{Binding ., Source={x:Reference MyView}}" Text="{Binding TextValue}" >
                <local:TargetClass.BindingContext>
                    <local:ViewModel />
                </local:TargetClass.BindingContext>
            </local:TargetClass>
        </local:MyView.Target>
    </local:MyView>
    <Button Text="Click" Clicked="Button_Clicked" />
</StackLayout>

Di halaman.xaml.cs:

private void Button_Clicked(object sender, EventArgs e)
    {
        (MyClass.BindingContext as ViewModel).TextValue = "Latest Value";
    }
0
Leo Zhu - MSFT 23 April 2020, 06:47