简体   繁体   English

WP7 Image Uri as StaticResource

[英]WP7 Image Uri as StaticResource

I have a Windows Phone 7 app and I'm trying to move some common elements into resource files. 我有一个Windows Phone 7应用程序,我正在尝试将一些常见元素移动到资源文件中。

Text and Styles work fine but I'm struggling to find the right way to partition out Uri's. 文本和样式工作正常,但我很难找到划分Uri的正确方法。

This is a code sample I'm trying to get to work. 这是我试图开始工作的代码示例。

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="{StaticResource MenuButton1}" Text="Button1"/>
        <shell:ApplicationBarIconButton IconUri="{StaticResource MenuButton2}" Text="Button2"/>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

I have made a AppImages.xaml file and here is the code from there; 我制作了一个AppImages.xaml文件,这里是代码;

<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=mscorlib">
    <sys:String x:Key="MenuButton1">/Images/button1.png</sys:String>
    <sys:String x:Key="MenuButton2">/Images/button2.png</sys:String>
</ResourceDictionary>

It's clear that I can't just use a string but I don't know what I should use. 很明显,我不能只使用字符串,但我不知道应该使用什么。

I am new to WP7 development so if I'm doing something obviously wrong please let me know. 我是WP7开发的新手,所以如果我做了明显错误的事情,请告诉我。

Thanks, Mike 谢谢,迈克

Without testing I'd guess that you're trying to use a string to a parameter which takes a URI. 如果没有测试,我猜你正在尝试将字符串用于带URI的参数。 You could try adding a converter to get it into the right type. 您可以尝试添加转换器以使其成为正确的类型。 The framework would normally make the conversion automatically. 该框架通常会自动进行转换。

Does the debug output show any (binding) errors? 调试输出是否显示任何(绑定)错误?

Rather than defining resources this way you could create a class which exposes static properties for each URI. 您可以创建一个为每个URI公开静态属性的类,而不是以这种方式定义资源。 See example on my blog at: http://blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html 请参阅我的博客上的示例: http//blog.mrlacey.co.uk/2011/03/binding-to-static-classes-in-windows.html

You could try to store the Uri itself as a resource: 您可以尝试将Uri本身存储为资源:

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

Note assembly=System in the xmlns:sys namespace declaration. 注意xmlns:sys命名空间声明中的assembly=System Also, when you type <sys: in the Visual Studio editor, Uri won't be shown in the IntelliSense list, but it seems perfectly fine to use it in XAML. 此外,当您在Visual Studio编辑器中键入<sys:时, Uri将不会显示在IntelliSense列表中,但在XAML中使用它似乎完全没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM