【问题标题】:SearchBar.IsEnabled binding are not updated when property changed in Xamarin.Forms当 Xamarin.Forms 中的属性更改时,SearchBar.IsEnabled 绑定不会更新
【发布时间】:2014-08-10 12:56:00
【问题描述】:

我想在我的视图模型忙碌时禁用搜索栏。 我的观点有以下 xaml(部分观点):

<SearchBar x:Name="searchBar" TextChanged="OnSearchQueryChanged" Grid.Row="0"  IsEnabled="{Binding IsBusy}"/>
<ActivityIndicator x:Name="progress" IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}"/>

两个元素都绑定到同一个属性,但是当我的 ViewModel 引发 IsBusy = false 时,SearchBar 保持禁用状态。同时进度被隐藏。

这里有什么问题?

【问题讨论】:

    标签: data-binding xamarin searchbar xamarin.forms


    【解决方案1】:

    当您查看模型IsBusy 属性为false 时,您想要将SearchBar.IsEnabled 属性设置为true反之亦然

    您现在正在做的是在您的 vie 模型不再忙(IsBusy 为 false)时禁用(将 IsEnabled 设置为 false)搜索栏。

    为此你需要一个转换器,一个返回 true 表示 false 和 false 表示 true 的转换器:

    public class NotConverter:IValueConverter
    {
        public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.GetType () == typeof(bool))
                return !((bool)value);
            return value;
        }
    
        public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
    

    为了在 xaml 中的绑定中使用它,让我们将它的一个实例作为资源包含在包装 xaml sn-p 的容器中(假设它是 ContentPage),然后我们可以在{Binding} 标记扩展:

    <ContentPage 
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:YourNamespace;assembly=YourAssemblyName">
        <ContentPage.Resources>
            <ResourceDictionary>
                <local:NotConverter x:Key="notConverter"/>
            </ResourceDictionary>
        </ContentPage.Resources>
        <ContentPage.Content>
            <StackLayout Orientation="Vertical">
                <SearchBar x:Name="searchBar" TextChanged="OnSearchQueryChanged" Grid.Row="0"  IsEnabled="{Binding IsBusy, Converter={StaticResource notConverter}}"/>
                <ActivityIndicator x:Name="progress" IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}"/>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2020-04-08
      • 2021-09-01
      • 2011-09-19
      • 1970-01-01
      • 2011-04-27
      相关资源
      最近更新 更多