【问题标题】:How to use ResourceDictionary as StaticResource in Windows Phone 8 (element not fount)如何在 Windows Phone 8 中将 ResourceDictionary 用作 StaticResource(元素不是源)
【发布时间】:2014-01-16 07:28:04
【问题描述】:

我正在创建一个 Windows Phone 8 应用程序。在应用程序中,我在 XAML 文件中定义了一个 ResourceDictionary:

     <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="YellowGreen" />
        <Setter Property="FontSize" Value="35" />
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>

    <Style TargetType="TextBox" x:Key="CommonSetters">
        <Setter Property="Width" Value="450"/>
        <Setter Property="FontSize" Value="35" />
        <Setter Property="Foreground" Value="YellowGreen" />
        <Setter Property="Height" Value="100"/>
        <Setter Property="Background" Value="Red">
            <!--<Setter.Value>
                <ImageBrush ImageSource="logo.png" Opacity="0.1"/>
            </Setter.Value>-->
        </Setter>
    </Style>

</ResourceDictionary>

此 ResourceDictionary 在 App.xaml 中被引用为:

xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:Work_Force" x:Key="LocalizedStrings"/>
        <ResourceDictionary x:Key="myDict">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resource.xaml"/>
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
        <Style TargetType="TextBlock" x:Key="NN">
            <Setter Property="Width" Value="450"/>
            <Setter Property="FontSize" Value="35" />
            <Setter Property="Foreground" Value="YellowGreen" />
            <Setter Property="Height" Value="100"/>
        </Style>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

然后是最简单的部分:

Static Resources NN 工作正常,但 commonSetters 无法正常工作,它在 resourse.xaml 中声明。

【问题讨论】:

    标签: windows windows-phone-8


    【解决方案1】:

    按照您在 App.xaml 中定义资源的方式,您的“myDict”资源字典是 Application.Resources 默认字典中的嵌套字典。我不知道如何在 XAML 中引用嵌套字典中的资源的任何语法(因为我还不需要这个;我认为后面代码中的 FindResource(...) 可以做到这一点)。解决问题的一种方法是,您直接修改 Application.Resources 的默认字典以与您的“资源”字典合并。您可以通过修改 Application.Resources 部分来做到这一点:

    <Application.Resources>
        <ResourceDictionary>
            <local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    请注意,现在您不再拥有名为“myDict”的 ReosurceDictionary。您的“资源”字典将直接合并到没有键的默认字典中。那么资源就会被正确解析。

    【讨论】:

      【解决方案2】:

      最后我得到了类似于 aspx 或 html 页面的解决方案,我们在所需页面中添加样式的引用

      无论你在哪里,都需要在你需要的页面中添加页面的引用

             <phone:PhoneApplicationPage.Resources>
                <ResourceDictionary Source="yourstylepage.xaml"/>     
              </phone:PhoneApplicationPage.Resources>
      

      把上面的代码放在&lt;phone:PhoneApplicationPage&gt;之后

      【讨论】:

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