AppAlertService that wraps multiple Image sources

2 posts, 0 answers
  1. Graeme
    Graeme avatar
    21 posts
    Member since:
    May 2012

    Posted 25 Aug 2015 Link to this post

    I just wanted to share a simplified version of a wrapper service for the DesktopAlert that we use to handle Images from multiple sources - see below.

     I trust that someone will find this useful.

    Enjoy! 

    Graeme

     

    1. Resource Image references

    2.                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    3.    <BitmapImage x:Key="MailImage" UriSource="Images/Mail.png" />
    4.    <BitmapImage x:Key="ContactImage" UriSource="Images/Contact.png" />
    5.</ResourceDictionary>

     

    2. Type References to images

    1.public enum AppAlertIconType
    2.{
    3.    Mail,
    4.    Contact,
    5.    Url = -1
    6.}

     

    3. Alert service interface (we use MEF)

    1.public interface IAppAlertService
    2.{
    3.    void ShowMessage(string header, string content, string imageUrl, int showDuration = 5000, double width = 48, double height = 48);
    4.    void ShowMessage(string header, string content, AppAlertIconType icon, string imageUrl = "", int showDuration = 5000, double width = 48, double height = 48);
    5.    void ShowMessage(string header, string content, Image image, int showDuration = 5000, double width = 48);
    6.    void CloseAllAlerts();
    7.}

     

    4. Alert Service

    01.public class AppAlertService : IAppAlertService
    02.{
    03.    RadDesktopAlertManager manager = new RadDesktopAlertManager(AlertScreenPosition.TopRight);
    04. 
    05.    public void ShowMessage(string header, string content, string imageUrl, int showDuration = 5000, double width = 48, double height = 48)
    06.    {
    07.        this.ShowMessage(header, content, AppAlertIconType.Url, imageUrl, showDuration, width, height);
    08.    }
    09. 
    10.    public void ShowMessage(string header, string content, AppAlertIconType icon, string imageUrl = "", int showDuration = 5000, double width = 48, double height = 48)
    11.    {
    12.        Func<AppAlertIconType, Image> GetResourceImage = new Func<AppAlertIconType, Image>(t => new Image { Source = Application.Current.FindResource(string.Format("{0}Image", t.ToString())) as ImageSource, Width = width, Height = height });
    13.        Func <string, Image> GetWebImage = new Func<string, Image>(t => new Image { Source = new BitmapImage(new Uri(t, UriKind.Absolute)), Width = width, Height = height });
    14.        this.ShowMessage(header, content, icon == AppAlertIconType.Url ? GetWebImage(imageUrl) : GetResourceImage(icon), showDuration, width);
    15.    }
    16. 
    17.    public void ShowMessage(string header, string content, Image image, int showDuration = 5000, double width = 48)
    18.    {
    19.        manager.ShowAlert(new DesktopAlertParameters
    20.        {
    21.            Header = header,
    22.            Content = content,
    23.            Icon = image,
    24.            IconColumnWidth = width,
    25.            IconMargin = new Thickness(10, 0, 20, 0),
    26.            ShowDuration = showDuration
    27.        });
    28.    }
    29. 
    30.    public void CloseAllAlerts()
    31.    {
    32.        manager.CloseAllAlerts();
    33.    }
    34.}

     

    5. Usage

    1.IAppAlertService alertService = new AppAlertService();
    2.alertService.ShowMessage("header", "content goes here", AppAlertIconType.Mail);
    3.alertService.ShowMessage("header", "content goes here", AppAlertIconType.Url, "http://greywolf.critter.net/images/gallery/critters/2007-09-14-happy-pink-unicorn.jpg");

  2. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 26 Aug 2015 Link to this post

    Hi Graeme,

    Thanks for sharing your solution with the community. If you would like to, you could submit a sample runnable project demonstrating the exact approach in our WPF Code Libraries, so everyone can easily test it.

    I have updated your Telerik points for your involvement.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top