0

我从事 WPF 项目已经有一段时间了,但现在已经有 2 个月没有碰它了。现在我回来了,我有以下问题:

我使用合并字典来创建皮肤,并在运行时轻松交换它们。但是,每个皮肤在运行时都不会改变。但是,虽然我对此不以为然,但我只是遵循了一堆教程,并且到处使用 DynamicResource,甚至不知道是否应该使用 StaticResource。

结果是,当我尝试使用 Windows XP(我使用七个)将我的应用程序发送给朋友时。Luna 主题被抛出我的应用程序,使设计变得丑陋......据我所知,它来自 DynamicResource。

所以现在,我想更改为StaticResource,我只是尝试用“StaticResource”替换每个“DynamicResource”。它向我抛出了这个异常:

Message = "'La valeur fournie sur 'System.Windows.StaticResourceExtension' a levé une exception.' numéro de ligne '8' et position de ligne '230'."

In english, it means: Message = "'The value given to 'System.Windows.StaticResourceExtension' threw an exception.' line number '8' and position on the line '230'.

然后我放了一个 try/catch 块来更好地了解问题:

{"Impossible de trouver la ressource nommée'styleBackground'. Les noms de ressources respectent la casse."}

用英语:Impossible to find the resource named 'styleBackground'. The resource names are case sensitive.

这是关于一个 TabItem 的。

所以这里有两个问题:我是否通过将 DynamicResource 更改为 StaticResource 来做正确的事情?如果是这样,我需要做什么?

谢谢,

FB。

编辑:程序在这些行的最后一行得到错误:

<Window x:Class="Sc2ReplayMonkey.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:local="clr-namespace:Sc2ReplayMonkey" ResizeMode="CanResize"
        Title="Starcraft II Replay Monkey"
        Width="991" Icon="Resources\Icons\SC2_Replay_Monkey.png"
        WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="1024" Height="774" Visibility="Visible" Foreground="#00000000" Margin="0">
4

2 回答 2

0

您如何认为问题来自 DynamicResource ?

据我所知,DynamiResource 与 StaticResource 不同,仅用于在运行时获取资源,而不是在编译时。DynamicResource 在您必须在声明之前引用资源的情况下很有用(如果您使用 StaticResource,则会引发编译错误)。

<ResourceDictionary><DataTemplate>... <Rectangle Fill={DynamicResource MyBrush}></Rectangle>  ...</DataTemplate> <SolidColorBrush x:Key="MyBrush"></SolidColorBrush> <ResourceDictionary>

在这种情况下,使用 StaticResource 而不是 DynamicResource 将引发编译错误。

现在,关于您的问题,如果您没有为控件明确分配样式,WPF 将从当前操作系统主题中选择默认样式,在您的情况下为 XP 主题。要解决这个问题,您可以选择 Win 7 主题,然后将其嵌入到您的应用程序中。

这为您增加了更多工作,但这是重新定义所有控件样式以使其保留这些样式的唯一方法,无论它在哪个操作系统上执行。

高温高压

里亚纳

于 2011-04-01T11:29:10.663 回答
0

解决方案是在启动时加载所有资源字典,而不是从没有皮肤(适用于动态资源)开始并让用户在运行时选择皮肤。

于 2011-08-27T09:48:01.813 回答