工具栏在使用Xamarin.Forms的Android上不起作用

问题描述 投票:0回答:5

我已使用以下代码使用Xamarin.Forms创建搜索栏。

但是到此为止。

protected void Search(StackLayout layout)
{
    SearchBar searchBar = new SearchBar
    {
        Placeholder = "Xamarin.Forms Property",
    };
    layout.Children.Add(searchBar);
}

protected override void BuildView(StackLayout layout)
{

    Search(layout);

    CallDataFromDB(layout);

    Device.OnPlatform(
        //broken in Xamarin 1.2. Awaiting a fix
        Android: () =>
        {
            var tbi = new ToolbarItem("Refresh", "", () =>
            {
                BuildView(layout);
            }, 0, 0);
            tbi.Order = ToolbarItemOrder.Secondary;  // forces it to appear in menu on Android
            if (ToolbarItems.Count == 0)
                ToolbarItems.Add(tbi);
        }
    );

}

当我在屏幕上做手势(触摸)时会中断。

这是我现在面临的确切错误:

缺少装配中的方法Android.Widget.SearchView :: get_InputType()Mono.Android.dll,在程序集中引用Xamarin.Forms.Platform.Android.dll

有人可以帮我吗?

c# android xamarin xamarin.forms toolbar
5个回答
1
投票

此代码对我有用:

public class ItemsPage : ContentPage
{
ItemListView list;
SearchBar searchbar;

public ItemsPage ()
{
    Title = "Items";

    list = new ItemListView ();

    searchbar = new SearchBar () {
        Placeholder = "Search",
    };

    searchbar.TextChanged += (sender, e) => {
        /*Work to be done at time text change*/
    };
    searchbar.SearchButtonPressed += (sender, e) => {
        /*Work to be done at time of Search button click*/
    };

    var stack = new StackLayout () {
        Children = {
            searchbar,
            list
        }
    };

    Content = stack;
}
}

  • 最低Android版本:覆盖-Android 4.0.3(API级别15)

  • 目标框架:使用最新安装的平台(API级别21)

  • 目标Android版本:自动-API级别19


0
投票

我使用了它及其工作。我设置:构建>常规>目标框架:使用最新安装的平台(4.4)构建> Android应用程序>最低Android版本:覆盖-Android 4.0.3(API级别15)构建> Android应用程序>目标Android版本:自动-使用目标框架版本。


0
投票

您是否尝试过在Xaml中进行相同的操作?我在我的应用程序中使用了SearchBar,它工作正常。下面是一些示例代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="Results"
             x:Class="MyApp.ResultPage">
<StackLayout Orientation="Vertical" 
                 Spacing="0">
<SearchBar x:Name="SearchBar"
                   Placeholder="Search by name"
                   SearchButtonPressed="OnSearchButtonTapped"
                   CancelButtonColor="Gray"
                   TextChanged="OnSearchBarTextChanged" />
        <ListView x:Name="ListView"
                  HasUnevenRows="true"
                  IsGroupingEnabled="true"
                  GroupDisplayBinding="{Binding Title}"
                  ItemTapped="DirectoryListOnItemTapped"
                  VerticalOptions="FillAndExpand"
                  SeparatorColor="Silver"
                  BackgroundColor="White"
                  IsPullToRefreshEnabled="true"
                  Refreshing="OnRefresButtonTapped">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Name}"
                              TextColor="Black" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
</StackLayout>
<ContentPage>

0
投票

我正在使用以下代码,我正在遵循MVVM设计模式在Veiw页面中编写波纹管代码:-

 internal class NotificationsPage :  ContentPage
    {
        #region Variables and Controlls declaration 
        private NotificationsViewModel _viewModel; 
        private SearchBar _notificationSearchBar; 
        #endregion Variables and Controlls declaration

        #region Constructor

        public NotificationsPage()  
        {
                BindingContext = App.Locator.Notification;
                _viewModel = BindingContext as NotificationsViewModel;

                _notificationSearchBar = new SearchBar()
                { 
                };
                _notificationSearchBar.SetBinding(SearchBar.TextProperty,"TEXT");
                _notificationSearchBar.SetBinding(SearchBar.SearchCommandProperty,"SearchCommand" );
        }

在viewModel中可以

#region Search Functionality


    #region Commands
    private RelayCommand _searchCommand;
    public RelayCommand SearchCommand
    { get { return _searchCommand ?? (_searchCommand = new RelayCommand(async () => 
    {
        if (SearchedList.Count > 0)
        {

            if (!string.IsNullOrEmpty(Text))
            {

                List<Notification> entities = SearchedList.Where(e =>

                 Convert.ToString(e.NotificationSubject).ToLower().Trim().Contains(Text.ToLower().Trim())
                 || Convert.ToString(e.LinkedRecordName).ToLower().Trim().Contains(Text.ToLower().Trim())
                 || Convert.ToString(e.NotificationDateSent).ToLower().Trim().Contains(Text.ToLower().Trim())
                 || Convert.ToString(e.NotificationContent).ToLower().Trim().Contains(Text.ToLower().Trim())).ToList();

                NotificationsList = new ObservableCollection<Notification>(entities);
            }
            else
            {

                NotificationsList = SearchedList;

            }
        }

    }
    )); } }


    private string _searchText = string.Empty;

    public string Text          // This is the text property we bind on page   _notificationSearchBar.SetBinding(SearchBar.TextProperty,"TEXT"); 
    {
        get { return _searchText; }
        set
        {
            if (_searchText != value)
            {
                _searchText = value;
                Set(() => Text, ref _searchText, value);

                if (SearchCommand.CanExecute(null))
                {
                    SearchCommand.Execute(null);

                }
            }
        }
    }
    #endregion

    #endregion

其中SearchedList和NotificationList是绑定到列表的可观察集合,您也可以将其用作类列表

private ObservableCollection<Notification> _notificationsList;

        public ObservableCollection<Notification> NotificationsList
        {
            get { return _notificationsList; }
            set
            {
                Set(() => NotificationsList, ref _notificationsList, value);
            }
        }



        /// <summary>
        /// Holds Searched notifications
        /// </summary>
        private ObservableCollection<Notification> _searchedList;

        public ObservableCollection<Notification> SearchedList
        {
            get { return _searchedList; }
            set
            {
                Set(() => SearchedList, ref _searchedList, value);
            }
        }

0
投票

我做了很多次。我更喜欢使用MVVM模式。我正在使用命令行为的事件将搜索命令绑定到ViewModel基本上,您搜索一些文本或数量,然后对已经拥有的某个集合进行操作。就如此容易。我已经为此写了blog,看看是否有任何帮助。谢谢 :)请参考:https://adityadeshpandeadi.wordpress.com/2018/01/14/search-through-listview-items-in-xamarin-forms/

© www.soinside.com 2019 - 2024. All rights reserved.