inside
inside

Reputation: 3177

Get System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); from XAML

Is there a chance to get:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

from XAML code?

<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title=" **Here** " Height="700" Width="660"
        Name="myWindow" xmlns:my="clr-namespace:TestWpf">

</Window>

Thanks!

Upvotes: 0

Views: 2463

Answers (2)

Spontifixus
Spontifixus

Reputation: 6660

In the code-behind of your Window create a property returning that string:

public string Version
{
    get
    {
        return System.Reflection.Assembly
            .GetExecutingAssembly().GetName().Version.ToString();
    }
}

Then reference that from your XAML code:

<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{Binding Version, RelativeSource={RelativeSource Self}}" 
        Height="700" Width="660"
        Name="myWindow" 
        xmlns:my="clr-namespace:TestWpf">

</Window>

As far as I know there is no way to retrieve the version if the program currently running directly in XAML code. You will always have to use some kind of background code. That can be the codebehind as well as a view model, a MarkupExtension or some other kind of DataContext.

Upvotes: 0

galenus
galenus

Reputation: 2137

You can use MarkupExtension for this:

public class Version : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return System.Reflection.Assembly
                     .GetExecutingAssembly().GetName().Version.ToString();
    }
}

And to use it this way:

<Window x:Class="TestWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="{my:Version}" 
    Height="700" Width="660"
    Name="myWindow" 
    xmlns:my="clr-namespace:TestWpf">

</Window>

Upvotes: 1

Related Questions