XAML code behind x:name property is unaccessable - c#

I'm new to xamarin forms, I created an Android Forms apps. But when i give x:Name="" to any element in xaml. In code behind i can't access this property id.
- I updated nuget packages even i keep trying to rebuild projects. But still unable to access xaml ids in code behind. I also uninstalled and installed visual studio multiple times. But still the same problem.

You have to build the project once after adding control in the Xaml.Then you will be able to access the control.

Try to execute this code and find what is wrong in your code:
TestPage.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestImageCropping.TestPage">
<ContentPage.Content>
<Label x:Name="label1" Text="Hello"/>
<Button x:Name="button1" Text="Xamarin"/>
</ContentPage.Content>
</ContentPage>
TestPage.xaml.cs:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace TestImageCropping
{
public partial class TestPage : ContentPage
{
public TestPage()
{
InitializeComponent();
label1.TextColor = Color.Blue;
button1.Text = "Click here";
}
}
}

There are some issues related to x:Name property and accessing it in background...
Try to write it manually, it will work.

Related

element defined in xaml "does not exist" in c# code behind

I´ve started to learn xamarin forms and now I'm already starting to apply animation to my Image. But I've got an error in my code.
Here's my code in xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace teste2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
// The animation
car.FadeTo(0, 2000);
}
}
}
And here's my error:
Error CS0103 Name 'car' does not exist in current context teste2
And here's my xaml code:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="teste2.TabsPage" BarBackgroundColor="#508ca7"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom">
<TabbedPage.Children >
<ContentPage Title="Voiture" IconImageSource="lab_icon_voiture.png" BackgroundColor="#DCCECD">
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="Center">
<!-- The image that i want to animate-->
<Image Source="lab_voiture.png" x:Name="car"/>
</StackLayout>
</ContentPage>
<ContentPage Title="Avion" IconImageSource="lab_icon_avion.png" BackgroundColor="#f4f4f4">
<StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="CenterAndExpand">
<Image Source="lab_avion.png" x:Name="plane"/>
</StackLayout>
</ContentPage>
</TabbedPage.Children>
</TabbedPage>
This is a common issue with Xamarin on VS, especially on MAC, what happens is that your debug file does not generate the XAML changes onto the xaml.g.cs file. Which is basically the file that is autogenerated where our compiler combines the xaml and xaml.cs file.
All you need to do for this to update correctly is to comment the following line of code:
car.FadeTo(0, 2000);
Once you do this go for a clean build or a rebuild. once your app builds successfully go back to this line of code again and uncomment it and this should start working
Goodluck !

Picture as background in Xamarin Forms is not displayed

I want to use a picture as background in my Xamarin Forms Android app. That image is stored in drawable folder. The image is not displayed when I debug the app on my smartphone. Below is the code. Thank you
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinTest"
x:Class="XamarinTest.MainPage"
BackgroundImage = "BGImage.jpeg">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace XamarinTest
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
}
I guess you just store it in the folder but not add it in the project.
you should right click the resource->drawable folder -->add-->add file to add the image file .
Check where do you store your images:
For Android, iOS - Resource Folder;
For Windows Phone, Win/UWP - Assets folder;
Also check "Build action" (it should be AndroidResource for Android, BundleResource for iOS) and additionally (if still not showing) "Copy To Ouput Directory" should be "Copy if newer".

C# to Xaml Conversion

I want to implement a custom accordion ui element but the code has been written in C# ( https://github.com/Kimserey/AccordionView ) but I want to use xaml. I tried to add Accordion view in my content but while I am compiling I get following error
HomePage.xaml : error : The given key was not present in the dictionary.
Here is the my xaml code
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:customaccordionmenu="clr-namespace:CustomAccordionMenu"
x:Class="CustomAccordionMenu.HomePage" Title="Accordion Example" >
<ContentPage.Content>
<StackLayout VerticalOptions = "Center">
<customaccordionmenu:AccordionView/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
So I just want to create an accordionview and add the content in xaml, not in c#
PS: The project is for Xamarin.Forms
You are trying to instantiate an empty AccordionView. Judging by the code in github, the accordionview doesn't have a parameterless constructor, which could cause a problem.
Also the AccordionSectionView class calls
private ImageSource _arrowRight = ImageSource.FromFile("ic_keyboard_arrow_right_white_24dp.png");
as well as
private ImageSource _arrowDown = ImageSource.FromFile("ic_keyboard_arrow_down_white_24dp.png");
so if these files aren't in your app's resources and named in the very same way, this is also a possible cause for a crash.

