Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Custom Popup/Alert with content from XAML

$
0
0

While working with Xamarin Forms, we are not happy with the solutions for item selection with a list of items, the Picker control. There is no binding with it and while there is a remedy for this, it's not true binding to a backing list.
What we'd like to do is be able to create/display a popup with custom content. Using dependency injection, we've defined a new navigation interface:

public interface INavigationService: INavigation
    {
        event EventHandler Navigated;
        Task PushAsync<T>(object viewModel) where T : Page, new();
        Task PushModalAsync<T>(object viewModel) where T : Page, new();
        void Popup<T>(object viewModel, string title, string positiveText, string negativeText, Action onPositive, Action onNegative) where T : ContentPage, new();
        Task SelectList<T>(object viewModel) where T : ListView, new();
    }

Right now only concerned with the Android implementation. Based on other code and examples, it looks like we should be able to get a renderer for the Page (which is written in XAML), set it's element and then put it's view into an AlertDialog to present in Android:

var layout = new T();
                layout.BindingContext = viewModel;
                IVisualElementRenderer renderer = new PageRenderer();
                renderer.SetElement(layout);
                var viewGroup = renderer.ViewGroup;
                var builder = new AlertDialog.Builder(viewGroup.Context);
                builder.SetTitle(title);
                builder.SetView(viewGroup);
                builder.SetPositiveButton(positiveText ?? "OK", (s, de) => onPositive());
                if (!string.IsNullOrEmpty(negativeText))
                    builder.SetNegativeButton(negativeText, (s, de) => onNegative());
                builder.Show();

At the renderer.SetElement(layout), a NullReferenceException is thrown. It looks like the RendererHandler is using Activator.CreateInstance for a constructor that isn't on the object it's trying to construct.
Getting a bit frustrated, any ideas or maybe a different approach we need to take?


Viewing all articles
Browse latest Browse all 58056

Trending Articles