Windows XP内容渲染

Windows XP content rendering

我有一个用 .NET 4.5 制作的应用程序,用于较新的 PC,但现在我有一个 Windows XP 客户端:/ 所以我必须还原,我已成功还原到 .net 4 for Windows XP,在 Windows 7 > Windows 10 上一切正常。但是我在 Windows XP 中遇到了一些严重的内容渲染问题,现在在我问这里之前,我搜索了 Google 并在这里找到解决方案。但到目前为止还没有运气。这就是问题, 我有 MainWindow 和 Window1(示例),main window 包含一些红色背景标签、按钮和一个 contentcontrol(当前为 null,因此它不可见),在单击按钮时,我有这条线:

this.contentControl.Content = new Window1().Content;

Window 1 背景设置为红色,所以它只是改变了背景,这在我的机器上有效(windows 10)但是同样的应用程序在 win xp 上崩溃了,看看图片。有任何想法吗? (我的整个应用程序都是使用 .Content 来更改 windows 所以我必须以某种方式修复变化内容的绘图,有什么想法可以尝试吗?)。

更新:很抱歉我没有包含例外:

Specified element is already the logical child of another element. Disconnect it first.

编辑后: 这是点击按钮:

this.contentControl.Content = new UserControl1().Content;
用户控件的

和xaml:

奖金问题:: 因为我的应用程序非常大,而且我在任何地方都使用 window,现在我必须切换到用户控件才能使此方法正常工作...因为如果我切换

this.contentControl.Content = new UserControl() { Background = Brushes.Green };

this.contentControl.Content = new Window() { Background = Brushes.Green };

再次弹出异常...任何解决此问题的方法...如果没有...我会将 windows 更改为用户控件没问题...

非常感谢 "mm8" 先生解决了我的问题:))

您可以将 Window1 的内容移动到 UserControl:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="Window8" Height="300" Width="300">
<Grid>
    <local:UserControl1 />
</Grid>

然后您可以简单地创建此 UserControl 的一个实例,并将 MainWindow 中 ContentPresenter 的 Content 属性 设置为此:

this.contentControl.Content = new UserControl1();

这就是您应该使用 ContentPresenter 的目的。

更新您的主窗口:

<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ContentPresenter x:Name="ContentPresenter"/>
        <Label Content="yoooooo"/>
        <Button Height="50" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

点击事件/代码隐藏:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var newControlContent = new ContentControl
    {
        Content = new UserControl1()
    };
    ContentPresenter.Content = newControlContent;
}

用户控件:

<UserControl x:Class="WpfApplication9.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Background="PaleVioletRed">

    </Grid>
</UserControl>