WP7 Uri 作为静态资源?

发布于 2024-12-20 19:32:00 字数 2499 浏览 2 评论 0原文

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

WP7 图像 Uri 作为 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 类的解决方案,请参阅以下问题的最后一个答案:

是是否可以为 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上使用,以及如何实现它,有人可以给我一些提示或示例?或者帮我解决第一个解决方案。 提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏佲洛 2024-12-27 19:32:00

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

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

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

以下是如何构建的示例C# 中的 ApplicationBar。它还允许您添加更多功能,例如透明度切换。

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.

冷︶言冷语的世界 2024-12-27 19:32:00

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

using the datatemplate may figure out your issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文