You are here:   Blog
Register   |  Login

Jan 20

Written by: kchristo
Friday, January 20, 2012  RssIcon

Reading this article in MSDN forums and taking the best (I hope) of what LR_ and Michael Washington offered as solutions I ended up with an extension class that allows you to put your application’s logo on the Ribbon Bar of any shell having one. You also have the choice to put it either on the left or the right. LR_’s original code was only modified in 2 points.

  1. You can decide if you want you Application Logo on the left or on the right of your commands.
  2. You don’t need to pass a URL to the image. The application’s logo (defined in project properties) is bound inspired by the XAML provided by Michael Washington.

I even left the LeftButtonDown part as it’s a good idea.

Although implied from the detailed reference to them I want to thank both guys for making it so easy for me to put my logo on screen.

    private static class LogoPlacement

    {

         public static void AddLogo(this Microsoft.LightSwitch.Client.IClientApplication application, System.Windows.HorizontalAlignment alignment) {

           Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() => { LogoPlacement.AddLogo(System.Windows.Application.Current.RootVisual, alignment); });

         }

      internal static void AddLogo(UIElement element, System.Windows.HorizontalAlignment alignment) {

        if (rcb != null) return;

 

        for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(element); i++) {

          if (rcb != null)

            return;

          UIElement child = (UIElement)System.Windows.Media.VisualTreeHelper.GetChild(element, i);

          AddLogo(child, alignment);

        }

        if (element is Microsoft.LightSwitch.Runtime.Shell.Implementation.Standard.RibbonCommandBar) {

          rcb = element as Microsoft.LightSwitch.Runtime.Shell.Implementation.Standard.RibbonCommandBar;

          Image myImage = new Image() {

            Stretch = System.Windows.Media.Stretch.Uniform,

            Margin = new Thickness(2,8,14,8),

            HorizontalAlignment = alignment,

            Cursor = System.Windows.Input.Cursors.Hand

          };

          myImage.SetValue(ComponentViewModelService.ViewModelNameProperty, "Default.LogoViewModel");

          myImage.SetBinding(Image.SourceProperty, new System.Windows.Data.Binding { Path = new PropertyPath("Logo") });

          myImage.MouseLeftButtonUp += (s, e) => MessageBox.Show("Here may be some About...");

          myImage.SizeChanged += (s, e) => {

            double left = (s as Image).HorizontalAlignment == HorizontalAlignment.Left ? e.NewSize.Width + 10.0 : 0.0;

            double right = (s as Image).HorizontalAlignment == HorizontalAlignment.Right ? e.NewSize.Width + 10.0 : 0.0;

            rcb.Padding = new Thickness(left, 0, right, 0);

          };

          ((Grid)rcb.Parent).Children.Add(myImage);

        }

      }

 

      private static RibbonCommandBar rcb = null;

    }

Tags:
Categories:

16 comment(s) so far...


Gravatar

Re: Application Logo

Here's the same code running it through a C# converter:

Private NotInheritable Class LogoPlacement
Private Sub New()
End Sub


_
Public Shared Sub AddLogo(application As Microsoft.LightSwitch.Client.IClientApplication, alignment As System.Windows.HorizontalAlignment)

Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(Function()
LogoPlacement.AddLogo(System.Windows.Application.Current.RootVisual, alignment)

End Function)

End Sub

Friend Shared Sub AddLogo(element As UIElement, alignment As System.Windows.HorizontalAlignment)

If rcb IsNot Nothing Then
Return
End If

For i As Integer = 0 To System.Windows.Media.VisualTreeHelper.GetChildrenCount(element) - 1

If rcb IsNot Nothing Then

Return
End If

Dim child As UIElement = DirectCast(System.Windows.Media.VisualTreeHelper.GetChild(element, i), UIElement)


AddLogo(child, alignment)
Next

If TypeOf element Is Microsoft.LightSwitch.Runtime.Shell.Implementation.Standard.RibbonCommandBar Then

rcb = TryCast(element, Microsoft.LightSwitch.Runtime.Shell.Implementation.Standard.RibbonCommandBar)
Dim myImage As New Image() With { _
Key .Stretch = System.Windows.Media.Stretch.Uniform, _
Key .Margin = New Thickness(2, 8, 14, 8), _
Key .HorizontalAlignment = alignment, _
Key .Cursor = System.Windows.Input.Cursors.Hand _
}

myImage.SetValue(ComponentViewModelService.ViewModelNameProperty, "Default.LogoViewModel")

myImage.SetBinding(Image.SourceProperty, New System.Windows.Data.Binding() With { _
Key .Path = New PropertyPath("Logo") _
})

myImage.MouseLeftButtonUp += Function(s, e) MessageBox.Show("Here may be some About...")

myImage.SizeChanged += Function(s, e)

Dim left As Double = If(TryCast(s, Image).HorizontalAlignment = HorizontalAlignment.Left, e.NewSize.Width + 10.0, 0.0)

