Home Forums WPF controls Xceed Toolkit Plus for WPF coding a MaterialToast control in XAML and calling ShowToast() in C# code

Tagged: 

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • rebeldev
    Participant
    Post count: 7
    #44072 |

    I want to code a MaterialToast control in my View and call its ShowToast() method from the code-behind.

    View (with unnecessary code removed)

    
    <UserControl x:Class="Views.MyView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:commonui="clr-namespace:Common.UI.View;assembly=Common.UI"
                 xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">
        <Grid Name="MainGrid">
            <commonui:CommonListView x:Name="listViewGrid" />
            <xctk:MaterialToast Content="Delete Successful!" Location="Top" x:Name="myToast"
                                Height="200" Width="200" CornerRadius="2"
                                MaterialAccent="Green" DisplayTime="5000">
                
            </xctk:MaterialToast>
        </Grid>
    </UserControl>
    

    In my code behind, I have this code (with unnecessary code removed).

    
    namespace Views
    {
        public partial class MyView : UserControl
        {
            private void PopupToast()
            {
                myToast.ShowToast();
            }
        }
    }
    

    When I run this and it tries to execute ShowToast(), I get an ArgumentException with this message:
    “Must disconnect specified child from current parent Visual before attaching to new parent Visual.”

    Is it possible to code the whole MaterialToast control in XAML, and, if so, how do I code it (and where does the toast control go in the XAML), and how do I invoke it? Or must I code the toast control in C# code?

    Fawzi [Xceed]
    Member
    Post count: 722

    Hi,

    First, you have to know that the property DisplayTime is a TimeSpan. You can define it to be 5 seconds in XAML this way :
    <xctk:MaterialToast.DisplayTime>0:0:5</xctk:MaterialToast.DisplayTime>

    For the exception you get, it’s already fixed. The fix will be included in the next release : v3.2 Plus.

    In the meantime, you can create the MaterialToast in Code-Behind :
    private void Button_Click( object sender, RoutedEventArgs e )
    {
    var myToast = new MaterialToast();
    myToast.Content = “Delete Successful!”;
    myToast.Location = MaterialToastLocationEnum.Top;
    myToast.Height = 200;
    myToast.Width = 200;
    myToast.CornerRadius = new CornerRadius( 2 );
    myToast.MaterialAccent = Xceed.Wpf.Toolkit.MaterialControls.MaterialAccentEnum.Green;
    myToast.DisplayTime = new TimeSpan( 0, 0, 5 );

    myToast.ShowToast();
    }

    or to create it in XAML, add the fix:
    1) go in file Xceed.Wpf.Toolkit/MaterialControls/MaterialToast/Implementation/MaterialToastAdorner.cs
    a) in property Toast
    replace the content of the setter with :
    _toast = value;
    if( _toast != null )
    {
    var toastParent = _toast.Parent;
    if( toastParent != null )
    {
    var panel = toastParent as Panel;
    if( panel != null )
    {
    panel.Children.Remove( _toast );
    }
    var contentControl = toastParent as ContentControl;
    if( contentControl != null )
    {
    contentControl.Content = null;
    }
    var decorator = toastParent as Decorator;
    if( decorator != null )
    {
    decorator.Child = null;
    }
    }
    this.AddVisualChild( _toast );
    }

    b) Add the following method :
    internal void RemoveToast()
    {
    this.RemoveVisualChild( this.Toast );
    }

    2) go in file Xceed.Wpf.Toolkit/MaterialControls/MaterialToast/Implementation/MaterialToastAdornerManager.cs
    Replace the content of the method RemoveAdorner( MaterialToast toast ) with :
    this.RemoveEventHandlers( toast );

    var toastAdorner = this.GetAdorner( toast );
    if( toastAdorner != null )
    {
    this.RemoveAdornerFromList( toastAdorner );

    AdornerLayer layer = AdornerLayer.GetAdornerLayer( toast.GetOwner() is Window ? ( ( Window )toast.GetOwner() ).Content as Visual : toast.GetOwner() as Visual );
    if( layer != null )
    {
    layer.Remove( toastAdorner );
    }
    toastAdorner.RemoveToast();
    toastAdorner = null;
    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.