Help! Complete newbie to C# and Xamarin Form

anub13anub13 IDMember

Hi, I am a complete newbie to C# and Xamarin Form

And actually I've tried to do samples in xamarin site and all worked fine, and here I am trying to create a very simple coding, it just showing Hello World with editing background(actually this is from example in xamarin site also) but I am bit struggle at it.. I am using Visual Studio 2015, Community Edition and here's the code.

EDIT:

After compiling it just load the application without showing the text.

Best Answer

  • C0D3NameC0D3Name US ✭✭✭
    Accepted Answer

    Hello Fahri,

    You are trying to declare an editor in your App.cs class, this is the wrong place, you need to do this in the MainPage.xaml.cs file. Also, your code is never adding the editor you create to the page. I notice that you have an editor in your MainPage.xaml file already defined. To set the text for that editor in the code behind, you'll need to provide it with an x:Name.

    Also, your <OnPlatform x:TypeArguments="x:Color"...
    should be <OnPlatform x:TypeArguments="Color" ...

    Make these changes to your MainPage.xaml
    - Name the Editor control so you can reference it in the code-behind
    - Change x:Color to Color

        <?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:FormsSandbox"
                     x:Class="HelloWorldXamarinForm.MainPage"
                     Title="Hello World">
          <ContentPage.Content>
            <StackLayout Padding="5,10">
              <Editor x:Name="MyEditor">
                <Editor.BackgroundColor>
                  <OnPlatform x:TypeArguments="Color"
                              iOS="#a4eaff"
                              Android="#2c3e50"
                              WinPhone="#2c3e50" />
                </Editor.BackgroundColor>
              </Editor>
            </StackLayout>
          </ContentPage.Content>
    
        </ContentPage>
    

    And in your MainPage.xaml.cs code behind
    - Reference the editor control by name and set it's text property.

        using Xamarin.Forms;
    
        namespace HelloWorldXamarinForm
        {
            public partial class MainPage : ContentPage
            {
                public MainPage()
                {
                    InitializeComponent();
                    MyEditor.Text = "Hello World";
                }
            }
        }
    

    Hope this helps.

Answers

  • C0D3NameC0D3Name USUniversity ✭✭✭
    Accepted Answer

    Hello Fahri,

    You are trying to declare an editor in your App.cs class, this is the wrong place, you need to do this in the MainPage.xaml.cs file. Also, your code is never adding the editor you create to the page. I notice that you have an editor in your MainPage.xaml file already defined. To set the text for that editor in the code behind, you'll need to provide it with an x:Name.

    Also, your <OnPlatform x:TypeArguments="x:Color"...
    should be <OnPlatform x:TypeArguments="Color" ...

    Make these changes to your MainPage.xaml
    - Name the Editor control so you can reference it in the code-behind
    - Change x:Color to Color

        <?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:FormsSandbox"
                     x:Class="HelloWorldXamarinForm.MainPage"
                     Title="Hello World">
          <ContentPage.Content>
            <StackLayout Padding="5,10">
              <Editor x:Name="MyEditor">
                <Editor.BackgroundColor>
                  <OnPlatform x:TypeArguments="Color"
                              iOS="#a4eaff"
                              Android="#2c3e50"
                              WinPhone="#2c3e50" />
                </Editor.BackgroundColor>
              </Editor>
            </StackLayout>
          </ContentPage.Content>
    
        </ContentPage>
    

    And in your MainPage.xaml.cs code behind
    - Reference the editor control by name and set it's text property.

        using Xamarin.Forms;
    
        namespace HelloWorldXamarinForm
        {
            public partial class MainPage : ContentPage
            {
                public MainPage()
                {
                    InitializeComponent();
                    MyEditor.Text = "Hello World";
                }
            }
        }
    

    Hope this helps.

  • anub13anub13 IDMember
    edited August 2016

    It's worked like a magic! Thanks.

    EDIT:

    And ya of course it's help, it fix my misunderstanding. :smiley:

Sign In or Register to comment.