Msbuild: %(Filename) includes extension for XAML files

Created on 1 Nov 2017  路  2Comments  路  Source: dotnet/msbuild

I am trying to nest my ViewModels under my XAML files using the following naming convention PageNameViewModel.cs

I was trying to create the nesting by using this:

<Compile Update="**\%(Filename)ViewModel.cs" DependentUpon="%(Filename).xaml" />

The problem I am facing is that the %(Filename) variable includes the .xaml extension (MainPage.xaml), which according to the documentation, it shouldn't. (https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-well-known-item-metadata)

This means that my statements are the same as:

<Compile Update="**\MainPage.xamlViewModel.cs" DependentUpon="MainPage.xaml.xaml" />

Obviously, this is not what I am wanting to achieve. How can I get just the file name and not include the XAML extension?

Most helpful comment

We have had similar issues with this. We ended up using something like:

<Compile Update="**\*ViewModel.cs" DependentUpon="$([System.String]::Copy('%(Filename)').Replace( 'ViewModel', '.xaml' ))" />

I can confirm that it works with the current version of visual studio - 15.6.3.

All 2 comments

So I tried to use a workaround, but it doesn't seem to work either:

<Compile Update="**\$([System.String]::new('%(Filename)').Split('.').GetValue(0))ViewModel.cs" DependentUpon="%(Filename)" />

ANy ideas where I am going wrong?

We have had similar issues with this. We ended up using something like:

<Compile Update="**\*ViewModel.cs" DependentUpon="$([System.String]::Copy('%(Filename)').Replace( 'ViewModel', '.xaml' ))" />

I can confirm that it works with the current version of visual studio - 15.6.3.

Was this page helpful?
0 / 5 - 0 ratings