【发布时间】:2021-11-15 19:50:57
【问题描述】:
我正在使用适用于 Android 和 iOS 的 Xamarin.Forms(版本 5.0.0.2012)Shell 应用程序。在应用程序中,我有一个概念,如果用户登录,我必须显示 Shell Search 框,否则必须隐藏搜索框。我遵循标准的 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 是我的可绑定属性,我将视图模型中的值绑定到该属性,该视图模型像往常一样继承 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";
页面加载后,Shell SearchHandler的SearchBoxVisibility属性总是只绑定其默认值Expanded。因此,页面具有 Shell 搜索的扩展视图。 Shell 搜索不采用可绑定属性值。如何通过 Shell 搜索实现我的要求?
【问题讨论】:
-
SearchBoxVisibility是一个枚举,而不是一个字符串
标签: xamarin.forms xamarin.forms.shell