c# - Xamarin 依赖绑定(bind)

标签 c# ios xamarin xamarin.ios xamarin.forms

我正在努力找出我认为应该非常基本的东西,我希望有人可以帮助我。
我正在尝试根据是否向其提供数据来使自定义控件可见。
例如:
我有一个使用自定义控件的主页,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:XamarinDependantProperty.Controls;assembly=XamarinDependantProperty"
             x:Class="XamarinDependantProperty.Pages.MainPage">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness">
            <On Platform="iOS" Value="0,20,0,0"/>
        </OnPlatform>
    </ContentPage.Padding>
    <StackLayout>
        <Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />
        <controls:CustomEntry TextValue="Test" VerticalOptions="Center" HorizontalOptions="Center"></controls:CustomEntry>
    </StackLayout>
</ContentPage>
自定义控件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinDependantProperty.Controls.CustomEntry" IsVisible="{Binding TextIsVisible}">
    <StackLayout>
        <Label Text="{Binding TextIsVisible}"  />
        <Label Text="{Binding TextValue}"  />
    </StackLayout>
</ContentView>
而后面的代码如下:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamarinDependantProperty.Controls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomEntry : ContentView
    {
        public CustomEntry()
        {
            InitializeComponent();

            BindingContext = this;
        }

        public string TextValue
        {
            get { return (string)GetValue(TextValueProperty); }
            set { SetValue(TextValueProperty, value); }
        }

        public static BindableProperty TextValueProperty = BindableProperty.Create(nameof(TextValue), typeof(string), typeof(CustomEntry));

        public bool TextIsVisible => !String.IsNullOrEmpty(TextValue);
    }
}
如果我将 CustomEntry 的 TextValue 设置为“Test”,则输出为:

Welcome to Xamarin Forms!

False

Test


如果我输入一个空字符串,则根本没有输出,应用程序启动但没有显示任何内容。
如果我将 TextValueProperty 的默认值设置为 null,则输出为:

Welcome to Xamarin Forms!


从输出看来,当我设置 TextValue 时,TextIsVisible 值在第一个绑定(bind) (IsVisible) 中工作,即使第二个绑定(bind) (Text) 输出 False,但为什么它是错误的?
如果我不提供值并且我不告诉它 null 是一个可接受的空值,那么它会完全搞砸,但在这方面它什么也没说。没有错误,没有输出,什么都没有。有没有办法看到出了什么问题? (在 iphone 模拟器上测试)
然后如果我把这个概念从这个测试场景中拿出来,放到一个真实的场景中。然后设置 TextValue 并输出 TextIsVisible 仍然是错误的,但它没有显示。
我究竟做错了什么?我不明白什么?

最佳答案

您需要提高属性已更改 TextIsVisible 的事件通知 View 这个 属性已更改 .

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomEntry : ContentView
{
    public CustomEntry()
    {
        InitializeComponent();

        BindingContext = this;
    }

    public string TextValue
    {
        get { return (string)GetValue(TextValueProperty); }
        set{SetValue(TextValueProperty, value);OnPropertyChanged(nameof(TextIsVisible));}
    }

    public static BindableProperty TextValueProperty = BindableProperty.Create(nameof(TextValue), typeof(string), typeof(CustomEntry),propertyChanged:OnTextChanged);

    private static void OnTextChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        var entry = bindable as CustomEntry;
        entry?.OnPropertyChanged(nameof(TextIsVisible));
    }

    public bool TextIsVisible => !String.IsNullOrEmpty(TextValue);
}

关于c# - Xamarin 依赖绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441121/

相关文章:

c# - Entity Framework - 有条件地包括相关实体

javascript - 为什么通过 Javascript 更改文本框的文本不会触发 Asp .Net 中的 OnTextChanged

c# - 如何从 Controller 类更新 MainWindow 中的标签?

ios - 识别 iCloud coreData 更新 : good practice

visual-studio - 如何修复Xamarin/NuGet错误NU1107? - "Version conflict detected for Xamarin.Android.Support.Compat"

c# - 电报机器人 : Wrong response from the webhook: 404 Bad Request

ios - UITextField,在缩放模式下聚焦时文本模糊

ios - 在 "self"中有多个项目?

azure - 我可以在 xamarin 表单应用程序中使用除 id 之外的任何其他列名称吗?

c# - Xamarin.Forms 中的导航