如何设置Xamarin的可见性属性.通过可绑定属性搜索表单?



我正在与Xamarin合作。Forms(Version 5.0.0.2012)ShellAndroid和iOS应用程序。在应用程序中,我有一个概念,我必须显示Shell搜索框,否则必须隐藏搜索框。我遵循标准的MVVM模式。

<Shell.SearchHandler>
<helper:ProductSearchHandler Placeholder="Search by Title, ISBN, Publisher" TextColor="{StaticResource Primary}" FontSize="Micro"
ShowsResults="true" SearchBoxVisibility="{Binding TopSearchVisibility}" DisplayMemberName="Name" 
ItemTemplate="{StaticResource ProductSearchTemplate}" />
</Shell.SearchHandler>

以上是在我的ContentPage中放置Search的代码。

TopSearchVisibility是我的可绑定属性,我绑定的值从viewmodel继承BaseViewModel像往常一样。

ProductSearchHandler搜索处理程序类。

在我的视图模型中

// Declaration with default value
public string topSearchVisibility = "Collapsible";
public string TopSearchVisibility
{
get { return topSearchVisibility; }
set
{
topSearchVisibility = value;
OnPropertyChanged();
}
}

从API服务调用获得用户数据后,我正在做基于UserId显示或隐藏搜索框的过程,如下所示

long userID = ... from API;
if (userID > 0)
TopSearchVisibility = "Hidden";
else
TopSearchVisibility = "Collapsible";

页面加载后,SearchBoxVisibility属性总是只绑定它的默认值Expanded. 因此,页面扩展了Shell搜索视图。Shell搜索不接受可绑定属性值。如何实现我的要求与壳牌搜索?

您可以使用IValueConverter接口将string属性转换为SearchBoxVisibility属性。

:

createSearchVisibleConvert:

public class SearchVisibleConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SearchBoxVisibility searchBoxVisibility = SearchBoxVisibility.Expanded;
if (value !=null)
{
switch (value)
{
case "Hidden":
searchBoxVisibility = SearchBoxVisibility.Hidden;
break;
case "Collapsible":
searchBoxVisibility = SearchBoxVisibility.Collapsible;
break;
default:
searchBoxVisibility = SearchBoxVisibility.Expanded;
break;
}

}
return searchBoxVisibility;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

在你的xaml:

中使用
...
<ContentPage.Resources>
<ResourceDictionary>
<controls:SearchVisibleConvert x:Key="visibleConvert" />
</ResourceDictionary>
</ContentPage.Resources>
<Shell.SearchHandler>
<helper:ProductSearchHandler Placeholder="Search by Title, ISBN, Publisher" TextColor="{StaticResource Primary}" FontSize="Micro"
ShowsResults="true" SearchBoxVisibility="{Binding TopSearchVisibility, Converter={StaticResource visibleConvert}}" DisplayMemberName="Name" 
ItemTemplate="{StaticResource ProductSearchTemplate}" />
</Shell.SearchHandler>

相关内容

  • 没有找到相关文章

最新更新