none
Retrieve x:uid from FrameworkContentElement

    Question

  • Hi,

    I'm trying to build a state persist for WPF (persist the windows locations, column width, etc).

    Most of the example code I saw use the property PersistId which is obsolete. After reading about globalization and localization I decide to use x:uid property as key.

    The problem is that now all the UI elements have that property - UIElement and all it's children have this property, but FrameworkContentElement does not. One of the derived class of FrameworkContentElement is ColumnDefinition of Grid which is important one for me. There is no problem settings x:uid in xaml for ColumnDefinition but I don't understand how to retrieve the value in code.

     

    Thank you,

    Ido

    Thursday, June 24, 2010 7:52 PM

All replies

  • Try this:

    // Obtain the BAML stream.
    Stream source = entry.Value as Stream;
    
    // Create a BamlLocalizer on the stream.
    BamlLocalizer localizer = new BamlLocalizer(source);
    BamlLocalizationDictionary resources = localizer.ExtractResources();
    
    // Write out all the localizable resources in the BAML.
    foreach (DictionaryEntry resourceEntry in resources)
    {
      BamlLocalizableResourceKey key = resourceEntry.Key as BamlLocalizableResourceKey;
      BamlLocalizableResource value = resourceEntry.Value as BamlLocalizableResource;
      Console.WriteLine(
        "  {0}.{1}.{2} = {3}",
        key.Uid,
        key.ClassName,
        key.PropertyName,
        value.Content
        );                
    }
    

    Javaman
    Friday, June 25, 2010 2:49 AM
  • Hi,

    Thanks, you directed me to interesting direction.

    I've manage to complete your code with this one:

         IUriContext uriContext = (IUriContext)serviceProvider.GetService(typeof(IUriContext));
         var streamInfo = Application.GetResourceStream(uriContext.BaseUri);
         BamlLocalizer bamlLoc = new BamlLocalizer(streamInfo.Stream);
    

    The problem is that this is localization oriented API (as they should be) and I can't see how to find the item I need in the baml dictionary.

     

    I have a class derived from MarkupExtension which needs to generate a key for each item using it.

    I have this code which simply extract the element and property which use with this markup extension:

      public override object ProvideValue(IServiceProvider serviceProvider) {
       IProvideValueTarget provideValue = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
       if (provideValue == null) {
        throw new NotSupportedException("The IProvideValueTarget is not supported");
       }
    
       DependencyObject targetObject = provideValue.TargetObject as DependencyObject;
       if (targetObject == null) {
        Debug.Fail(string.Format("can't persist type {0}, not a dependency object", provideValue.TargetObject));
        throw new NotSupportedException();
       }
    
       DependencyProperty targetProperty = provideValue.TargetProperty as DependencyProperty;
       if (targetProperty == null) {
        Debug.Fail(string.Format("can't persist type {0}, not a dependency property", provideValue.TargetProperty));
        throw new NotSupportedException();
       }
    
       // build string key here

    So I have targetObject in hand and possibly the BamlLocalizationDictionary but I don't see how to relate those two.

    If you have other idea of how to generate string key it will be great.

     

    Thank you,

    Ido

    Friday, June 25, 2010 7:30 AM
  • Hi, I wanted to bump this as I have similar problem. Maybe the OP found the answer ?

    Anyone know how to retrieve x:Uid programatically from objects derived from FrameworkContentElement ?

    As UIElements have a Uid Property I can use:

     

    PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(myParagraph, new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) });<br/>
    PropertyDescriptor dp = (PropertyDescriptor)propertyDescriptors.Find("Uid", false);<br/>
    <br/>
    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(dp);<br/>
    DependencyProperty dpUID = dpd.DependencyProperty;<br/>
    string uidTest = (string)(myParagraph as DependencyObject).GetValue(dpUID);
    

     

    However as  FrameworkContentElement and its derivatives don't have a UId property or a UidPropertyAttribute defined I can see no way of retrieving the UId that has been applied in XAML. Does anyone know if it's available somehow ?

    Thanks.

    Wednesday, December 22, 2010 6:16 PM