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

Multiple WPF Windows using same Resource simultaneous
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我的应用程序中有一个奇怪的错误.每当将显示AboutDialog时,主窗口上的companylogo就会消失.

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

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

在我的Mainwindow上,我使用ContentPresenter,并将路径存储为Application资源.字符串不会发生这种情况.但是我的徽标仅显示在一个窗口上.

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

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

编辑:

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

解决过程1

设置x:Shared = falseCanvas资源来回报新实例每次.

   <Canvas x:Key="BrandingLogo" x:Shared = "false"/>
速聊1:
这非常好,如果我的资源不在ResourceDictionary中,它将可以正常工作.-.- connect.microsoft.com/VisualStudio/feedback/details/776631/...
速聊2:
是的,当我尝试加载ResourceDictionary时会出现以下异常.XamlParseException:名称空间"schemas.microsoft.com/winfx/2006/xaml"中的共享属性只能在编译的资源字典中使用.
速聊3:
你的字典有什么作用?
速聊4:
buildaction是资源
速聊5:
如所指出的解决方法..您可以尝试将其更改为Page并使用x:Shared重试吗?

I had an strange Bug in my application. Whenever the AboutDialog would be displayed the companylogo on the mainwindow vanished.

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. 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.

Solutions1

Set x:Shared = false on the Canvas resource to return the new instance everytime.

   <Canvas x:Key="BrandingLogo" x:Shared = "false"/>
Talk1:
This is very good and would work if my Resource would not be located in a ResourceDictionary. -.- connect.microsoft.com/VisualStudio/feedback/details/776631/…
Talk2:
Yes, i will get following exception when trying to load my ResourceDictionary. XamlParseException: Shared attribute in namespace 'schemas.microsoft.com/winfx/2006/xaml' can be used only in compiled resource dictionaries.
Talk3:
what is the buildaction of your dictionary?
Talk4:
buildaction is Resource
Talk5:
as the workaround stated.. can you try changing it to Page and try again with x:Shared
转载于:https://stackoverflow.com/questions/19095412/multiple-wpf-windows-using-same-resource-simultaneous

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入