答案 0 :(得分:3)
我找到了解决方案,因为我创建了一个自定义的PageRenderer类。
public class RightToolbarMenuCustomRenderer : PageRenderer
{
//I used UITableView for showing the menulist of secondary toolbar items.
List<ToolbarItem> _secondaryItems;
UITableView table;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
//Get all secondary toolbar items and fill it to the gloabal list variable and remove from the content page.
if (e.NewElement is ContentPage page)
{
_secondaryItems = page.ToolbarItems.Where(i => i.Order == ToolbarItemOrder.Secondary).ToList();
_secondaryItems.ForEach(t => page.ToolbarItems.Remove(t));
}
base.OnElementChanged(e);
}
public override void ViewWillAppear(bool animated)
{
var element = (ContentPage)Element;
//If global secondary toolbar items are not null, I created and added a primary toolbar item with image(Overflow) I
// want to show.
if (_secondaryItems != null && _secondaryItems.Count > 0)
{
element.ToolbarItems.Add(new ToolbarItem()
{
Order = ToolbarItemOrder.Primary,
Icon = "more.png",
Priority = 1,
Command = new Command(() =>
{
ToolClicked();
})
});
}
base.ViewWillAppear(animated);
}
//Create a table instance and added it to the view.
private void ToolClicked()
{
if (table == null)
{
//Set the table position to right side. and set height to the content height.
var childRect = new RectangleF((float)View.Bounds.Width - 250, 0, 250, _secondaryItems.Count() * 56);
table = new UITableView(childRect)
{
Source = new TableSource(_secondaryItems) // Created Table Source Class as Mentioned in the
//Xamarin.iOS Official site
};
Add(table);
return;
}
foreach (var subview in View.Subviews)
{
if(subview == table)
{
table.RemoveFromSuperview();
return;
}
}
Add(table);
}
}
表源类是继承的UITableViewSource
public class TableSource : UITableViewSource
{
// Global variable for the secondary toolbar items and text to display in table row
List<ToolbarItem> _tableItems;
string[] _tableItemTexts;
string CellIdentifier = "TableCell";
public TableSource(List<ToolbarItem> items)
{
//Set the secondary toolbar items to global variables and get all text values from the toolbar items
_tableItems = items;
_tableItemTexts = items.Select(a => a.Text).ToArray();
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return _tableItemTexts.Length;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
string item = _tableItemTexts[indexPath.Row];
if (cell == null)
{ cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier); }
cell.TextLabel.Text = item;
return cell;
}
public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
return 56; // Set default row height.
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
//Used command to excute and deselct the row and removed the table.
var command = _tableItems[0].Command;
command.Execute(_tableItems[0].CommandParameter);
tableView.DeselectRow(indexPath, true);
tableView.RemoveFromSuperview();
}
}
在View Model中我有
public ICommand CancelCommand { get; set; }
//In constructor of ViewModel I Created Event for Command
CancelCommand = new Command(async () => await Cancel());
private async Task Cancel()
{
await Task.CompletedTask;
}
在内容页面Xaml我使用工具栏项目
<ContentPage.ToolbarItems>
<ToolbarItem Text ="Cancel" Priority="1" Order="Secondary" Command="{Binding CancelCommand}"/>
</ContentPage.ToolbarItems>
并且解决方案的输出与我想要的相同。
快乐的结局:)。如果我能使用更好的方法,请告诉我。准备好学习。
答案 1 :(得分:3)
Amit Manchanda的一些变化回答:
表源类是继承的UITableViewSource
ThreadPoolTaskExecutor
自定义PageRenderer类。
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
//Used command to excute and deselct the row and removed the table.
var command = _tableItems[**indexPath.Row**].Command;
command.Execute(_tableItems[**indexPath.Row**].CommandParameter);
tableView.DeselectRow(indexPath, true);
tableView.RemoveFromSuperview();
}
答案 2 :(得分:0)
非常感谢@Amit Manchanda和@Igor Tsapko。这对我来说效果很好。
我和@BatMaci有相同的问题。这是我使用MessagingCenter解决问题的方法:
在RightToolbarMenuCustomRenderer构造函数中,我订阅了来自MessagingCenter的消息:
public RightToolbarMenuCustomRenderer()
{
MessagingCenter.Subscribe<BaseViewModel>(this, AppConstants.str_iOS_TAP_OUTSIDE_CUSTOM_TOOLBAR, CloseRenderedControl);
}
public void CloseRenderedControl(object sender)
{
if (table != null)
ToolClicked(); // same as the ToolClicked() method Amit posted
}
然后,在我页面的标记中,我添加了一个带有TapGestureRecognizer的ContentView,它包装了视图的内容,并绑定到Command。
<ContentView>
<ContentView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OnTappedCommand}" />
</ContentView.GestureRecognizers>
<StackLayout>
<ListView .....
该命令是BaseViewModel上的ICommand,该ICommand在该VM的构造函数中初始化,以执行我的OnTappedMethod:
public BaseViewModel()
{
OnTappedCommand = new Command(OnTappedMethod);
}
public ICommand OnTappedCommand { protected set; get; }
public void OnTappedMethod()
{
if(Device.RuntimePlatform == Device.iOS)
MessagingCenter.Send<BaseViewModel>(this, AppConstants.str_iOS_TAP_OUTSIDE_CUSTOM_TOOLBAR);
}
这几乎只是模拟单击再次关闭的自定义控件。我想发布此消息,以节省一些时间。我应该指出,我是Xamarin的新手,并且仅在一台iOS设备(运行12.4.1的iPhone 7)上进行了测试。