Using Tag Property (problem)
Last post 05-06-2008 7:23 PM by Bill Reiss. 8 replies.
Sort Posts:
09-13-2007 11:43 AM
Using Tag Property (problem)

I'm trying to get the Tag property to work on a MediaElement

MyMediaElement.Tag.Value = 3   ---- to set a value

 MyMediaElement.Tag  --- to retrieve the value

To my reading of the SDK this should work... right?  But instead (in VisStudio) I see  MyMediaElement.Tag  returning   ""

SetValue("Tag", 3)  --- doesn't work (and shouldn't according to the SDK)

What am I missing

tom

 

TBink

Joined on 05-01-2007
Posts 92
09-13-2007 11:57 AM
Re: Using Tag Property (problem)

I routinely add name/value pairs to the tags to canvas's using the following functions:


function getPropertyItem(rootCanvas, itemName)
{
    var tagValues = rootCanvas.tag;
   
    var index = 0;       
    while (index <= (tagValues.split("~").length -1))
    {
        if (tagValues.split("~")[index].split("|")[0] == itemName)
        {
            return tagValues.split("~")[index].split("|")[1];
        }
        index ++;
    }
   
    return "";
}

function addPropertyItem (rootCanvas, itemName, itemValue)
{
   
    if (rootCanvas.tag == "")
    {
        rootCanvas.tag = itemName + "|" + itemValue;
    }
    else
    {
        rootCanvas.tag += "~" + itemName + "|" + itemValue;
    }
   
}

 

Hope they help.

=> Dave

Senior Engineer/Dev Manager, Vertigo Software
Vertigo

SD West 2008 March 3-7. Silverlight Classic Arcade Games

Dave Britton

Joined on 06-27-2007
San Francisco Bay Area
Posts 219
09-13-2007 12:27 PM
Re: Using Tag Property (problem)

oohh... I hadn't considered putting pairs in a tag... very cool!

Thanks for the tip and definitely for the code :)

-Dave

Stay in the 'Light
MVP: Visual Developer - Client Application Development
http://www.wynapse.com
Twitter SLNews | Join me @ SilverlightCream | SL Web Articles | My Articles | My Tutorials | My Tooltips | SilverlightCream

WynApse

Joined on 05-13-2004
Posts 339
09-13-2007 12:53 PM
Re: Using Tag Property (problem)

So just to clarify, the string in the tag looks something like the following:

"name1|value1~name2|value2~name3|value3" etc ... About as cryptic as I could make it Wink

=> Dave

Senior Engineer/Dev Manager, Vertigo Software
Vertigo

SD West 2008 March 3-7. Silverlight Classic Arcade Games

Dave Britton

Joined on 06-27-2007
San Francisco Bay Area
Posts 219
09-13-2007 2:02 PM
Marked as Answer
Re: Using Tag Property (problem)

Thanks Dave,

I figured out what the problem was... the value you assign to TAG must be of STRING type.  I was trying to apply a number while iterating thru an array...

this.MovColl.getItem(i).Tag = i

The simple solution ...

this.MovColl.getItem(i).Tag = i.toString();

I do this alot in Browser javascript since I can apply custom attributes and values (to those) at will soo..... <div myattribute="blatt|012;squam|Billy">blah blah</div> ...  which I would parse into a multilevel array.  Very handy to tie data to specific elements.

TAG will allow me to do the same in Silverlight.  Thanks!

tom

TBink

Joined on 05-01-2007
Posts 92
05-06-2008 7:01 PM
Re: Using Tag Property (problem)

 In my SL 2.0 B1 app when I set the Tag property, which is of type object, it converts my value to a string (usually a string representation of the fully qualified class name). This seems like a bug and makes the Tag property not very useful. Shouldn't the internal Property of FrameworkElement.Tag look pretty much like:

 public object Tag { get; set; }
 

ptoinson

Joined on 02-27-2008
Posts 8
05-06-2008 7:03 PM
Re: Using Tag Property (problem)

I guess it's tricky because of reference counting, etc. With strings they don't have to worry about that. I think they are looking into this but no word on whether it's going to make it.


Bill Reiss - Client App Dev MVP (What's an MVP?)
Silverlight and XNA Game Development Tutorials at http://www.bluerosegames.com/brg

Bill Reiss

Joined on 05-01-2007
Posts 470
05-06-2008 7:16 PM
Re: Using Tag Property (problem)

 Reference Counting? Why would it be any different than if I had a Property in a CutomControl defined as:

public object UserData { get; set; } 

Which is what I have done even though the doc state that the Tag Property is meant such that I don't have to do this in a CustomControl. 

ptoinson

Joined on 02-27-2008
Posts 8
05-06-2008 7:23 PM
Re: Using Tag Property (problem)

A lot of Silverlight is in unmanaged C++, so not sure at what level Tag is implemented at. I guess you could generate your tag values and keep your own list of key/value pairs.


Bill Reiss - Client App Dev MVP (What's an MVP?)
Silverlight and XNA Game Development Tutorials at http://www.bluerosegames.com/brg

Bill Reiss

Joined on 05-01-2007
Posts 470