简体   繁体   English

多个WPF Windows同时使用同一资源

[英]Multiple WPF Windows using same Resource simultaneous

I had an strange Bug in my application. 我的应用程序中有一个奇怪的错误。 Whenever the AboutDialog would be displayed the companylogo on the mainwindow vanished. 每当将显示AboutDialog时,主窗口上的companylogo就会消失。

I have the discovered that this is related to multiple windows using the same resource simultaneous. 我发现这与同时使用同一资源的多个窗口有关。 In my testapplication I am starting two windows like this: 在我的测试应用程序中,我正在启动两个这样的窗口:

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

On my Mainwindow I am using a ContentPresenter with a path stored as Application resource. 在我的Mainwindow上,我正在使用ContentPresenter,并将路径存储为Application资源。 This does not happen with a string. 字符串不会发生这种情况。 But my logo is only displayed on one Window. 但是我的徽标仅显示在一个窗口上。

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

Does anyone have an idea how to fix this? 有谁知道如何解决这个问题?

Edit: 编辑:

In my real-world-application my logo is stored in a ResourceDictionary so it is not possible to set x:Shared to false. 在我的真实应用程序中,我的徽标存储在ResourceDictionary中,因此无法将x:Shared设置为false。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM