Question

I want to define URI in the resource files, and use them on the ApplicationBar. I done it as the first answer of the following question:

WP7 Image Uri as StaticResource

likes:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=System">

        <sys:Uri x:Key="MenuButton1">/Images/button1.png</sys:Uri>
        <sys:Uri x:Key="MenuButton2">/Images/button2.png</sys:Uri>
    </ResourceDictionary>

But it doesn't work for me, the xaml file can't be parse.

And then I found another solution that is extending the StaticResourceExtension class, see the last answer of the following question:

Is it possible to supply a type converter for a static resource in WPF?

likes:

public class MyStaticResourceExtension : StaticResourceExtension
{
    public IValueConverter Converter { get; set; }
    public object ConverterParameter { get; set; }

    public MyStaticResourceExtension()
    {
    }

    public MyStaticResourceExtension(object resourceKey)
        : base(resourceKey)
    {
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        object value = base.ProvideValue(serviceProvider);
        if (Converter != null)
        {
            Type targetType = typeof(object);
            IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            if (target != null)
            {
                DependencyProperty dp = target.TargetProperty as DependencyProperty;
                if (dp != null)
                {
                    targetType = dp.PropertyType;
                }
                else
                {
                    PropertyInfo pi = target.TargetProperty as PropertyInfo;
                    if (pi != null)
                    {
                        targetType = pi.PropertyType;
                    }
                }
            }
            value = Converter.Convert(value, targetType, ConverterParameter, CultureInfo.CurrentCulture);
        }
        return value;
    }
}

But I don't know whether it can be used on windows phone 7, and how to implement it, can someone give me some tips or example? or help me fix the first solution. thanks in advance.

Was it helpful?

Solution

You don't want to do this in XAML, since the ApplicationBar have no support for data-binding.

Instead, you should create the ApplicationBar with C#, which also offers you the ability of doing localization.

As for defining the URLs, I recommend you use a .NET Resource File, or define a static class with navigation URLs. The only reason for defining a URL as a resource in the first place, would be because you intend to re-use it, and as such, you're likely to also need to access it from C#, thus why a Resource File would be a optimal solution.

Here's an example of how to build a ApplicationBar in C#. It also allows you to add more features, like transparency toggling.

OTHER TIPS

using the datatemplate may figure out your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top