【问题标题】:Toolbar not working on Android using Xamarin.Forms使用 Xamarin.Forms 的工具栏无法在 Android 上运行
【发布时间】:2014-09-08 06:38:43
【问题描述】:

我使用以下代码使用 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

谁能帮我解决这个问题?

【问题讨论】:

  • 请告诉我当它(要求)中断时你得到的异常。
  • 请发布完整课程
  • 未处理的异常。未创建对象的实例。
  • 如上所述,发布完整课程。我怀疑您是在页面调用 InitializeComponents 之前尝试使用布局。

标签: c# android xamarin xamarin.forms toolbar


【解决方案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

【讨论】:

    【解决方案2】:

    我使用了这个和它的工作。 我设置: 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框架版本。

    【讨论】:

      【解决方案3】:

      您是否尝试过在 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>
      

      【讨论】:

        【解决方案4】:

        我正在使用以下代码,我正在遵循 MVVM 设计模式 在 Veiw Page 中编写以下代码:-

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2018-05-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-11-17
            • 2017-10-27
            • 2017-02-23
            相关资源
            最近更新 更多