Xamarin Generating incorrect c# from xaml

CharlesHoranCharlesHoran ✭✭USMember ✭✭
edited October 2014 in Visual Studio

Currently the generation of the *.xaml.g.cs files is creating incorrect c#. To be specific extraneous members are being generated. I don't know if it is actually related or coincidence but at the same time I started receiving the "to old to be used with this version..." error again.
I expect a learning curve, however the learning curve becomes steeper when the tools being used blow up apparently at random.

Here is a sample generated file:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18444
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Teachers.Pages {
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;


    public partial class MessageCenter : ContentPage {

        private ToolbarItem Show Tray;//Member created for no good reason...no x:Name in the xaml...just a normal name

        private void InitializeComponent() {
            this.LoadFromXaml(typeof(MessageCenter));
            Show Tray = this.FindByName<ToolbarItem>("Show Tray");
        }
    }
}

And the corresponding 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"
             xmlns:pages="clr-namespace:Teachers.Pages;assembly=Teachers"
                       x:Class="Teachers.Pages.MessageCenter">

  <ContentPage.Resources>
    <ResourceDictionary>
      <DataTemplate x:Key="folder">
        <StackLayout Orientation="Horizontal">
          <ImageCell Text="{Binding Name}" Image="{Binding Icon}"/>
        </StackLayout>
      </DataTemplate>
    </ResourceDictionary>
  </ContentPage.Resources>

  <ContentPage.ToolbarItems>
   <!--NO x:Name here!!!-->
    <ToolbarItem Name="Show Tray" Command="{Binding ShowMasterCommand}" Order="Primary" Icon="slideout.png"/>
  </ContentPage.ToolbarItems>

  <StackLayout VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand" Orientation="Horizontal" >

    <ScrollView WidthRequest="180" VerticalOptions="FillAndExpand" IsVisible="{Binding ShowMaster}" >
      <StackLayout>
        <ContentView Padding="10,40,0,5" BackgroundColor="Transparent">
          <Label Text="Folders" Font="Medium"/>
        </ContentView>
        <ListView ItemsSource="{Binding Folders}" ItemTemplate="{StaticResource folder}"/>
      </StackLayout>
    </ScrollView>

    <ScrollView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
      <StackLayout HorizontalOptions="FillAndExpand" BackgroundColor="Red">
        <Label Text="Item1"/>
        <Label Text="Item2"/>
        <Label Text="Item3"/>
      </StackLayout>

    </ScrollView>
  </StackLayout>
 </ContentPage>

Posts

  • MihaMarkicMihaMarkic ✭✭✭✭ SI ✭✭✭✭

    You shouldn't put spaces in name in first place (because autogeneration and possibly other issues).

    Second, x:Name and Name differs rarely and you can consider them the same hence the generation.

    Lastly, put these kind of questions to Forms forum - you'll get more answers there.

  • CharlesHoranCharlesHoran ✭✭ USMember ✭✭

    @MihaMarkic‌
    Perhaps you should look at that Xaml.

    Name is a property of the toolbaritem. That name can be shown to the user. Spaces in the value of the property are not only fine, but expected. If I switch that toolbaritem to secondary the Name becomes part of the pull down menu...do you really think that "ShowTray" is what would be expected by a user?!

    Name and x:Name are completely different. (There is a reason for x:)
    Where the heck are you getting your misinformation from?

    Name is http://xamarin.com/schemas/2014/forms/Name
    x:Name is http://schemas.microsoft.com/winfx/2009/xaml/Name

    Hmmm those look different to me. I can guarentee that any xml parser with namespace support will see them entirely different as well.

    This is/was a bug in the code generation plain and simple. (I haven't seen it since the last stable update)

    Please don't give advice that is 100% wrong like 'x:Name and Name differs rarely...' Some poor sap that doesn't know better might take you seriously.

    As a last thought, since this was a Visual Studio Tooling failure why would I post it in the form's forum?

  • MihaMarkicMihaMarkic ✭✭✭✭ SI ✭✭✭✭

    Read here about x:Name vs Name in WPF world, I assume the same behavior for XF.

    As per XF ToolbarItem I'd say that the property is poorly named. "Name" should refer to instance name, not to displayed text. It should be Caption or Text or whatever instead.

    And while it is a tooling issue (which actually most probably isn't) it is more related to Forms than to Visual Studio. Even more so, since the very same issue appears in Xamarin Studio.

  • CharlesHoranCharlesHoran ✭✭ USMember ✭✭

    @MihaMarkic‌
    I'm aware of the WPF bug The fact that microsoft chose to introduce that bug doesn't change it from being a bug. We could debate if it is a design fault or an implementation bug, but it is still a bug.

    In WPF it was self inflicted in a vain attempt to placate tool developers by not requiring them to be fully compliant. It had the exact same effect as the early relaxed rules of HTML parsing, namely mass confusion and behavioral differences based on the runtime environment from the same input.

    In this particular context "Name" as applied to a toolbar item clearly has a different semantic intent than "x:Name" applied to the same toolbaritem.
    Name is the label the user will see This may or may not be an unfortunate choice of property name but it is by no means invalid.
    x:Name is the name of the instance variable that will be generated in the codebehind file (*_g.cs).

    Confusing those two clearly different semantic intents is a bug.

    I have never seen this in Xamarin Studio, I'm not saying that it doesn't occur there, simply that I have not seen it and as such placed this post in the most appropriate forum based on my knowledge. If you feel it should be opened in the Forms Forum feel free to do so.

    P.S. The name property is still in my Xaml and causes no issues. As I stated after the last stable update I no longer see this occurring. Sorta lends credence to the whole bug thing.

Sign In or Register to comment.