【问题标题】:WP7 Uri as StaticResource?WP7 Uri 作为静态资源?
【发布时间】:2011-12-10 15:46:00
【问题描述】:

我想在资源文件中定义 URI,并在 ApplicationBar 上使用它们。我将其作为以下问题的第一个答案:

WP7 Image Uri as StaticResource

喜欢:

    <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>

但这对我不起作用,无法解析 xaml 文件。

然后我找到了另一个扩展 StaticResourceExtension 类的解决方案,请参阅以下问题的最后一个答案:

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

喜欢:

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;
    }
}

但我不知道它是否可以在windows phone 7上使用,以及如何实现它,有人可以给我一些提示或例子吗?或帮我解决第一个解决方案。 提前致谢。

【问题讨论】:

    标签: windows-phone-7 converter staticresource


    【解决方案1】:

    您不想在 XAML 中执行此操作,因为 ApplicationBar 不支持数据绑定。

    相反,您应该使用 C# 创建 ApplicationBar,它还为您提供了进行本地化的能力。

    至于定义 URL,我建议您使用 .NET 资源文件,或使用导航 URL 定义静态类。首先将 URL 定义为资源的唯一原因是因为您打算重复使用它,因此,您可能还需要从 C# 访问它,因此为什么要使用资源文件最优解。

    Here's an example of how to build a ApplicationBar in C#。它还允许您添加更多功能,例如透明度切换。

    【讨论】:

    • 感谢您的回答。其实我已经弄清楚了本地化问题。首先,在 xaml 文件中定义一个字符串,如:&lt;core:String x:Key="App...Text"&gt;refresh&lt;/core:String&gt; 然后将文件导入 App.xaml 并使用它,如:&lt;shell:ApplicationBarIconButton x:Name="AppBarRefreshButton" Click="AppBarRefreshButton_Click" IconUri="/Themes/.../refresh.png" Text="{StaticResource App...Text}"/&gt; 并且它可以工作。我想对 IconUri 属性做同样的事情,但是不能在属性上使用 xaml 文件中定义的字符串,而且我不知道如何在 xaml 文件中定义 Uri。所以,这是我的问题。
    • 使用&lt;core:Uri&gt;,但是再次,这是一个的主意。
    【解决方案2】:

    使用数据模板可能会解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 2022-12-04
      • 2013-08-01
      • 2014-08-29
      • 2014-07-29
      相关资源
      最近更新 更多