动态资源无法加载到以编程方式创建的控件中
我有一个 WPF (3.5) 应用程序,使用 Prism 以编程方式实例化多个视图,然后将它们添加到一个区域。我看到的问题是,第一次显示视图时,作为 DynamicResources 应用的视图内的样式不会被应用。如果我们更改屏幕并返回,它将正确加载,相当肯定这是由于加载和卸载控件所致。
失败的样式是在我们的根视图中定义的样式。根视图与子视图位于同一个类库中,将它们添加到应用程序资源不是一个选项,但它似乎可以解决问题。
我已经在示例应用程序中复制了该问题。
<Window x:Class="ProgrammaticDynamicResourceProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:ProgrammaticDynamicResourceProblem"
Height="350" Width="525">
<Window.Resources>
<Style x:Key="RedTextStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red" />
</Style>
</Window.Resources>
<StackPanel x:Name="root">
<l:TestUC /> <!-- Will have a foreground of Red -->
</StackPanel>
</Window>
示例 UserControl
<UserControl x:Class="ProgrammaticDynamicResourceProblem.TestUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock Text="Test Text" Style="{DynamicResource RedTextStyle}" />
</UserControl>
在 MainWindow 构造函数中,我添加了 TestUC 的另一个实例。
public MainWindow()
{
InitializeComponent();
root.Children.Add(new TestUC());
}
当应用程序加载时,第一个实例将具有预期的红色前景,从构造函数添加的前景将是默认的黑色。
有趣的是,如果我将构造函数修改为如下所示,它就可以工作。
public MainWindow()
{
InitializeComponent();
root.Children.Add(new TestUC());
var x = root.Children[1];
root.Children.RemoveAt(1);
root.Children.Add(x);
}
有没有一个好的解决方案可以让它发挥作用?将资源添加到应用程序资源不起作用,因为我们在同一应用程序中有其他 shell,并且这些资源是特定于 shell 的。我们可以将资源字典合并到每个视图中并将它们切换到 StaticResources,但是视图相当多,因此我们也希望避免这种解决方案。
更新:发现这个连接问题,但确实没有多大帮助。
I have a WPF (3.5) application using Prism to programmatically instantiate a number of views and then add them to a region. The problem I am seeing is that styles inside the view that are applied as DynamicResources are not being applied the first time the view is shown. If we change screens and come back, it will be loaded properly, fairly certain this is due to loading and unloading the control.
The styles that fail are the ones are defined in our root view. The root view is in the same class library as the child view, adding them to Application Resources is not an option, however it does appear to fix the problem.
I've replicated the problem in a sample application.
<Window x:Class="ProgrammaticDynamicResourceProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:ProgrammaticDynamicResourceProblem"
Height="350" Width="525">
<Window.Resources>
<Style x:Key="RedTextStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red" />
</Style>
</Window.Resources>
<StackPanel x:Name="root">
<l:TestUC /> <!-- Will have a foreground of Red -->
</StackPanel>
</Window>
The sample UserControl
<UserControl x:Class="ProgrammaticDynamicResourceProblem.TestUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock Text="Test Text" Style="{DynamicResource RedTextStyle}" />
</UserControl>
In the MainWindow constructor I add another instance of a TestUC.
public MainWindow()
{
InitializeComponent();
root.Children.Add(new TestUC());
}
When the application loads, the first instance will have a foreground of Red as expected, the one added from the constructor will be the default black.
Interestingly, if I modify the constructor to look like this, it works.
public MainWindow()
{
InitializeComponent();
root.Children.Add(new TestUC());
var x = root.Children[1];
root.Children.RemoveAt(1);
root.Children.Add(x);
}
Is there a decent solution to getting this to work? Adding the resources to the Application Resources doesn't work because we have other shells in the same application and these resources are shell specific. We can merge the resource dictionaries into each of the views and switch them to StaticResources, but there are quite a few views, so we would like to avoid that solution as well.
UPDATE: Found this Connect Issue, but it really didn't help much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常奇怪的问题,看来我只认为具有继承标志的依赖属性。
如果您在 RedTextStyle 中设置 Background 属性,它将正常更新。
所以我找到了两种方法来解决这个问题:
在文本元素上使用
ClearValue(TextElement.ForegroundProperty)
。或者将样式添加到 App.xaml 并使用一些默认值,如下所示:
Very strange problem and it is appeares i think only on dependencyproperties with inheritance flag.
If you set Background property in RedTextStyle it would update normally.
So i find two ways to resolve this problem:
Use
ClearValue(TextElement.ForegroundProperty)
on text element.Or Add style to App.xaml with some default value, like this: