0
votes

I get two errors if I click on the navigation menu during runtime? I am a beginner in C# and have no idea how to solve this problem.

var group = (SampleDataGroup)e.ClickedItem;
var groupId = group.UniqueId;
var item = (SampleDataItem)e.ClickedItem;
var itemId = item.UniqueId;

InvalidCastException:

In System.InvalidCastException ist eine Ausnahme vom Typ "AstroApp.exe" aufgetreten, doch wurde diese im Benutzercode nicht verarbeitet.

Zusätzliche Informationen: Das Objekt des Typs "AstroApp.Data.SampleDataItem" kann nicht in Typ "AstroApp.Data.SampleDataGroup" umgewandelt werden.

public class SampleDataItem : SampleDataGroup
{
    // add flag as last param
    public SampleDataItem(String uniqueId, String title, String subtitle,
    String imagePath, String description, String content, SampleDataGroup group, int intIsCustomNav): base(uniqueId, title, subtitle, imagePath, description)
    {
        this._content = content;
        this._group = group;
        this.intIsCustomNav = intIsCustomNav;
    }

    private string _content = string.Empty;
    public string Content
    {
        get { return this._content; }
        set { this.SetProperty(ref this._content,value);}
    }

    private void SetProperty(ref string p, string value)
    {
        throw new NotImplementedException();
    }

    private SampleDataGroup _group;
    public SampleDataGroup Group
    {
        get { return this._group; }
        set { this.SetProperty(ref this._group,value);}
    }

    private void SetProperty(ref SampleDataGroup sampleDataGroup, SampleDataGroup value)
    {
        throw new NotImplementedException();
    }

    public int intIsCustomNav { get; set; }           
}

/// <summary>
/// Wird aufgerufen, wenn auf ein Element innerhalb einer Gruppe geklickt wird.
/// </summary>
/// <param name="sender">GridView (oder ListView, wenn die Anwendung angedockt ist),
/// die das angeklickte Element anzeigt.</param>
/// <param name="e">Ereignisdaten, die das angeklickte Element beschreiben.</param>
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
    // Navigate to the appropriate destination page, configuring the new page
    // by passing required information as a navigation parameter

    var group = (SampleDataGroup)e.ClickedItem;
    var groupId = group.UniqueId;

    var item = (SampleDataItem)e.ClickedItem;
    var itemId = item.UniqueId;


    int intGroup = Convert.ToInt32(group);

    // Abfrage Gruppe 1
    if (intGroup == 0)

        // Abfrage der Seiten
        if (item.intIsCustomNav == 0)
        {
            // Gruppe 1, Seite 1
            this.Frame.Navigate(typeof(ItemDetailPage), itemId);
        }

        if (item.intIsCustomNav == 1)
        {
            // Gruppe 1, Seite 2
            this.Frame.Navigate(typeof(ItemDetailPageA2), itemId);
        }

        // Abfrage Gruppe 2
        if (intGroup == 1)
        {
            // Abfrage der Seiten
            if (item.intIsCustomNav == 0)
            {
                // Gruppe 2, Seite 1
                this.Frame.Navigate(typeof(ItemDetailPageB1), itemId);
            }
        }

    }
1
You might do well to post an english translation of the exception.Johnie Karr
In System.InvalidCastException an exception of type "AstroApp.exe" has occurred, but this was not handled in user code. Additional information: The object of type "AstroApp.Data.SampleDataItem can not be converted to type 'AstroApp.Data.SampleDataGroup ".user3447311
you don't really need the translation, as it is a class cast exception. SampleDataItem can't be cast to SampleDataGroup. The runtime is telling you exactly what and where the problem is. An apple is not a banana.John Gardner

1 Answers

1
votes

This is an invalid cast exception - in other words you are trying to use something that isn't of the type you expect. I noticed you are casting "ClickedItem" as both "SampleDataGroup" and as a "SampleDataItem". Unless there is some form of class inheritance going on here, this isn't possible. I would recommend you try the following code:

var group = e.ClickedItem as SampleDataGroup;
var groupId = group == null ? 0 : group.UniqueId;

var item = e.ClickedItem as SampleDataItem;
var itemId = item == null ? 0 : item.UniqueId;

I don't know if this is what you want but it will at least eliminate the error you are seeing. Hope that helps some!

EDIT & Another Issue:

I noticed that the next line after this error is:

// This won't work:
int intGroup = Convert.ToInt32(group);

This also doesn't look like it is going to work. I think you are going to want to learn more about strong types in .NET and how they work. Maybe the following MSDN page will get you started: http://msdn.microsoft.com/en-us/library/ms173104.aspx