Dim right As Double = If(TryCast(s, Image).HorizontalAlignment = HorizontalAlignment.Right, e.NewSize.Width + 10.0, 0.0)


rcb.Padding = New Thickness(left, 0, right, 0)

End Function


DirectCast(rcb.Parent, Grid).Children.Add(myImage)
End If

End Sub

Private Shared rcb As RibbonCommandBar = Nothing

End Class

By fishy on   Friday, January 20, 2012
Gravatar

Re: Application Logo

That was REALLY fast fishy! Respect!

By kchristo on   Friday, January 20, 2012
Gravatar

Re: Application Logo

I haven't tested it.

By the way, thank you so much for all the work you've done. I really appreciate it.

I am going to be adding your code to my commercial product.

By fishy on   Friday, January 20, 2012
Gravatar

Re: Application Logo

I got so much help from the community when staring with LS that giving back to the community is an obligation to me. Glad to know my code is helpful. As soon as I find the time I will also be publishing the commercial version of CLASS Extensions. A have a lot of goodies added since the free version.

By Kostas Christodoulou on   Friday, January 20, 2012
Gravatar

Which references?

Hi Kostas,
Can't wait trying it out.

I understand correctly that your extension class can be used directly in the client project of a LS project?
Which references are required. Is it necessary to have the extension framework installed?

Thank a lot !
paul.

By paul van bladel on   Saturday, January 21, 2012
Gravatar

Re: Application Logo

Sorry, I was lazy, I could have find myself which references to add: it's the Microsoft.LightSwitch.Client.Internal.dll.

I tried first to use the extension method in the Application_Initialize() method, but that's not working.
In your start up screen it works great !
e.g.:
partial void SearchCustomers_Run(ref bool handled)
{
this.AddLogo(System.Windows.HorizontalAlignment.Left);

}

Thanks Kostas. You are the Extension Method Master !

By paul van bladel on   Saturday, January 21, 2012
Gravatar

Re: Application Logo

Sorry for any trouble Paul. I could (and probably should) inlude references and "usings". I should also mention the start up screen thing. It was mentioned in original LR_ post in the thread that caused all these. Also another idea (more generic than startup screen since you might not want to have one) would be upon application initialization hooking (via IServiceProxy.NotificationsService if I am not mistaken) to ScreenOpened or ScreenActivated (I don't recall right now) and run on every screen as after the first time calling the method will do nothing at all.

By Kostas Christodoulou on   Saturday, January 21, 2012
Gravatar

Re: Application Logo

No trouble at all, Kostas. I'm very happy to have finally a decent and simple way to add a logo.
Still some questions.
In the app properties, it seems to be possible to load multiple logo's. Do you know if it's possible to choose one programmatically?
Is it anyhow possible to set the logo image programatically?
Enjoy the weekend.
paul.t

By paul van bladel on   Saturday, January 21, 2012
Gravatar

Re: Application Logo

I' m afraid I cannot help you with this multiple logo thing. At least not at the time being. I will look into it though.

By kchristo on   Saturday, January 21, 2012
Gravatar

Re: Application Logo

Paul, regarding the multiple logo issue. I am afraid you are mistaken. You cannot have multiple logos. You can select one from the assembly's resources. The window opening shows all available resources. And you can set your one and only logo either from the existing resources or add a new one. The question whether you can bind to any resource of the assembly and not only the one selected for logo (binding through the logo mechanism) is a different one and it's also discussed in the original thread. Take a look there.Hope this helps.

By kchristo on   Wednesday, January 25, 2012
Gravatar

Re: Application Logo

Thank you, your code works perfect.

By Keith Craigo on   Thursday, January 26, 2012
Gravatar

Re: Application Logo

You are welcome Keith. I am glad that my contribution is appreciated :-)

By kchristo on   Thursday, January 26, 2012
Gravatar

Re: Application Logo

That is exactly what I need, however...
- I need the code in VB
- Can you tell me where to add this code?
Thanks for your help

By Edgar on   Friday, January 27, 2012
Gravatar

Re: Application Logo

Edgar if you read the comments above you will find the code in VB AND when to call the code. The answer to where to put the code is in a separate file in your client project or any other libraby project your client project has access to.

By kchristo on   Saturday, January 28, 2012
Gravatar

Re: Application Logo

1) i cannot add system.drawing reference to Client Reference, it said only silverlight assemblies will work with this project

2) what assembly do i need to add for Casting ((Grid) rcb.Parent), since in Application.cs cannot recognize Grid.

Thanks a lot!

By TripleStar on   Friday, April 13, 2012
Gravatar

Re: Application Logo

You don't need System.Drawing. If you are looking for Image it's System.Windows.Controls. This also what you need for Grid.
Here http://code.msdn.microsoft.com/Application-Logo-Demo-8edb214d you can find a complete sample of using the code.

By kchristo on   Friday, April 13, 2012

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation