0

For my Windows Phone 8.1 app, I'm trying to make icons available as static resources by putting them into my XAML page's ResourceDictionary:

<phone:PhoneApplicationPage.Resources>
  <ResourceDictionary>
    <Path x:Name="MyIcon" Width="38" Height="31.6667" Fill="{StaticResource PhoneForegroundBrush}" Data="..."/>
  </ResourceDictionary>
</phone:PhoneApplicationPage.Resources>

When I try to use MyIcon inside a button from code-behind...

Button buyButton = new Button { Content = MyIcon };

...an ArgumentException is thrown at this line: Value does not fall within the expected range.

When referencing MyIcon directly in XAML, it appears correctly in the visual editor, but causes an XAMLParseException while the page is loading.

Am I doing something wrong or is using control objects as static resources just not possible?

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89
  • 1
    see my previous answer [here](http://stackoverflow.com/questions/13292179/best-way-to-use-a-vector-image-in-wpf/13293017#13293017) it will still apply to wp, you'll just turn it into a contentcontrol. – Chris W. May 20 '14 at 15:35
  • That would be a possible workaround, but doesn't really answer the question why mine fails, but thanks anyway. – Cedric Reichenbach May 20 '14 at 15:54
  • I would say because you're not calling it as a staticresource and expecting it to magically find MyIcon, but sorry amigo I'm a xaml / front-end guy so my code behind ignorance won't help you much. Hopefully someone can come teach us both something new. – Chris W. May 20 '14 at 18:04
  • @ChrisW I don't expect it to magically appear. Since above code snippet is located in the page's code-behind class, it actually **is** accessible that way, i.e. all UIElements with an `x:Name` can be addressed that way. If not, I probably would've gotten an error at compile time. – Cedric Reichenbach May 20 '14 at 23:10

1 Answers1

1

You can't use one instance of the UIElement in few places of the visual/logical tree. So your path is currently in the tree and can't be used. I have the same issue in last project, the my solution is a create a path by a value converter.

public class PathConv
    : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var data = value as string;
        if (data == null)
        {
            return value;
        }
        return CreatePath(data);
    }

    private Path CreatePath(string pathString)
    {
        var xaml = string.Format("<Path xmlns='{0}'><Path.Data>{1}</Path.Data></Path>",
            "http://schemas.microsoft.com/winfx/2006/xaml/presentation",
            pathString);

        return XamlReader.Load(xaml) as Path;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now you can create a string resource and convert it to path

<phone:PhoneApplicationPage.Resources>
  <ResourceDictionary>
    <PathConv x:Name="conv" />
    <String x:Key="PathData">...</String>
  </ResourceDictionary>
</phone:PhoneApplicationPage.Resources>

Now you can write something like this

Button buyButton = new Button { Content = conv.Convert(Resources["PathData"], null, null, null) };
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42