Ale*_*akh 1 data-binding searchbar xamarin xamarin.forms
我希望每当我的viewmodel忙时禁用搜索栏.我的视图中有以下xaml(部分视图):
<SearchBar x:Name="searchBar" TextChanged="OnSearchQueryChanged" Grid.Row="0" IsEnabled="{Binding IsBusy}"/>
<ActivityIndicator x:Name="progress" IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}"/>
Run Code Online (Sandbox Code Playgroud)
两个元素都绑定到相同的属性,但是当我的ViewModel引发IsBusy = false时,SearchBar保持禁用状态.同时进度变得隐藏.
这可能有什么问题?
您想要的是在查看模型属性时将SearchBar.IsEnabled属性设置为,反之亦然.trueIsBusyfalse
您现在正在做的是,IsEnabled当您的竞争模型不再繁忙时(IsBusy假设为false),禁用(设置为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 ();
}
}
Run Code Online (Sandbox Code Playgroud)
为了在xaml中的绑定中使用它,让我们在它包含你的xaml片段的容器中包含它的一个实例(让我们假设它是a 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2226 次 |
| 最近记录: |