绑定到 Entry 元素不会多次更新条目文本 - Maui

问题描述 投票:0回答:1

将值绑定到 Label.Text 时它工作正常但对 Entry.Text 执行相同操作我看到初始值但是当值更改时它永远不会更新。这是标签的 xaml:

            <Label x:Name="multiplierLabel"  VerticalOptions="Center"
            FontSize="22" 
               HorizontalTextAlignment="Center"
            Text="{Binding Source={x:Reference ControlView}, Path=Multiplier}" 
            Padding="16,0, 0,0"/>

和条目:

            <Entry 
                HorizontalTextAlignment="Center" 
                TextChanged="Entry_TextChanged"  
                Focused="Entry_Focused"
                Keyboard="Numeric"
                FontSize="22" 
                Text="{Binding Source={x:Reference ControlView}, Path=MultiplierB}"
                >
            </Entry>

两个项目在视图模型中绑定到相同的值:

            Multiplier="{Binding CurrentActionRow.Multiplier}"
            MultiplierB="{Binding CurrentActionRow.Multiplier}"

两者背后的代码是相同的:

    public string Multiplier
    {
        get => (string)GetValue(MultiplierProperty);
        set => SetValue(MultiplierProperty, value);
    }

    public static readonly BindableProperty MultiplierProperty =
           BindableProperty.Create(
               nameof(Multiplier),
               typeof(string),
               typeof(ActionRowElement),
               "Multiplier Not Set");

    public string MultiplierB
    {
        get => (string)GetValue(MultiplierBProperty);
        set => SetValue(MultiplierBProperty, value);
    }

    public static readonly BindableProperty MultiplierBProperty =
           BindableProperty.Create(
               nameof(MultiplierB),
               typeof(string),
               typeof(ActionRowElement),
               "MultiplierB Not Set");

当绑定值更改时,任何人都可以帮助获取要在 Entry 元素中更新的文本值吗?

binding label maui
1个回答
0
投票

你可以试试把你家的

BindingMode
改成
TwoWay

我创建了一个演示并进行了测试,它在我这边运行正常。

可以参考以下代码:

ChildView.xaml.cs

public partial class ChildView : ContentView 
{
    //other code

    public string Multiplier
    {
        get => (string)GetValue(MultiplierProperty);
        set => SetValue(MultiplierProperty, value);
    }

    public static readonly BindableProperty MultiplierProperty =
           BindableProperty.Create(
               nameof(Multiplier),
               typeof(string),
               typeof(ChildView),
               defaultBindingMode: BindingMode.TwoWay);

    public string MultiplierB
    {
        get => (string)GetValue(MultiplierBProperty);
        set => SetValue(MultiplierBProperty, value);
    }

    public static readonly BindableProperty MultiplierBProperty =
           BindableProperty.Create(
               nameof(MultiplierB),
               typeof(string),
               typeof(ChildView),
               defaultBindingMode: BindingMode.TwoWay);


    public ChildView()
      {
            InitializeComponent();
      }
}

ChildView.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiContentViewApp.ChildView"
             x:Name="TestControlView"
             >
    <VerticalStackLayout>

        <Label   Text="{Binding Source={x:Reference TestControlView}, Path=Multiplier}"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />

        <Entry   Text="{Binding Source={x:Reference TestControlView}, Path=MultiplierB}"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />

    </VerticalStackLayout>
</ContentView>

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mauiapp="clr-namespace:MauiContentViewApp"
             x:Class="MauiContentViewApp.MainPage"
             x:Name="mainpage"
             >

    <ScrollView>
        <VerticalStackLayout>

            <mauiapp:ChildView  
                               Multiplier="{Binding  TestName}"
                               MultiplierB="{Binding TestName}"
                               >
            </mauiapp:ChildView>


            <Button  Text="Change"  Command="{Binding ChangeNameCommand}"></Button>

        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

MainPage.xaml.cs

 public MainPage() 
      {
            InitializeComponent();

            this.BindingContext =  new MyViewModel();
      }

MyViewModel.cs

     public class MyViewModel: INotifyPropertyChanged 
    {
        private string _testName;
        public string TestName
        {
            set { SetProperty(ref _testName, value); }
            get { return _testName; }
        }

        public ICommand ChangeNameCommand => new Command(changeMethod);


        private void changeMethod()
        {
            index++;

            TestName = "change data" + index;          
        }

        public MyViewModel()
        {
            TestName = "test string";

        }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.