jyavenard
jyavenard

Reputation: 2095

How to display application title in XAML text field

I have a Windows Phone page code that is shared by multiple applications.

At the top of the page, I show the title of the application, like so:

Is it possible to make the text be bound to the application title as defined in the application's assembly?

I realise that I could do this by code by reading the title in the assembly and then doing something like:

this.ApplicationTitle.Text = title;

I was hoping that the title as defined in the assembly could be accessed with some magic like:

Text={assembly title}" directly from within the xaml.

Thanks

Upvotes: 0

Views: 1189

Answers (1)

Lzh
Lzh

Reputation: 3635

Create a property called ApplicationTitle that returns that name of the application like the following, and then bind to it in XAML:

public string ApplicationTitle
{
    get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; }
}

(You can use a relative binding source if you can't or don't want to use the data context.)

edit:

I just realized that my method involved security considerations since GetName is a method that is [Security Critical]. And I got a MethodAccessException stating: Attempt to access the method failed: System.Reflection.Assembly.GetName()

So here's another way to get the assembly name and return it in a property by using the AssemblyTitle attribute.

public string ApplicationTitle
{
    get
    {
        System.Reflection.AssemblyTitleAttribute ata =
            System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(
            typeof(System.Reflection.AssemblyTitleAttribute), false)[0] as System.Reflection.AssemblyTitleAttribute;
        return ata.Title;
    }
}

To bind in XAML, you can use this:

Text="{Binding ElementName=LayoutRoot, Path=Parent.ApplicationTitle}"

Upvotes: 1

Related Questions