Coding a Navigation Drawer Menu (Slider Menu). (Master Page Detail)

Trying to build a Slider menu I´m using this docu coming from official Xamarin.
MasterPageDetail
Quite odd, it´s not working. VS2015 intellense recognized me MasterDetailPage (my class is inheriting from it), but when I´m trying to launch the app, next error appears:
The type or namespace name 'MasterPageDetail' does not exist in the namespace 'Xamarin.Forms' (are you missing an assembly reference?)
What it happens? Do you know of any simply working demo?
I´m using this question as reference, but I´m not getting it works...
Slider
Some answer uses MasterPageDetail, another one implemented the solution in app (class) and I need to do it in a ContentPage
Thanks mates.
EDITED: I´m using Xamarin.Forms and I imported it and I don´t have any class called like this (MasterPageDetail)... Quite odd, it sounds like a stupid thing but I can´t see it.
MasterDetailPage is class from Xamarin.Forms if you have class with the same name then it may be a conflict.
Here are some useful links:
https://developer.xamarin.com/guides/xamarin-forms/user-interface/navigation/master-detail-page/
https://developer.xamarin.com/api/type/Xamarin.Forms.MasterDetailPage/
You can also download my Example from my Github:
https://github.com/15mgm15/XamarinForms-MasterDetailPage-Recipe
There's a simple way to create a MasterDetailPage using XAML, first of all you need to create a page which will contains the MasterDetailPage and a SecondPage which will contains a DetailPage, by doing this, you need to change your hierarchy of this page to MasterDetailPage, and finally load a SecondPage inside of MasterDetailPage.
In your project Create a New Page - MenuPage and change the Page Type ContentPage to MasterDetailPage
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
BackgroundColor="White"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourProject.MenuPage">
</MasterDetailPage>
Then go to your MenuPage.cs and change the hierarchy to MasterDetailPage too.
public partial class MenuPage : MasterDetailPage
{
public ChatPage()
{
InitializeComponent();
}
}
Go back to your XAML page - MenuPage and add this :
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourProjectName.MenuPage">
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Orientation="Vertical">
<!--Here goes your Menu Items-->
<Button Text="MyFirstButton"/>
<Button Text="MySecondButton"/>
<Button Text="MyThirdButton"/>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<local:SecondPage/>
</MasterDetailPage.Detail>
</MasterDetailPage>
And finally you need to add a reference to your DetailPage
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourProjectName.MenuPage"
<!-- Add this line to refer your DetailPage, `SecondPage`-->
<!-- put your namespace and repeat in assembly -->
xmlns:local="clr-namespace:YourProjectName;assembly=YourProjectName">
</MasterDetailPage>
I hope its helps you!
I got it.
Finally I clean all project and started a code from zero using the next example (adding just MasterPageItem.cs, piece of cake):
Working MasterDetailDemo
I improved the demo by my own, creating a Master Page Detail where Master´s items bind specific generic page, it will be fill with its own attributes whose depends of id passed to the page´s constructor.
Github Slider Menu improved demo
Hoping it helps...

UserControl for cross-platform Xamarin Forms

I have been looking for a long time now but still haven't found a way to create WPF like UserControls for cross-platform Xamarin Forms. Can this even be done? I am using Xamarin with Visual Studio 2013.
Here is a XAML example. Just to add you can have any base visual element. E.g. a Grid, StackLayout, Image etc.
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Mobile.Control.UserControl">
</Grid>
Here is the code behind
namespace Mobile.Control
{
public partial class UserControl : Grid
{
public UserControl()
{
InitializeComponent();
}
}
}
To use it, go to your page and implement the namespace
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:control="clr-namespace:Mobile.Control;assembly=Mobile"
x:Class="Mobile.View.MyPage">
And then in the page
<control:UserControl />

Categories