【问题标题】:Multiple WPF Windows using same Resource simultaneous多个 WPF Windows 同时使用相同的资源
【发布时间】:2013-09-30 13:23:17
【问题描述】:

我的应用程序中有一个奇怪的错误。每当显示 AboutDialog 时,主窗口上的公司徽标就会消失。

我发现这与同时使用同一资源的多个窗口有关。 在我的测试应用程序中,我正在启动两个这样的窗口:

void App_Startup(object sender, StartupEventArgs e)
{
  MainWindow mainWindow = new MainWindow();
  mainWindow.Top = 100;
  mainWindow.Left = 900;
  mainWindow.Show();
}

在我的主窗口上,我正在使用 ContentPresenter,其路径存储为应用程序资源。字符串不会发生这种情况。但是我的标志只显示在一个窗口上。

    <StackPanel>
        <ContentPresenter x:Name="Logo" Content="{DynamicResource BrandingLogo}" Margin="20" HorizontalAlignment="Center"/>
        <TextBox Text="{DynamicResource MyConstString}"/>
    </StackPanel>

有人知道如何解决这个问题吗?

编辑:

在我的实际应用程序中,我的徽标存储在 ResourceDictionary 中,因此无法将 x:Shared 设置为 false。

【问题讨论】:

    标签: c# wpf resources resourcedictionary


    【解决方案1】:

    Canvas 资源上设置x:Shared = false 以每次都返回新实例。

       <Canvas x:Key="BrandingLogo" x:Shared = "false"/>
    

    【讨论】:

    • 这非常好,如果我的资源不在 ResourceDictionary 中,它会起作用。 -.- connect.microsoft.com/VisualStudio/feedback/details/776631/…
    • 是的,我在尝试加载我的 ResourceDictionary 时会遇到以下异常。 XamlParseException:命名空间“schemas.microsoft.com/winfx/2006/xaml”中的共享属性只能在已编译的资源字典中使用。
    • 你的字典的构建动作是什么?
    • buildaction 是资源
    • 如解决方法所述..您可以尝试将其更改为 Page 并使用 x:Shared 重试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2011-08-28
    • 2014-01-18
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多