0
votes

I have page. In the page I have items - each one is a grid. In the grid i have few contentviews. In one of them - i have button.

Now I want try to display to user DisplayActionSheet. - But Because it's a content view - I cant call it -

I also tried App.Current.MainPage - but its doent help me.

P.S - Also question - maybe you know way to do something like this: http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_dropdown-menu&stacked=h In xamarin?

Thank you

1
You'll need a service or an event or a message to notify the owner page to show the action sheet - Sten Petrov
Can you give a link to example? - David

1 Answers

3
votes

This is just one way of doing it by using MessagingCenter.

In your ContentView you can going to publish a message when you want the containing page to perform a DisplayActionSheet. In the ContentView you can also subscribe to the result of the DisplayActionSheet if desired by having the ContentPage publish the result of the user's selection and whatever has subscribed to that message id (your ContentView maybe) can then react to it.

ContentView-based code:

var gridButton = new Button { Text = "This button is in a Content View" };
gridButton.Clicked += delegate
{
    var questions = new List<string> { "My ActionSheet Title", "Cancel", "Delete", "Share" };
    MessagingCenter.Send<object, List<string>>(this, "PhotoMessageQuestion", questions);
};

MessagingCenter.Subscribe<object, string>(this, "PhotoMessageAnswer", (sender, arg) =>
{
    System.Diagnostics.Debug.WriteLine("User choose: {0}", arg);
});

ContentPage-based code:

MessagingCenter.Subscribe<object, List<string>>(this, "PhotoMessageQuestion", async (sender, arg) =>
{
    var photoAction = await DisplayActionSheet(arg[0], arg[1], arg[2], arg[3]);
    MessagingCenter.Send<object, string>(this, "PhotoMessageAnswer", photoAction);
});

Results in:

enter image description here