如何使用Xamarin.Forms将SearchBar添加到每个页面的顶部

时间:2016-11-19 18:03:30

标签: c# xaml xamarin searchbar visual-studio-mac

我是Xamarin / Visual Studio For Mac的新手(预览版)。我不是C#的新手。我正在努力解决如何在标题中获取SearchBar。

我正在使用Xamarin网站上的Xamarin.CRM示例。

看起来像是一个ViewModel,它将标题绑定到每个页面的标题,但我宁愿有一个搜索栏。

XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Phoodie.AboutPage" xmlns:vm="clr-namespace:Phoodie;" Title="{Binding Title}">
<ContentPage.BindingContext>
    <vm:AboutViewModel />
</ContentPage.BindingContext>

代码背后

public partial class ItemsPage : ContentPage
{
    ItemsViewModel viewModel;

    public ItemsPage()
    {
        InitializeComponent();

        BindingContext = viewModel = new ItemsViewModel();
    }

    async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
    {
        var item = args.SelectedItem as Item;
        if (item == null)
            return;

        await Navigation.PushAsync(new ItemDetailPage(new ItemDetailViewModel(item)));

        // Manually deselect item
        ItemsListView.SelectedItem = null;
    }

    async void AddItem_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new NewItemPage());
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        if (viewModel.Items.Count == 0)
            viewModel.LoadItemsCommand.Execute(null);
    }
}

视图模型

public ItemsViewModel()
    {
        Title = "Browse";
        Items = new ObservableRangeCollection<Item>();
        LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());

        MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
        {
            var _item = item as Item;
            Items.Add(_item);
            await DataStore.AddItemAsync(_item);
        });
    }

我想要完成的事情

enter image description here

感谢您在时间前的帮助!

0 个答案:

没有答案