Xamarin Resource designer does not update properly - c#

I have a Visual Studio solution containing a Xamarin Android project.
Since the migration to VS17, the Resource designer from Xamarin started behaving strangely.
If I add a new XML layout in the Resources/layout folder, the Resource Designer will only notice it after I exclude, and re-include the new file.
However, if these steps are reproduced with a second new file, the Resource designer will notice the second new file, but the first one will be erased.
This makes refactoring impossible, and leads to completely corrupted project.
Has anyone a workaround for this ?
Visual studio 2017 entreprise version : 15.3.0
Xamarin version : 4.6.0.295 (306f67a)

On Android, files in the Resources folder should have its build action set to 'AndroidResource' in order for the Resource Designer to notice them.
So when you add existing files, make sure the build action is also set properly.
Same goes for files in the resource folder on iOS.
Only, the build actions is called 'BundleResource'.

Related

C# file (CodeBehind class) disappearing from project in Visual Studio - Xamarin

After adding a new Forms ContentPage with a CodeBehind class (Forms ContentPage XAML), everything works perfectly at first. As expected, it creates a .xaml file with a drop down leading to a .xaml.cs file. After adding code, I build my project, and the screens look and work as expected.
After quitting and returning to Visual Studio, however, the .xaml.cs file just disappears from the Solution. This completely breaks my project because Visual Studio can no longer read my event handlers within the .xaml.cs file. The strange thing is that when I press 'reveal in finder' I can see that the .xaml.cs file IS there.
Note, that this does not happen to all my files. Randomly some CodeBehind classes will just disappear from the Solution.
Has anyone else had this problem? What is going on?
I am using Visual Studio Mac 8.10 Community and this happens to me all the time, too. I make sure to push my projects to my GitHub rep permanently, so I have a backup. Try to use Rider instead of Visual Studio - it has less flaws according to my experience. If you have an Open Source GitHub Repository which your are committing regularly for 3 months, they will grant you a free license.

Error 'does not support code parsing or generation...' if moving C# project with a picture for Visual Studio 2019

I can add an image to a form no problem, and it builds with no errors. Then, when I relocate the project, there is the error 'does not support code parsing or generation because it is not contained within a project that supports code' when I reopen the project from the new file location. This can be resolved by removing the form and adding it again, however this would have to be done every time the project is opened after being moved, and for anyone trying to open it from a new location.
I've tried adding the image in various ways, and I've tried adding different images. Every time, images are added with no issues in the original project location, however as soon as the project is moved this error keeps occurring.
How can I add an image to a C# project in Visual Studio 2019 while retaining the ability to move the project to a different location without resulting in errors whenever the project is reopened?
*EDIT I'm now wondering if this occurs after a certain amount of time that the project is copied, instead of because of adding an image. I have started to get the error again after I have copied the project about 10 times, which is around when I started to get the error when I was working with an image included in the project.
**EDIT I have rephrased the question here: After I have copied a C# project in Visual Studio 2019 about 10 times or so, I get an error when I reopen the project
Remove Special characters from Project folder Name.
Amend Project file paths in solution File.
Delete .vs Hidden folder or .sou file
I recently had a similar experience where i would get the error when trying to open the designer in VS after i moved my project.
to give some context i moved a project from TFS to GIT source control. I created a project in azure devops and the project name contained a space.
the clone url then escaped the space character as %20, when Cloning this repository from the command line without renaming the destination folder I ended up with a folder name containing %20 in its name, instead of a space. my solution file then had the same naming conventions containing the %20 in the project path.
This https://social.msdn.microsoft.com/Forums/en-US/cadde249-3fa9-46de-aa23-37ceae612e8c/form-not-contained-wiithin-a-project-that-supports-code?forum=winformsdesigner helped me:
Close the solution. go to the solution's file folder and delete the solution's .SUO files. Then reload the solution in Visual Studio. In the solution file explorer, right click on the targeted form source and select the "View code"(not "View Designer"). When the source file is opened, then right click over the source text and select View in designer.

Xamarin Visual Studio 2017: Cannot Add Cross-Platform View Page

