0

I have been trying to get similar functionality to an Android "toast" using Xamarin forms. After looking around, I found what I think is a good solution. The general approach appears to be to make a new Absolute layout, and make it appear for a set time, then disappear. While I think I generally understand what is being done, I can't seem to get it to work. Can anyone suggest how I would use this class if I want to make a toast appear in my MainPage? Should I be adding an AbsoluteLayout in the XAML file? Sorry, I'm sure this is a simple question, but I can't really figure out what to do...

Any help would be greatly appreciated!

public static class Popper
{
    public async static Task Pop (string message, AbsoluteLayout attachLayout, int showforMilliseconds = 1500)
    {
        var container = new StackLayout
        {
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            BackgroundColor = Color.FromHex ("#DDEFEFEF"),
            Padding = 10
        };

        var label = new Label
        {
            Text = message,
            FontAttributes = FontAttributes.Bold,
            Style = (Style)Application.Current.Resources["PopupText"]
        };

        container.Children.Add (label);

        container.Scale = 0;
        container.Opacity = 0;

        attachLayout.Children.Add (container, attachLayout.Bounds, AbsoluteLayoutFlags.PositionProportional);
        container.ScaleTo (1.0f, 100);
        container.FadeTo (1.0f, 100);

        await Task.Delay (showforMilliseconds);

        container.ScaleTo (0.0f, 250);
        await container.FadeTo (0.0f, 250);
        attachLayout.Children.Remove (container);
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
TwoChain
  • 1
  • 5
  • 1
    Possible duplicate of [Toast equivalent on Xamarin Forms](https://stackoverflow.com/questions/35279403/toast-equivalent-on-xamarin-forms) – EvZ May 07 '18 at 06:32
  • Is this really a duplicate? I'm posting specific code, and asking for help on using it. I'm not clear on how to implement the AbsoluteLayout that is called for in the Pop method signature. – TwoChain May 07 '18 at 15:27
  • One of the advantages of the approach I am asking about is that customizing the toast should be relatively easy. Thus, I would be very interested if anyone could provide some guidance. – TwoChain May 07 '18 at 17:56

1 Answers1

1

On Android you don't have to reinvent the wheel since Toast exists natively. On other platforms there is no such thing like Toast, therefor there is no silver-bullet solution here. This problem have been solved by multiple people in multiple ways, thats why I left a comment that your question might be a duplicate of existing thread with multiple examples.

Now about your idea. Your implementation is working, however it will show Toast only on an AbsoluteLayout. Why to set such a restriction? If you will recheck the link I shared in comments you will find a more appropriate and elegant solutions.

I can't seem to get it to work.

All you need is a an AbsoluteLayout on your page so you could call your method:

await Popper.Pop("Hello world", referenceToYourAbsoluteLayout, 5000);

If you still for some reason want to stick to this exact solution, maybe it will make sense to have an extension method instead. However this solution just does not make sense for the average user.

P.S: Once again, please check the existing thread for more information and details.

P.S.S: Usage example of your code snippet

<!-- XXXPage.xaml -->
<?xml version="1.0" encoding="utf-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:XXX"
    x:Class="XXX.XXXPage">
    <AbstractLayout x:name="myLayout />
</ContentPage>

// XXXPage.xaml.cs
public partial class XXXPage : ContentPage
{
    public Test999Page()
    {
        InitializeComponent();

    }

    async protected override void OnAppearing()
    {
        base.OnAppearing();
        await Popper.Pop("Hello world", myLayout, 5000);
    }
}
EvZ
  • 11,889
  • 4
  • 38
  • 76
  • Hi EvZ, Thanks for your suggestion. My question is: Where do I put the AbsoluteLayout? You say to put it on my "page". From this, I understand that I should either: 1) nest an AbsoluteLayout in my MainPage.xaml file, or programatically create a new AbsoluteLayout in my MainPage.cs file. Which is best? Also, the reason I think this approach is useful is that (at least for me) it seems it will be much easier to create a "custom toast" (e.g. font size, colour, size, etc.) on multiple platforms. – TwoChain May 08 '18 at 20:02
  • "XAML or programmatically?" -> What is better an apple or a melon? Matter of taste or of what will suit you the best. "e.g. font size, colour, size, etc." using custom renderers / platform specific implementations you will have a wider range of customizations. – EvZ May 08 '18 at 20:08
  • Apples are far better. I've yet to find a melon that I consider palatable. Back to the code discussion, I was under the impression that XAML was better for this type of thing, as it is preferable based on the MVVM approach... Not really sure though, but just wanted to check. – TwoChain May 09 '18 at 20:39
  • @ EvZ, So... I'm still kind of stuck. Let's say I want to use xaml. From what I understand, I would add a new xaml file to the main project, and put my 'absolutelayout' inside this xaml file? Then call this file using "await Popper.Pop" method? This is what I've tried, but I can't seem to get anything to work. I can't actually call the absolutelayout (generated in a different file) from my MainPage. I know this is probably very simple, but I'm quite stuck. Any suggestions? – TwoChain May 10 '18 at 00:32
  • From your questions I understand that you misunderstood basic concepts of Xamarin.Forms and you don't understand the shared code snippet, therefor I would highly recommend to re-read or get familiar with Xamarin.Forms official documentation. I updated my answer and provided a very basic example. MVVM can be done without XAML, read about it. Since you are new to SO please get familiar with the next concept: https://stackoverflow.com/help/how-to-ask and please keep in mind that SO is not a code writing service provider. Good luck! – EvZ May 10 '18 at 06:26
  • Thanks.... but what you suggested is actually what I tried originally and it didn't seem to work. The whole point of this approach is that if the xaml is built using a StackLayout (which mine is), and you don't want to re-write with an AbsoluteLayout, you can use this method to attach to ANOTHER AbsoluteLayout. What you have written works fine if the entire page is written using AbsoluteLayout. I'll keep playing around and let you know once I figure it out. Thanks! – TwoChain May 10 '18 at 19:08