2016-12-13 138 views
0

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

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

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

windows 10 before button clicked

windows 10 after button clicked

Windows XP before button clicked

Windows XP after button clicked

更新:对不起,我不包括例外:

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

enter image description here

编辑后: 这是按键点击:

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

和XAML用户控件的: enter image description here

奖金问题:: 因为我的应用程序是相当大的,不论我用窗口,现在我必须切换到用户控制这种方法工作...因为如果我开关

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

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

它弹出例外...什么办法来解决这个问题...如果没有..我将改变窗口的用户控件没有问题... ...

enter image description here

和一个大感谢先生“MM8”固定我的问题:))

+0

做你的客户有很好的理由不从XP升级?操作系统是一个恐龙,支持已被拉下。 – Takarii

+0

相信我我说过..但是他们说他们有太多使用XP的电脑了......所以它就像我被迫..我们正在许多电脑上安装软件..不仅仅是一个..所以我有点被迫。 ..即使我不喜欢它..但是... – ShadyOverflow

+0

所以我很清楚,Window1 litterally“填充”MainWindow时,它被称为? – Takarii

回答

1

你可以窗口1的内容移动到用户控件:

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

然后,您可以简单地创建这种用户控件的实例,并在主窗口设置的ContentPresenter的内容属性,这一个:

this.contentControl.Content = new UserControl1(); 
+0

刚试过这个,在win 10上运行,它在win xp上引发同样的异常。 – ShadyOverflow

+0

UserControl1的XAML标记和代码如何看起来像那样? – mm8

+0

我编辑的问题,有一个新的PIC,顺便说一句我没有任何代码在usercontrol1除了默认(初始化组件..) – ShadyOverflow

0

这是你应该使用什么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> 
+0

是啊这个作品,第一个答案的作品是相同的方式,除了我没有正确使用它..,contentControl和内容演示者之间的区别究竟是什么?任何想法为什么我应该切换到它? – ShadyOverflow

+0

ContentControl用于显示“content”的控件,ContentPresenter用于显示“controls”的内容。在我看来,拥有一个ContentPresenter稍微干净一点,然后用动态数据更新ContentPresenter,因为它更清楚明了发生了什么。 –