【问题标题】:Uri in Resource dictionary in WP7WP7资源字典中的Uri
【发布时间】:2012-09-21 07:17:13
【问题描述】:

我在我的 wp7 项目中的主题文件夹中定义了一个资源字典,名称为 darktheme.xaml

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

    <sys:Uri x:Key="AppBarSettingsImage">/Images/dark/Settings.png</sys:Uri>
    <sys:Uri x:Key="AppBarTimingsImage" >/Images/dark/Timings.png</sys:Uri>

</ResourceDictionary>

我称这是我的 App.xaml 像这样

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/DarkTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

我将所有图像作为构建动作内容和 CopyIfnewer 并将我的主题构建动作作为页面

一旦我运行我的项目,它就会抛出未处理的异常来加载资源字典。但是当我在我的主题(资源字典)中注释掉这段代码时,它就开始工作了。

<sys:Uri x:Key="AppBarSettingsImage">/Images/dark/Settings.png</sys:Uri>
<sys:Uri x:Key="AppBarTimingsImage" >/Images/dark/Timings.png</sys:Uri>

实际上,我正在设置这些 uri 来设置我的 appbar iconuri 属性以使用我的这些静态资源进行设置。正如这里所讨论的 WP7 Image Uri as StaticResource

【问题讨论】:

    标签: c# windows-phone-7 xaml expression-blend resourcedictionary


    【解决方案1】:

    很遗憾,您无法将(使用静态资源)绑定到 ApplicationBarIconButton。它不是 Silverlight 控件,它只是用于与 Windows Phone 7 操作系统进行低级互操作的包装对象。所以不能数据绑定。

    我可以建议两个选项来做到这一点。

    第一个也是更简单的一个:您可以从代码隐藏中对其进行操作。在这里您也可以访问您的资源字典,它只是我的一个工作示例(带有硬编码字符串)。

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        //this.AppBarButton1.IconUri = new Uri("/Images/dark/Timing.png"); //WRONG NullReferenceException
        var button1 = this.ApplicationBar.Buttons[1] as ApplicationBarIconButton;
        button1.IconUri = new Uri(@"./Images/dark/Timing.png", UriKind.Relative);
    }
    

    第二个需要更多编码: 您可以实现自己的 ApplicationBarIconButton。您需要从 ApplicationBarMenuItem 派生并实现 Microsoft.Phone.Shell.IApplicationBarIconButton。 之后,您可以将 DependencyProperty 添加到您自己的控件中,例如:

    public Uri IconUri
        {
            get { return (Uri)GetValue(IconUriProperty); }
            set { SetValue(IconUriProperty, value); }
        }
    
    // Using a DependencyProperty as the backing store for IconUri.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IconUriProperty =
        DependencyProperty.Register(
            "IconUri",
            typeof(Uri),
            typeof(ApplicationBarIconButton),
            new PropertyMetadata(default(Uri), (d, e) => ((ApplicationBarIconButton)d).IconUriChanged((Uri)e.NewValue)));
    
    private void IconUriChanged(Uri iconUri)
    {
        var button = SysAppBarMenuItem as Microsoft.Phone.Shell.IApplicationBarIconButton;
        button.IconUri = iconUri;
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多