.NET Maui The name 'InitializeComponent' does not exist in the current context - c#

I have created a ResourceDictionary file that comes with a .cs file. So afer creating the ResourceDictionary file I'm getting an error The name 'InitializeComponent' does not exist in the current context.
Below is the xaml file associated with the above file.
I'm using VS2022 17.5.0 Preview 1.0
I have set the build action for the c# file to C# Compiler
I have cleaned and rebuilt my project several times and tried deleting the bin and obj folders nothing seems to work

When I do Project / rt-click / Add / NewItem / .Net Maui ResourceDictionary (XAML), this is what gets added to .csproj:
<MauiXaml Update="Dictionary1.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
Note that the Compile is nested within the MauiXaml element; it is not a separate Item.
See if your .csproj has TWO separate Items, one for Dictionary1.xaml, another for Dictionary1.xaml.cs.
If so, replace those two items, with a combined item as shown above.
(For me, this builds without problem.)
In your case, the .csproj doesn't show MauiXaml BuildAction?
Replace:
<ItemGroup>
<None Include="Themes\Light.xaml" />
</ItemGroup>
with:
<ItemGroup>
<MauiXaml Update="Themes\Light.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>
I don't know whether this should be kept or removed:
<ItemGroup>
<Folder Include="Themes\" />
</ItemGroup>
Leave it in for now.

