I have 2 pages. One page contains another, where the Grid looks like the following:
<Grid>
<views:MapViewPage/>
</Grid>
I've included the views namespace correctly and everything runs fine but the designer seems to be having a bit of an issue with this.
It underlines the views:MapViewPage line and says object reference not set to an instance of an object, and complains about the constructor. I don't understand why, I've done this in WPF where I've included views in another page or window so I don't understand why the XAML designer complains in UWP. I've restarted visual studio and deleted the bin and obj directories but i still see the error - builds fine and runs fine tho... and includes the view as expected...
You are on the right path but sometimes designer picks up wrong metadata. Please delete your obj and bin directories and rebuild your project. It should work
It underlines the views:MapViewPage line and says object reference not set to an instance of an object, and complains about the constructor.
Yep, this is common behavior, if you just create folder and make a xaml file in it then use xaml file without build the project, the Visual Studio will not find the specific the namespace (xmlns:views="using:projectname.Views"). Please try to
restore the nuget package (right click the solution Restore Nuget Package) and build or re-build your project before running it.
Related
I am trying to build a cross platform Xamarin.Forms application.
I have tested that the app works and it does indeed build successfully however, the IntelliSense isn't working and every content page (xml and cs) that I open floods the error log with multiple errors (but the app still builds fine? The errors are all saying that the XML stuff isn't recognised, such as the labels and Contents, even the includes at the top...)
I have read around and tried every fix I can find:
I have updated all nu get packages to their most recent build, I have cleared the Nuget cache, I have changed the "Open With" on XML files to Source Code (Text) with encoding, but still have multiple errors and no IntelliSense
Any help would be great, I am using VS 2017 on windows, using live player to run the app directly to iPhone
Edit: Providing a screenshot to show what the errors look like and where they occur...
here
At first, be sure that the "open with" option for xaml file is set to "Source Code (text) Editor"
upgrading to XF 3.0.0.482510 seems to fix the intellisense problem
If you have resharper, set xaml intellisense to Visual Studio not to Resharper
In my case, the solution to the IntelliSense on Xamarin XAML files was tied to NuGet package references, I think especially the Xamarin.Forms package. If you have the yellow warning symbol on your packages, you have to get through the following steps on your specific project:
Make sure it builds without errors
Pass an MSBuild /restore [path to the specific project] in CL
Pass a .\nuget restore at your solution in CL
Close and reopen your solution
Right-click on the concerned XAML file > Open With... > Make sure to have"XAML Designer" or "Xamarin.Forms Previewer" set to default for XAML files
You should now have IntelliSense.
In my case, that made the yellow warnings disappear, then XAML IntelliSense worked again on Xamarin.Forms XAML files
I have a Strange issue that I can't seem to fix, my Intellisense for XAML is no longer showing up and the code behind is only showing the premade Members. The Classes and Methods I have made are not showing up. Also Visual Studios is not recognizing other pages and wont recognize Navigation either.
I have tried going to Tools>Text Editor>C#>Intellisense -Statement completion and checking the boxes Auto list members and Parameter information. I also tried to clear out the cache.
From what I have researched it seems nobody else is having the same problems.
Close all open tabs in the project and quit VS, reopen the solution in VS and right click the XAML file in the Solution Explorer and then select Open With….> Source Code (Text) Editor.
Delete obj folder and clean project
Right click the XAML page>Properties>Build Action>change it to something else and back
Add a new content page under this specific project and check it works or not.
I would like to add that this worked for my .cs files only.
To get the XAML files Intellisense to work try to repeat the above steps on your .cs files or wait for the XAML files to gain Intellisense.
For ReSharper users: I found that having ReSharper's IntelliSense enabled for all languages can break Intellisense unexpectedly (especially for XAML files). Here's how I was able to fix the same problem that the asker had in Visual Studio 2017 with ReSharper installed:
In Visual Studio, go to the ReSharper menu and click on Options.
On the left side go to Environment > IntelliSense > General.
Select the Custom IntelliSense radio button.
Change any languages that have broken IntelliSense to Visual Studio (like XAML).
Click the Save button.
Just Exclude and Include Xaml Pages and It works again.
I solve that by changing the default editor in visual studio :
In Visual : File > Open
In the open file box : Select a .xaml file (don't open it)
Select "Open with" in the button arrow
Select "Source Code (Text) Editor and Set as Default
Click OK
I tried most of the above without much luck, but noticed if I created a new page, then intellisense worked as expected. For the properties section of the xaml file I noticed that the new page had a Custom Tool assigned (MSBuild:UpdateDesignTimeXaml) do I tried to cut-and-paste this into the existing forms without luck.
Looking at the .cs page for the new page that worked I notice that there is some extra info above the partial class [XamlCompilation(XamlCompilationOptions.Compile)] so I added this along with a using Xamarin.Forms.Xaml statement.
Finally I went back to the xaml properties page and selected Reset to Default for the Custom tool. Voila, for me everything started behaving itself.
Please add the following Nuget Package from Nuget Console.
Install-Package MobileEssentials.FormsIntellisense -Version 0.1.1-pre
You can download the latest update from the following link.
https://www.nuget.org/packages/MobileEssentials.FormsIntellisense/0.1.1-pre
After installing the package please restart the project and wait for sometime and check it.
Just delete .vs directory. this directory is hidden. so
Just Exclude and Include Xaml Pages worked for me too. The difference in .csproj file was:
MSBuild:Compile
now:
XamlIntelliSenseFileGenerator
I could fix the issue in VS2019 by launching the VS installer to modify it, un-check and check the workload ‘.NET desktop development’ to re-install it.
I know that there a lot of questions about this problem. but any of the answers solved me it:
I use VisualStudio 2013. I have a sulation with a Wpf project.
I create for example a public class, and I want to define a data template for this type in Xaml code so I add a reference to the viewModel directory-
xmlns:vm="clr-namespace:MyProject.ViewModels"
then I add a dataTemplate
.
I get an error:
The name "MainViewModel" does not exist in the namespace "clr-namespace:MyProject.ViewModels"
I try all the sulations: rebuild it, restart VisualStudio, delete the suo file, debug it in AnyCpu and then switch it back to X64.
I try to delete this row, and when I begin to write it, this row is completed auto, so I havn't spelling mistakes.
But although all, I get this error (for all the xaml references to files in my project), it run perfectly, but the errors list isn't empty and I can't give it up.
What else can I do? it looks like a bug in the Visual studio or something like that.
(I can't move the project to a local disk because we use a network disk)
I'm guessing here, but I knew someone who had a similar problem and believe me, it is probably something simple, you're just not seeing it ;).
Scenario (yes, I could be wrong)
You created youre 'class' but since then, you had build errors, so you're code has never compiled with the newly created class. Your Intellisense tells you the class does not exist, while in youre eyes it does.. You're designer needs the compiled dll in order to include the classes in the namespace. So you have to find the bug in you're code and fix it => Compile it => Reload Designer and you're errors should be gone.
As an idea, can you post you're Build Output if this is not the solution?NoteThe reason why you would be able to 'run' youre code is because you could have a previous compiled .exe in you're output directory (you changed you're build from 32 to 64 bits, so you're output folder also changed)
Adding Additional Activity .cs and Layout axml Using Visual Studio 2015.
I'm very new to Xamarin and Android development, but have been a developer for a few years using VB and now C#. I have a simple app on Android 4.2 that is getting more complicated as I go along. The simple matter us that I want to add an additional GpsAction.cs and corresponding Gps.axml layout to the project. It seems impossible to find the right combination syntax to achive this. I have a mainActivity with main.axml. In VS 2015 it's very simple to add new but I keep getting "resource.id does not contain a definition for" I would really appreciate your help with this
namespace AddCam
{
[Activity(Label = "GpsActivity")]
public class GpsActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.GpsLayout);
string c = FindViewById<TextView>(**Resource.Id.textView1**).Text;
// Create your application here
}
}
For people who are still facing this issue, the default Build Action of the layout file would be set to TransformFile. Select the layout, go to the Layout Properties, and in the properties pane, Select AndroidResource as your Build Action. Clean build your project and it should work.
I changed the text field "id" from "#+id/imageView1" to "1", saved, rebuilt and changed it back to "#+id/imageView1", it fixed it. I would like to add, this whole problem came from
Adding a new activity and layout.
Using preexisting code from another app that I had.
Copying and pasting code from the original app to the new Activity
and Layout.
All fairly common stuff, the real problem seemed always to be adding any new Activities and Layouts to a main Activity. It can get very convoluted and with no (known to me) logical way to run down a problem with Xamarin. Don't get me wrong compared to 10 years ago (the last mobile app I tried to write) Xamarin is heaven. Good coding folks, now if I can only figure out why Keyword "this" is error-ring on the added Activity.cs
Just add namespace like that Android.Resource.Id - it's resolve for me
What did work for me (Visual Studio 2017, opening an old Xamarin project):
Delete obj and bin folders, build.
If errors, restart Visual Studio (I know the pain).
Build again
Now the Resource will be visible (of course, if you defined it correctly).
The best solution I have found is to build solution.
Choose Build solution from Build menu (or Ctrl+Shift+B).
This action will resolve the issue.
You Should add set value forandroid:id="#+id/button1" in axml of app, then rebuild the project and try again.
like thisButton button = (Button)FindViewById(Resource.Id.button1); .
Check if you are missing these namespaces in your layout file -
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
(VS 2019) I did the previous solutions and nothing, I had the same problem and the solution for me was to remove '&' from the text of TextView, I was trying to Set the text to "text&text" even &&(I though it was like mnemonics as in WindowsForms) didn't work so I had to remove it and it fixed.
Cleared Main.axml page and Reset and it is working.
My problem was I was trying to debug Xamarin Android project (native) using Xamarin Live Player, connecting using USB cable and selecting my device for debugging solved the problem
Make sure your axml is well formed and rebuild the solution it should work, if you continue facing the same issue then remove axml and add it again then build the solution.
In Visual Studio 2019 when you add a new Android Layout to the Project it is added as .xml file. I already had some created earlier layouts in Resources/layout folder with extension .axml (not .xml) and for me changing the extension .xml -> .axml worked.
I understand that this issue is very old, but I've run into it as well in Visual Studio 2019, and have found a solution.
The issue occurs, for me, when adding an element to the layout and then attempting to add code. The issue appears to be related to the way the project is built.
Add the element to your layout, then build your project before adding any additional code. Apparently Resource.Id does not update with additional members until it's built, and attempting to refer to the new member in the code before Resource.Id recognizes it prevents the project from being built.
Like Maniacz said, in VS 2019 I just had to change the XML to axml extensión to the layout in layout folder under resources
For anyone else looking for an answer despite running clean/build/rebuild which didn't work for me:
I had freshly installed a number of tools for xamarin development in VS. Though a build/rebuild may have actually worked, in my case I believe what also fixed it was closing and re-opening VS. I'm pretty sure I had a few issues, primarily stemming from newly installed tools (android SDKs in my case) requiring VS to be restarted. If you're working on a project already having installed the tools you need, try as others have said - build/rebuild.
I had to modify Build Action for my layout file and re set the original Build Action as it was previously set. This made my visual studio to regenerate the resource ids in Resource.designer.cs file.
Step #1:
Go to properties of the layout file that is missing its ids and click the Build Action DropDown.
Step #2:
Select something from the dropdown other than AndroidResource.
Step #3:
Reselect AndroidResource from that Build Action.
Now, you will have your Resource.designer.cs file regenerated and it will have the reference ids to your controls in the layout file.
Check the Resource.Designer class file. There will be a class like public partial class Id. There the integer IDs of the controls are written. Use them instead of Resource.Id
I am new to WPF coding. I have a project that has custom controls coded as xaml's with C# code-behinds. I try to import these files into another project and when I try to use these xaml controls in my main view file, Visual Studio cannot find the namespace that the imported xaml's and C# code-behinds are attached to.
I tried changing the namespace to be the same as my current project. I tried restarting Visual Studio 2013. Neither of these worked.
I tried adding the project with the custom controls to my solution. The imported project can read the custom xaml's, but my main project still can't reference them. I tried dragging the files over to the main project, and they still won't work.
I tried rebuilding the controls xaml's and C# code-behinds in my new project it still says:
The type 'local:ClickSelectTextBox' was not found. Please verify that that you are not missing an assembly reference and that all reference assemblies have been built.
This item is under the appropriate namespace and in the current project.
In visual studio go to TOOLS -> choose Toolbox Items. this will allow you either choose from an array of different components or browse around to find a dll that you would like to use.
EDIT:
Oh so you are trying to take customized xaml files that you already have written and modify them in a separate project? I would suggest one of two things.
1: right click your project name -> add existing, and add the xaml and xaml.cs file to your project at the same time.
if for some reason that does not work you can also try
2: creating a new xaml control (with the same name of the control you want to drop in) in your project and copy pasting the xaml code into that control, this should autogenerate the codebehind designer shell you are going to need. you can then go into the xaml.cs file and drop whatever business logic you are looking for. It is not the cleanest solution but sometimes the WPF editor gets a little funky when importing xaml files
I know it's an old post but still somebody may find it useful:) So! ... Make sure
1)You don't have public class outside of root namespace, check this
link
2)You are not mixing targets of your assemblies (unless you can't 100% avoid that)
For example, if you are referencing something like SQLite.Core NuGet (that has both x86 and x64 versions of SQLite.Interop.dll built in) in a project that is AnyCPU, sometimes it's easier to set application's target to x86 or x64 to solve the x86/x64 paths issues - but then you may get all these "type not found/control not built/assembly not found" and all that sorts of nonsense from Designer even despite your app builds and runs ok.
Hope that helps
Try downloading ReSharper's trial version, install it and then open up your code again. One of the awesome features Resharper has with XAML code is that it will automatically map objects to their appropriate namespace. I think this will make it easier for you and will show you a ton of ways to do things better. When I was first learning WPF it was honestly a godsend to use Resharper.