xamarin 从视图设置时未命中自定义控件可绑定属性

kyvafyod  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(114)

我创建了一个控件,它有一个可绑定的属性,但是当我尝试设置它的值时,它没有设置,当我检查它的setter时,它在调试时没有被命中,不确定我做错了什么。

public decimal MetricValue
        {
            get => (decimal)GetValue(MetricValueProperty);
            set => SetValue(MetricValueProperty, value);
        }

        public static readonly BindableProperty MetricValueProperty =
            BindableProperty.Create(
                propertyName: nameof(MetricValue),
                returnType: typeof(decimal),
                declaringType: typeof(HeightSelection),
                defaultBindingMode: BindingMode.TwoWay,
                propertyChanged: MetricValuePropertyChanged);

我还有一个属性已更改,它没有被提升。

<controls:customControl
                                        CurrentSystemOfMeasure="{Binding CurrentSystemOfMeasure}"
                                        MetricValue="{Binding CurrentHeight}"
                                        TextAlignment="Start"
                                        OnHeightSelectedCommand="{Binding HeightSelectionCommand}"
                                        IsValid="True" />

任何意见都是有帮助的,

41ik7eoe

41ik7eoe1#

在xaml视图中设置上下文了吗?
说到这里我道:

public static readonly BindableProperty CancelButtonVisibleAvailableProperty = BindableProperty.Create(
        propertyName: "CancelButtonVisible",
        returnType: typeof(bool),
        declaringType: typeof(SelphiDetailPhotosView),
        defaultValue: true);

    public bool CancelButtonVisible
    {
        get { return (bool)GetValue(CancelButtonVisibleAvailableProperty); }
        set { SetValue(CancelButtonVisibleAvailableProperty, value); }
    }

ContentView需要有一个x:name来设置属性IsVisible中的上下文。在您的情况下,需要在标签的Text属性中设置上下文以显示十进制值
我已设置上下文,使其引用我的自定义视图selphiDetailPhotosView

<Button IsVisible="{Binding Source={x:Reference selphiDetailPhotosView}, Path=CancelButtonVisible}"
                        Margin="27,0,27,18"
                        BackgroundColor="{StaticResource PlatinumColor}"
                        Command="{Binding CancelCommand}"
                        Text="Cancelar"
                        TextColor="White"
                        TextTransform="None" />


<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="confia.Views.Views.OnBoardingAndLivenessSharedViews.SelphiDetailPhotosView"
   xmlns:svg="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
xmlns:transformation="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
xmlns:views="clr-namespace:confia.Views.Views"
  xmlns:converters="clr-namespace:confia.Views.Converters"
x:Name="selphiDetailPhotosView"
>

最后,在ContentPage中调用自定义控件并与ViewModel绑定
<customControl:SelphiDetailPhotosView CancelButtonVisible="{Binding CanCancel}"/>

3okqufwl

3okqufwl2#

我已经复制了你的场景,这对我很有效。
CodeBehind自订控件

public partial class CustomControl : ContentView
{
    public CustomControl()
    {
        InitializeComponent();
    }

    public static readonly BindableProperty MetricValueProperty = BindableProperty.Create(
       propertyName: nameof(MetricValue),
       returnType: typeof(decimal),
       declaringType: typeof(CustomControl),
       defaultBindingMode: BindingMode.TwoWay,
       defaultValue: 0m);

    public decimal MetricValue
    {
        get { return (decimal)this.GetValue(MetricValueProperty); }
        set { this.SetValue(MetricValueProperty, value); }
    }
}

自定义控件XAML

<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ferreplace.Views.Controls.CustomControl"
x:Name="this"
>
<ContentView.Content>
    <StackLayout HorizontalOptions="CenterAndExpand" >
        <Label Text="Metric Value" FontSize="40"/>
        <Label FontSize="20" Text="{Binding Source={x:Reference  this}, Path=MetricValue}" ></Label>
    </StackLayout>
</ContentView.Content>

调用自定义控件的ContenPage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:ferreplace.Views.Controls"
             x:Class="ferreplace.MainPage">
 <StackLayout>
      <controls:CustomControl MetricValue="{Binding MetricValueBinding}"/>
    </StackLayout>
</ContentPage>

检视模型

public class MainPageViewModel: BindableBase, IInitializeAsync
{

    private decimal _metricValueBinding;
    public decimal MetricValueBinding
    {
        get
        {
            return _metricValueBinding;
        }
        set
        {
            SetProperty(ref _metricValueBinding, value);
        }
    }

    public MainPageViewModel()
    {
    }

    public async Task InitializeAsync(INavigationParameters parameters)
    {
        MetricValueBinding = 30m;
    }
}

Decimal Binding Result可能您在创建可绑定属性时忘记了默认值

相关问题