This sounds like a similar issue that often plagues Xamarin solutions. I think that there's an underlying bug, or delay in interpreting the files when displayed within Visual Studio.
Several options:
Make a small change to the file in question - even adding or deleting a space will do, save it and rebuild solution.
Clean the solution and rebuild.
Unload and reload project to which the files belong. (Right click on the project in Solution explorer window, select unload, then select load.
Xaml files may need to be set as embedded resource as their Build Action under their properties (again in Solution explorer).

Related

Include manually (not through VS) a file into csproj without being overridden

I am trying to develop a project that generates code. One of the code files it generates is sql code and I am trying to include it in one of the projects of my solution.
Since I didn't find yet a solution to do this programmatically, I tried to modify the .csproj manually:
<ItemGroup>
<Build Include="DIL\*.sql" />
</ItemGroup>
What my code does it generate a Test.Sql and places it into the DIL folder without including it in the project. If I then go modifying the .csproj as shown above and reload the .csproj itself I will find the Test.sql being added.
However there will be no Build Include="DIL*.sql" / anymore in the .csproj, meaning that if I generate another sql file it will not be added automatically to the project. Is there a permanent solution to this by any chance? Thanks in advance!

Why is Visual Studio creating .meta files even outside Assets folder?

I have the following project setup:
Unity project in C:\XXX
Added an another project to solution, located in C:\XXX\Tests
Now, whenever adding a class to that Test project, I found that a .meta file is being added as well.
This doesn't seem correct as .meta files are supposedly only for files inside Assets.
Using Process Monitor I discovered that indeed it is devenv.exe that is creating these .meta files.
Quick-fix:
Using ignores Visual Studio and Unity ignores, these .meta files are not committed to the repository,
And adding this to the csproj will hide them:
<ItemGroup>
<None Update="**/*.meta" Visible="false" />
</ItemGroup>
But in reality, this only hides the problem ...
And unfortunately, this approach won't work in Directory.Build.props, forcing one to manually add former code block to each of these 'external' projects.
Question:
Why are .meta files also created outside Assets and how to turn that off?
For now, putting this in Directory.Build.props at root effectively removes the need to manually update each project:
<Project>
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);**\*.meta</DefaultItemExcludes>
</PropertyGroup>
</Project>
But this only hides the problem instead of fixing it...
Update
The problem comes from Resharper and has been fixed:
Avoid creating meta files outside Assets and Packages (#1489)

How do I get the Xamarin build process to process my XAML files?

I have two Xamarin.Forms projects making use of XAML pages. We'll call them ProjectOK and ProjectBroken.
ProjectOK builds okay. ProjectBroken fails, saying it cannot find InitializeComponent. Investigation (by increasing build output to Normal) has revealed that this is because ProjectBroken is not processing my XAML files at all. In the build output of ProjectOK, I get:
_CoreXamlG:
Source: Page1.xaml
Language: C#
AssemblyName: App1
OutputFile obj\Debug\App1.Page1.xaml.g.cs
ProjectBroken contains no such lines.
In Solution Explorer, the properties for the XAML pages in both projects are identical. Page1.xaml file settings are: Embedded Resource, Do not copy, Custom Tool=MSBuild:UpdateDesignTimeXAML. Page1.xaml.cs settings are: Compile, Do not copy.
Opening the project files, I find that ProjectOK contains:
<Compile Include="Page1.xaml.cs">
<DependentUpon>Page1.xaml</DependentUpon>
</Compile>
And later:
<ItemGroup>
<EmbeddedResource Include="Page1.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
ProjectBroken contains no references to my newly created Page1.xaml.
Even if I manually add the lines above to the ProjectBroken.csproj file, it still does not invoke the XAML compiler.
So, it seems that something is broken outside of my solution folder. Does anyone have any idea what this might be, and - more importantly - how can I 'reset' it?

Setting the Build Action for a folder in VS for a Win 8.1 app

In previous Windows 8 /8.1 projects, any images that I have added to a folder inside the Assets folder is automatically set to "Content" in the Build Action.
As a result, the images render when the project is built.
But our latest project seems to default to BundleResource, and after wasting time trying to work out why the image isn't rendering, I remember that I have to manually change each image to Content.
Does anyone know how to set the whole folder to Content so that newly added images will be Content by default? I don't know why this project is behaving differently to our previous ones. :-(
I had the same issue while ago. As far as I know is not possible to set Build Actions for folders on Visual Studio, only individual files.
The solution I've found is to edit Project.projitems manually (I mean, outside of VS and after it is closed, via another code editor) from:
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Assets\example.json" />
<Content Include="$(MSBuildThisFileDirectory)Assets\NestedFolder\example.file" />
</ItemGroup>
to this:
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Assets\**\*.*">
</ItemGroup>
Which will do the trick of setting everything under Assets as Build Action: Content, and will be able to see it mapped on Solution Explorer after restarting VS - or rebuilding files.
NOTE: Keep in mind that if you add anything to this folder via VS this rule could be overwritten. So, my advice is: once this rule is added, only add files directly to the folder.

How can I connect xaml and xaml.cs files

I had a VB projected and converted it to C# using online conversion tools. Now the problem is xaml and xaml.cs file do not connect to each other, that is they don't recognize their dependencies (Red area in Fig). Actually it should appear like Window1 Files (Green Area in the image.) How can I achieve this.
I am trying my hands on WPF so may be a layman sort of question.
This is simple, try to add in project existing items and select the XAML (not .cs, etc.) files in list of formats. In VS2010 thats helps.
If you cannot get the IDE to do it (Papa John's post), then you can do it by editing the project file.
That information is in the .csproj file (which is an XML file -- you can open it in a text editor, or by right-clicking on it, choosing "unload", and then opening it -- choose reload to load it up as a project again).
Here is the information to look for, you would need to add the "DependentUpon" tag.
<Compile Include="TheFile.xaml.cs">
<DependentUpon>TheFile.xaml</DependentUpon>
</Compile>
Easiest Way!!!
I came across the same. I got the way out. Here is how to get the .xaml.cs nested under the .xaml in Solution Explorer:
In Windows File Explorer (outside of Visual Studio), open the folder where the required files are.
Select both files (.xaml and .xaml.cs) together.
Drag it onto your project name in the Solution Explorer.
Its done! :)
Using a Xamarin PCL Solution:
1) Go to your PCL folder and open your MySolution.csproj file
2) There should be several groups of <ItemGroup> tags. One of them declares <EmbeddedResource> tags and another will contain, <Compile> <DependentUpon></DependentUpon></Compile> groups of tags.
3) For MyPage.xaml and MyPage.xaml.cs files to be linked, you must have a group of xmls that declare your xaml page.
<EmbeddedResource Include="MyPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<LogicalName>MyPage.xaml</LogicalName>
</EmbeddedResource>
<Compile Include="MyPage.xaml.cs">
<DependentUpon>MyPage.xaml</DependentUpon>
</Compile>
Note that if your page is in a folder you should specify that like so:
<Compile Include="Views\MyPage.xaml.cs">
<DependentUpon>MyPage.xaml</DependentUpon>
</Compile>
<EmbeddedResource Include="Views\MyPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<LogicalName>MyPage.xaml</LogicalName>
</EmbeddedResource>
Note that this works with OSX and Windows
Based on Kyle White's comment on the official Xamarin bug report 55591: .xaml files in .NETStandard library appear twice in solution explorer, I found a simple solution to this problem within the linked .NET Standard sample project by Oren Novotny
Within your .csproj file, add the following <ItemGroup>:
<ItemGroup>
<Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
<EmbeddedResource Include="**\*.xaml" SubType="Designer" Generator="MSBuild:UpdateDesignTimeXaml" LogicalName="%(Filename)%(Extension)" />
</ItemGroup>
Afterwards, all xaml files within the project structure will be displayed in the Solution Explorer window automatically - even if you'll ever add new xaml files.
How does this magic work?
The Compile element within the ItemGroup is using wildcards to iterate through all directories, searching for .xaml.cs files and marking them as dependent on the xaml files of the same name. Please note that this works only because the %(Filename) item metadata used for the DependentUpon element contains the left-most file extension, which matches the name of the xaml file by convention.
The EmbeddedResource element will include all xaml files to the project, so that they are visible within the Solution Explorer window while marking them as Designer files, and declaring that the UpdateDesignTimeXaml target defined within the Xamarin.Forms NuGet package should be used to generate code from the markup file.
Using Xamarin Shared Code solution:
1) Go to you project folder after unloading the shared project
2) Find the projitems file and edit that adding the DependentUpon tag as described in other answers above.
3) Save the file
4) Go back to visual studio and you should get a dialog that allows you to reload all or just open the project again.
An even easier and faster solution for Xamarin Forms projects, no need to touch csproj file at all, very quick fix.
Make sure you have Show All Files selected for solution explorer - it may be on by default.
Select all affected files
Right click > Exclude from Project
Select the same files again (should be faded out)
Right Click > Include in Project
They should now all be nested correctly and all the changes necessary to the .csproj file will be done.
You may have an InitializeComponent() does not exist in the current context) error after this.
If that's the case, the simple fix is..
Select all affected items and change Build Action from Page to Embedded Resource
I was able to just restart Visual Studio for Mac 2019 v16.2 and it reconnected my .xaml & .cs files that should've been connected.
If that doesn't work, Hitsa's solution worked for me as well on a separate occasion.
If you get the Nested File VS Extension, you can do this by right clicking similarly named files and choosing to "nest automatically" or you can nest manually.
https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
In a Shared Project there is .shproj file instead of a .csproj file and the and items do not exist there.
However I found there is also a .projitems file and adding a section there as described above caused the .xaml and .cs files to be linked
The usual - Close Visual Studio, delete the vs folder in the root folder of your solution and reopen Visual Studio (2019) just worked for me.
For VS 2019, you have only to edit .csproj file and eliminate all references to .xaml.cs file
es:
<ItemGroup>
<Folder Include="Models\" />
<Folder Include="Services\" />
<Folder Include="ViewModels\" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Views\MyPage.xaml">
<LogicalName>MyPage.xaml</LogicalName>
</EmbeddedResource>
</ItemGroup>
I found the problem was when i reloaded a github project from my menu, instead of the actual project file inside that local downloaded repo. Once I selected the .sln file it worked again!

Categories

Resources