0

我是 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);
        });
    }

我想要完成的事情

在此处输入图像描述

提前感谢您的帮助!

4

0 回答 0