简体   繁体   English

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

[英]Toolbar not working on Android using Xamarin.Forms

I have used the following code to create a searchbar using Xamarin.Forms.我使用以下代码使用 Xamarin.Forms 创建搜索栏。

But it breaks at this point.但它在这一点上打破了。

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

}

It breaks when I make a gesture (touch) on the screen it breaks.当我在屏幕上做手势(触摸)时它会中断。

This is the exact error which I am facing now:这是我现在面临的确切错误:

Missing method Android.Widget.SearchView::get_InputType() in assembly Mono.Android.dll, referenced in assembly Xamarin.Forms.Platform.Android.dll程序集 Mono.Android.dll 中缺少方法 Android.Widget.SearchView::get_InputType(),在程序集 Xamarin.Forms.Platform.Android.dll 中引用

Can anybody help me with this?有人可以帮我解决这个问题吗?

This code works for me :这段代码对我有用:

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;
}
}

Note :注意

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

  • Target framework: Use latest installed platform (API Level 21)目标框架:使用最新安装的平台(API 级别 21)

  • Target Android version: Automatic - API Level 19目标 Android 版本:自动 - API 级别 19

I used this and its working.我使用了这个和它的工作。 I set: Build > General > Target framework: Use latest installed platform (4.4) Build > Android Application > Minimum Android version: Override - Android 4.0.3 (API Level 15) Build > Android Application > Target Android version: Automatic - use target framework version.我设置:Build > General > Target framework: Use latest installed platform (4.4) Build > Android Application > Minimum Android version: Override - Android 4.0.3 (API Level 15) Build > Android Application > Target Android version: Automatic - use target框架版本。

Have you tried doing the same in Xaml?你有没有试过在 Xaml 中做同样的事情? I'm using a SearchBar in my app and it's working just fine.我在我的应用程序中使用 SearchBar,它工作得很好。 Here's some sample code below:下面是一些示例代码:

<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>

I am using following code,I am following MVVM design pattern write bellow code in Veiw Page :-我正在使用以下代码,我正在遵循 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" );
        }

In the viewModel can在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

Where SearchedList and NotificationList is the observable collection binded to list you can take it as list of class as well其中 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);
            }
        }

I did this a lot of time.我这样做了很多时间。 I prefer using MVVM pattern.我更喜欢使用 MVVM 模式。 i am using event to Command Behaviours for binding search command to ViewModel basically you search on some text or quantity and operate on some collection that you already have.我正在使用事件到命令行为将搜索命令绑定到 ViewModel 基本上你搜索一些文本或数量并操作你已经拥有的一些集合。 as simple as that.就如此容易。 I have written a blog about this, see if that helps you in any way.我写了一篇关于这个的博客,看看它是否对你有任何帮助。 Thanks :) please Ref: https://adityadeshpandeadi.wordpress.com/2018/01/14/search-through-listview-items-in-xamarin-forms/谢谢:) 请参考: https : //adityadeshpandeadi.wordpress.com/2018/01/14/search-through-listview-items-in-xamarin-forms/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM