2015-07-10 70 views
0

是否可以將Assembly版本加載到純XAML代碼中? 我發現這個在這裏:在Pure XAML中獲取組件版本GetExecutingAssembly

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) 
+0

感謝LightBulb審查佈局 – Seppl

回答

2

嘗試在第一個示例中使用GetEntryAssembly而不是GetExecutingAssembly。對我來說,這在設計器中顯示爲14.0.0.0,但在應用程序運行時是正確的。

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