Xamarin forms Resources designer.cs not creating using visual studio mac - xamarin

I'm facing a strange issue I have created a brand new xamarin forms project and i'm trying to adding Resource file in the shared project but when i add the Resource file then .resx extenshion file is created Like AppResources.resx creates but the code behind like AppResource.designer.cs not creating
Things that i have tried.
I have try multiple time and clean rebuild the project.
Create new project and try to again add resource file.
use custom tool to build the resource.

To manually fix the csproj, so it generates AppResources.designer.cs from AppResources.resx, follow these steps:
Close Solution (or quit Visual Studio).
Open YourProject.csproj in any text editor.
Find all lines that refer to AppResources.designer.cs or AppResources.resx.
Add or change as needed, to be similar to this (from https://github.com/xamarin/xamarin-forms-samples/blob/main/UsingResxLocalization/UsingResxLocalization/UsingResxLocalization/UsingResxLocalization.csproj):
<ItemGroup>
<Compile Update="Resx\AppResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>AppResources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resx\AppResources.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>AppResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
Save the csproj file.
Open your solution.

Related

Visual Studio - Prevent Creation of <Compile> and <None> tags when moving files in CSPROJ

In my CSPROJ I have a subfolder ›None‹. It is setup like this:
<ItemGroup Label="None">
<Compile Remove="None/**/*.cs" />
<None Include="None/**/*.cs" />
</ItemGroup>
In there I have CS files, that I don't want to compile.
No, I want to move a file (lets say ‌›CompileIt.cs‹ from ›None‹ back to the project, because now I want to compile it again.
If I move it outside of Visual Studio (like directly in the explorer or via PowerShell) it does exactly what I want, the file is now in a different folder inside the project and the Build-Action changes from ›None‹ to ›C# Compiler‹.
But if I move the file in Visual Studio, something different happens. Visual Studio changes the CSPROJ file like this:
<ItemGroup Label="None">
<Compile Remove="None/**/*.cs" />
<None Include="None/**/*.cs" />
</ItemGroup>
<ItemGroup>
<None Include="CompileIt.cs" />
</ItemGroup>
Now I have to either delete the ItemGroup from the CSPROJ again or change the "Build Action" in the Solution Explorer`s Properties page of the file.
Of course I know, this is usually exactly what you would expect, because normally a build action shouldn't change if you only move the file.
But anyway, is there a way to let Visual Studio know to not change CPSROJ file if I move a file inside it?
Or alternatively, is there a way to mark a subfolder in Visual Studio so that Visual Studios knows it should ignore all files in it?
PS: Oh, I forgot to say this, it is a Core-Style CSPROJ file, that means, all CS files are included automatically.

T4 templates not generating output in new VS2017 csproj projects

I migrated a project.json/.xproj project to the newer CS2017 .csproj format.
The project contains a T4 (.tt) template file.
It doesn't regenerate its output on save or build. The output .cs file isn't nested below the .tt file either.
Is there something I have to do to get this working?
.tt files are only auto-run by VS on save. You can install AutoT4 to have them run before/after build. (Be aware that at the moment there is a limitation with the new .csproj files - the options don't show up for them in the properties window.)
If you've converted from the old project.json/.xproj format, you may need to add the template to the project explicitly:
<ItemGroup>
<None Update="Foo.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Foo.cs</LastGenOutput>
</None>
<Compile Update="Foo.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Foo.tt</DependentUpon>
</Compile>
</ItemGroup>
Related GitHub issue
Edit
As mentioned in the comments below, you can do this quickly & easily by excluding, then including the template in your project.
I realise this is 2+ years old but for those bumping into this issue years on like me, the method listed below works for me without installing anything. I had the exact same issue, after upgrading a project from Visual Studio 2010 to Visual Studio 2017. YMMV. Make a backup copy of your .csproj file before you start.
Forcing rebuild of all .tt files when you build your project can be achieved without installing anything, by editing the .csproj project file. Editing the .csproj file seems clunky, but is is the approved way https://learn.microsoft.com/en-gb/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2015
Within your .csproj file, you will find lots of PropertyGroup nodes. At the end of the list of PropertyGroup nodes (position not critical), add another PropertyGroup node with this content:
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
Now look near the end of the .proj file, and you will see a line like this:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
(For interest, on my computer with VS2017 on it that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.CSharp.targets)
Beneath that line, add a line like this:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets" />
(For interest, on my computer that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets)
YMMV. If yours is a web project, there is probably a line nearby that is similar but to do with Microsoft.WebApplication.targets, from which you can draw inspiration.
That, possibly with a restart of Visual Studio, should do it. If you delete the transformed file that your .tt file emits, and then do a rebuild of your project, you should see that the emitted file reappears.

Web config transformation in visual studio 2012

I am using visual studio 2012 and I am finding it difficult to add web.config files for diff environment. I looked at this link and tried right clicking on .pubxml file and choose add config transform option. After I do this, system adds web.simplePublish.config file which I am not able to rename using visual studio.
If I rename the file using windows explorer, the file is not listed under web.config tree in the solution.
What is the right way to add web.config files for various environments in Visual Studio 2012.
you can try to modify the *.csproj then find out the filename before you rename it and to revise the filename that you using windows explorer.
or you can just try to add a new tag into *.csproj
<ItemGroup>
<Content Include="..\default.licenseheader">
<Link>default.licenseheader</Link>
</Content>
<Content Include="packages.config" />
<None Include="Properties\PublishProfiles\xxxx.pubxml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.SimplePublish.config">
<DependentUpon>Web.config</DependentUpon>
</None>
The other way that you can use Configuration Manager at your visual studio please refer the following link Add an Additional
I used SlowCheetah - XML Transforms plugin of visual studio to create web.config files for diff environment.

renaming resource files correctly for satellite assemblies

I just created a Visual Studio 2010 console application project, c#.
Then I added a folder named de.
Then I added a resx file called myres.resx to the main folder.
Then I translated the resx to german and copied this to the de folder and renamed the file to myres.de.resx
Visual studio created the satellite assembly almost correctly. What is wrong is the name of the resource in the satellite assembly. It is Projectname.de.myres.de.resources instead of Projectname.myres.de.resources. How can I remove the superfluous de?
Thanks for any hints.
For automatically generated code in your project you have a very little power to change the namespace and this usually follows the folder structure of your project.
You can, however, edit your files manually to change the namespace. See this question.
Thanks a lot, that helped.
I edited Projectname.csproj file:
<ItemGroup>
<EmbeddedResource Include="de\MyResource.de.resx">
<LogicalName>ResApp.MyResource.de.resources</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="MyResource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>MyResource.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>

Include Files using Wildcard into a folder in Visual Studio

I am using
<ItemGroup>
<EmbeddedResource Include="..\..\resources\hbm\*.hbm.xml" />
</ItemGroup>
to include a bunch of xml files into my C# project. Works fine.
But, I don't want them in the "root level" of my project, I would rather see them in a subfolder in my project.
For example, this file is included into a Mapping folder in Visual Studio:
<ItemGroup>
<EmbeddedResource Include="Mapping\User.hbm.xml" />
</ItemGroup>
That's what I want for my *.hbm.xml files.
I can't figure out how to do it and still keep my wildcard *.hbm.xml part and also keep the actual files in a different directory.
I've looked at MSDN's doc on MSBUILD and items, but no luck.
Perhaps this has changed in MSBuild since the original answer was posted, but it is possible to use both wildcards and links at the same time. For example, I use the following block in a C# project to import data files into a test library.
<ItemGroup>
<None Include="..\SOMENAME.Tests\data\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>data\%(RecursiveDir)%(Filename)%(Extension)</Link>
</None>
</ItemGroup>
The only disadvantage I've seen so far is that MSBuild recreates the folder names on the file system (ie $(ProjectPath)\data\somesubfolder) which is a little annoying but not a huge issue.
As a test, I also tried the OP's request of embedding resources, using the following snippet, and again this seemed to work fine - dotPeek showed the resources were present in the compiled assembly in addition to being present in the Solution Explorer.
<ItemGroup>
<EmbeddedResource Include="..\SOMENAME.Tests\data\**\*.*">
<Link>resources\%(RecursiveDir)%(Filename)%(Extension)</Link>
</EmbeddedResource>
</ItemGroup>
(This was using Visual Studio 2013 and still works as of VS2019)
Update 08Jun2021: The above syntax works is fine for old style csproj files, but if you are using the new SDK format, the syntax is a little different, albeit simpler.
The OP's original question of wildcard embedding can be accomplished with the following
<ItemGroup>
<EmbeddedResource Include="data\**\*.*" />
</ItemGroup>
And to have wildcard file copies for changed or missing files.
<ItemGroup>
<None Update="data\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
I think you can't use links and wildcard at the same time.
You could use this notation to link to include User.hbm.xml file in Mapping folder in Visual Studio :
<ItemGroup>
<EmbeddedResource Include="..\..\resources\hbm\User.hbm.xml">
<Link>Mapping\User.hbm.xml</Link>
</EmbeddedResource>
</ItemGroup>
But you can't do that
<ItemGroup>
<EmbeddedResource Include="..\..\resources\hbm\**\*.hbm.xml">
<Link>%(RecursiveDir)\%(Filename)%(Extension)</Link>
</EmbeddedResource>
</ItemGroup>
I have been able to create the logical name based on some regex (for only one folder depth). The root namespace I want is Agility.BmsData.Create and and example of the final name of the resource could be Agility.BmsData.Create.tables.mytable.sql where the file is in a folder tables\mytable.sql
<ItemGroup>
<EmbeddedResource Include="..\..\AgilityDatabase\Create\**\*.sql" >
<!-- see https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-well-known-item-metadata?view=vs-2022 -->
<!-- https://learn.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2022 -->
<LogicalName>
Agility.BmsData.Create.$(
[System.Text.RegularExpressions.Regex]::Match("%(Directory)", "(?<=\\)[^\\]+(?=[\\]*$)")
).%(Filename)%(Extension)
</LogicalName>
</EmbeddedResource>
</ItemGroup>
It is also possible to customise the Link attribute of the EmbeddedResource element but the value needs to be suitably encoded for embedding in an xml attribute.
Following on from the above example, I have sql files located in the database project on a relative path to the current project of ..\..\AgilityDatabase\Create\ and I want to mimic the file structure (and resource names) in my other project. I can use the following MsBuild command $([System.Text.RegularExpressions.Regex]::Match("%(Directory)", "(?<=\\AgilityDatabase\\Create\\).+$"))\%(Filename)%(Extension)
So my Item group becomes:
<EmbeddedResource Include="..\..\AgilityDatabase\Create\**\*.sql"
Link="Create\$([System.Text.RegularExpressions.Regex]::Match("%(Directory)", "(?<=\\AgilityDatabase\\Create\\).+$"))\%(Filename)%(Extension)">
</EmbeddedResource>

Resources