2016-12-13 140 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,因爲它更清楚明瞭發生了什麼。 –