获取纯XAML的汇编版本

本文关键字:汇编 版本 XAML 获取 | 更新日期: 2023-09-27 18:02:06

是否可以在纯XAML代码中加载程序集版本?我在这里找到了这个:

http://www.widecodes.com/0mHWVVkWeV/is-it-possible-to-bind-a-textblocks-text-to-an-applications-assemblyversion.html

但是总是返回4.0.0

<TextBlock xmlns:ref="clr-namespace:System.Reflection;assembly=mscorlib">
    <TextBlock.Text>
        <Binding Path="Version">
            <Binding.Source>
                <ObjectDataProvider MethodName="GetName">
                    <ObjectDataProvider.ObjectInstance>
                        <ObjectDataProvider MethodName="GetExecutingAssembly"
                                            ObjectType="{x:Type ref:Assembly}" />
                    </ObjectDataProvider.ObjectInstance>
                </ObjectDataProvider>
            </Binding.Source>
        </Binding>
    </TextBlock.Text>
</TextBlock>

然后我在这里尝试了这个:(如果应用程序运行,它可以完美地工作)

<TextBlock Grid.Row ="0" Grid.Column="0" xmlns:ref="clr-namespace:System.Reflection;assembly=mscorlib" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <TextBlock.Text>
        <Binding Path="Version">
            <Binding.Source>
                <ObjectDataProvider MethodName="GetName">
                    <ObjectDataProvider.ObjectInstance>
                        <ObjectDataProvider MethodName="ReflectionOnlyLoadFrom"
                                            ObjectType="{x:Type ref:Assembly}">
                            <ObjectDataProvider.MethodParameters>
                                <sys:String>.'Modules'Myassembly.dll</sys:String>
                            </ObjectDataProvider.MethodParameters>
                        </ObjectDataProvider>
                    </ObjectDataProvider.ObjectInstance>
                </ObjectDataProvider>
            </Binding.Source>
        </Binding>
    </TextBlock.Text>
</TextBlock>

但是现在设计器说(想要在System32中找到Assembly)

Could not load file or assembly 'file:///C:'Windows'system32'Modules'MyAssembly.dll' or one of its dependencies. The system cannot find the file specified.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

获取纯XAML的汇编版本

尝试在第一个示例中使用GetEntryAssembly而不是GetExecutingAssembly。对我来说,这在设计器中显示为14.0.0.0,但在应用程序运行时是正确的。

    <ObjectDataProvider MethodName="GetName">
        <ObjectDataProvider.ObjectInstance>
            <ObjectDataProvider MethodName="GetEntryAssembly" ObjectType="{x:Type ref:Assembly}" />
        </ObjectDataProvider.ObjectInstance>
    </ObjectDataProvider>