So I use the Xamarin Cross-Platform application template. I want to add LoginPage.xaml view to my project. So I choose add -> new item -> contentpage.
There is always error:
The project system has encountered an error:
Did not find new element in the hierarchy for item
"Views\LoginPage.xaml". The item would have been added with type
"Page". The project item schema service knows about this item type.
The item does exist in the project file.
After I close the error. There is another error:
Microsoft Visual Studio
Exception of type "System.Exception" was thrown.
Then what happenend next is the "LoginPage.xaml" file is created but not included in the project (it is hidden). I cannot include it in my project because it keeps refusing. There is no xaml.cs file created.
How can I fix this?
This issue is officially "Under Investigation" (reported and answered: https://developercommunity.visualstudio.com/content/problem/160939/bug-adding-xaml-content-page-to-shared-project-xam.html) and there is a workaround:
a workaround is to install the Universal Windows Platform development workload
The best I could do was actually add it in a different version of Visual Studio. I just happened to have both 2015 and 2017 installed. My VS 2015 didn't give me as many options for templates, so I ended up making content pages and later changing the markup to master or whatever I needed.
Just FYI, I've also found that sometimes I can add an XAML page, but it separates the .xaml and .xaml.cs instead of nesting them.

Xamarin.Forms Windows 10 Universal Error "CS0103 InitializeComponent does not exist"

I have a working Android Project with a .Droid-Solution and a SharedProject. I started the App on my phone and it works great. Now, I just added a blank Windows 10 Universal Solution and did the instructions here: http://developer.xamarin.com/guides/xamarin-forms/platform-features/windows/installation/universal/
Anyway... I did nothing else. Just followed the instructions and tryed to start the Windows 10 Universal app, but I get this error:
"CS0103 C# The name 'InitializeComponent ' does not exist in the
current context."
I googled a few times, but didn't find something that helps.
I also tryed the example here: https://github.com/jamesmontemagno/Hanselman.Forms with the same error.
Does someone know this error with Xamarin.Forms and Windows 10 Universal Apps?
Everytime you create a .XAML page in Xamarin.Forms, you are using partial class definitions in three different places.
Your actual .xaml file will have a Class="" attribute on the root element that defines the namespace and class name.
The actual root element type will define the base class that your new .xaml page is subclassing. By default, that would be ContentPage.
You also get a .xaml.cs file that is linked which contains a partial class definition as well. This is your code behind file, where you put page specific code, this is also where the InitializeComponent() call is called from the constructor.
This call serves to parse the xaml contents and fill out the references to all the views you gave ids to using x:name="something" in xaml.
InitializeComponent() is automatically generated by xamarin, it is part of the output of your project and it should get re-generated each time you edit your .xaml file.
In order for this to happen, in the properties of your .xaml file Build Action needs to be set to "Embedded Resource" and the Custom Tool needs to be set to "MSBuild:UpdateDesignTimeXaml"
Now, even with these values set correctly, there are bugs in Xamarin which can lead to the file not getting generated and you getting the error.
The absolute first thing to try when you get this error is to right click the .xaml file and click on "Run Custom Tool".
You only need to do this once, on ONE xaml file, the tool will automatically run for all the .xaml files in your project.
This is somethign that I have to do about once a day during my normal development and it started when i switched to VS 2015. Your mileage may warry.
Another very important thing to check is the BuildTargets in your .csproj file.
The way Xamarin is able to do it's thing is by specifying build actions that call into it's DLL that you get when you install the Xamarin.Forms nuget package. Sometimes the build actions import line get's messed up and this causes all sorts of problems.
Unload your .csproj and find the line that imports Xamarin.Forms.targets.
It should look something like this:
<Import Project="..\..\packages\Xamarin.Forms.2.0.0.6484\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\..\packages\Xamarin.Forms.2.0.0.6484\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
make sure that the version in here matches the version of Xamarin.Forms from your packages.config file, then make sure that you can actually navigate to your project's root folder, and find the actual file the important line is referring to. Ie, packages\xamarin...\Xamarin.Forms.targets
A common thing to do is to delete everything from \packages\ and let nuget re-download everything on your next build. Just make sure the version in your .csproj import and the version in packages.config is the same.
If all of these are done, you should have a valid generated partial class that implements InitializeComponent() and the errors should dissapear.
Just as an update to this:
New, clean Windows 10 Pro installation with all updates in a VM.
New, clean Visual Studio 2015 Community Edition installation with all options selected and all updates.
New project: Blank Xaml App (Xamarin.Forms.Portable) with nowt but generated code.
The project will not build as InitializeComponent in App.xaml.cs does not exist in the current context.
Opinion: this Xamarin add-on is a free add-on on top of a free product, but even so this kind of thing doesn't really inspire you to use it...

Localizing Xamarin Forms

I am trying to localize a Xamarin Forms app. I followed all the steps included here: https://developer.xamarin.com/guides/xamarin-forms/localization/
However, in the Android Emulator, after changing the default language, the strings do not get displayed, they remain the default language. On the device the app doesn't even launch.
Steps taken so far:
1) tried breakpoint (BP) in the main launcher, it skipped the BP and entered the main page
2) tried BP on the Localize class, it doesn't reach it
3) tried BP on the Device.OS check, skips it.
The resource files in Visual Studio are the following:
AppResources.resx
AppResources.fr.resx
AppResources.es.resx
Anyone ever succeeded localizing a Xamarin Forms app before ? I need some assistance with this as Visual Studio does not raise any exception ...
UPDATE 1 :
The resource files in Visual Studio are the following:
AppResources.resx
AppResources.fr_FR.resx
AppResources.es_ES.resx
UPDATE 2:
After recent suggestions and modifications I have the following exception: Markup extension not found.
This is disturbing as the markup extension class is there.
Doesn't work in DEBUG mode (Android only)
If the translated strings are working in your RELEASE Android builds
but not while debugging, right-click on the Android Project and select
Options > Build > Android Build and ensure that the Fast assembly
deployment is NOT ticked. This option causes problems with loading
resources and should not be used if you are testing localized apps.
SOLVED:
The problem was in the declaration of the extension. In a PCL project, the following took place:
a) the TranslateExtension class is located in a subfolder named Pages
b) initial declaration was
xmlns:languages="clr-namespace:MyApp;assembly=MyApp"
c) the following declaration solved the problem:
xmlns:languages="clr-namespace:MyApp.Pages;assembly=MyApp"
along with language code naming conventions of the .resx files as mentioned above
2021 UPDATE
All the current answers are not valid anymore! You need to add resources. Right click your xamarin forms project (Not xamarin android or ios) => Go to project properties ==> Resources ==> Add Resource.
This will create a "Properties" Folder and a resources.resx file which you can delete if you want to.
Then follow the official microsoft documentation by creating a AppResources.resx and other language files (such as AppResources.ar.resx file for arabic). All these files MUST BE in the properties folder.
Then, in your XAML page, declare the namespace as:
xmlns:res="clr-namespace:YourProject.Properties"
Then simply use:
<Label Text="{x:Static res:AppResources.Address}" >
This will use the corresponding Language files depending on what language the device is using. If the language is not supported it will use the defulat file (AppResources.res)
FIUH!!! Wasted one hour to solve this!!!

Categories