Windows Phone 7 UserExtendedProperties

故事扮演 提交于 2019-12-30 11:11:35

问题


After being directed here: http://msdn.microsoft.com/en-us/library/microsoft.phone.info.userextendedproperties.getvalue%28v=VS.92%29.aspx

I tried the following code on my sideloaded application. All that was written out was the 000001 value.

I knew that would be the case in the emulator, but I was hoping to get a real value when it was on my phone. Any ideas?

                 int ANIDLength = 32;  
                 int ANIDOffset = 2;

                 string result = string.Empty;  
                 object anid;  
                 if (UserExtendedProperties.TryGetValue("ANID", out anid))  
                 {
                     if (anid != null && anid.ToString().Length >= (ANIDLength + ANIDOffset))
                     {
                         result = anid.ToString().Substring(ANIDOffset, ANIDLength);
                     }
                     else
                     {
                         result = "000001";
                     } 
                 }   

回答1:


You need to remove 1 from the calculation of your length check to account for the substring operation working on a zero based index:

 if (anid != null && anid.ToString().Length >= (ANIDLength + ANIDOffset - 1))

This works on my machine:

private string GetAnid()
{
    object anidValue;

    int ANIDLength = 32;
    int ANIDOffset = 2;

    if (UserExtendedProperties.TryGetValue("ANID", out anidValue))
    {
        if (anidValue != null && anidValue.ToString().Length >= (ANIDLength + ANIDOffset - 1))
        {
            return anidValue.ToString().Substring(ANIDOffset, ANIDLength);
        }
        else
        {
            return "???";
        }
    }
    else
    {
        return "???";
    }
}


来源:https://stackoverflow.com/questions/4520387/windows-phone-7-userextendedproperties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!