2016-12-25 32 views
-2

首先,對不起,這個跛腳的問題。不過,我自己正在學習WPF。 我現在正在學習Panel課程。要了解這一點,我正在瀏覽這個MSDN網頁。 MSDN描述的使用代碼創建Panel的方式吸引了我很多。例如如何在WPF中實現以下內容

// Create the application's main window 
mainWindow = new Window(); 
mainWindow.Title = "Canvas Sample"; 

// Create the Canvas 
myParentCanvas = new Canvas(); 
myParentCanvas.Width = 400; 
myParentCanvas.Height = 400; 

// Define child Canvas elements 
myCanvas1 = new Canvas(); 
myCanvas1.Background = Brushes.Red; 
Canvas.SetLeft(myCanvas1, 0); 
. 
. 
. 
myParentCanvas.Children.Add(myCanvas3); 

// Add the parent Canvas as the Content of the Window Object 
mainWindow.Content = myParentCanvas; 
mainWindow.Show(); 

我希望在網頁上顯示的方式來創建Panel(佈局),但我很困惑,我應該寫(複製粘貼)這些代碼。我很精通使用XAML

創建Layoutfilename.xaml.cs我們有以下的代碼行

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

    } 
} 

我試圖把這個代碼(MSDN的)線內MainWindow方法,但它並沒有幫助我很多。根據我的小知識,我們應該有一個方法,如mainWindowContent(),這應該是InitializeComponent()或類似的調用(我可能是這個錯誤)。

請以正確的方式指導我實現此目標。

+0

顯示[MCVE]你想在XAML什麼,也許可以提供一個代碼等同。 – Nkosi

+1

顯示的文章中的示例很可能是基於評論在啓動時完成的。如果你已經有一個主窗口,你可以在'InitializeComponents'後執行MainWindow構造函數中的所有操作。 – Nkosi

+0

SO不是語言,更不用說_API_轉換服務 – MickyD

回答

1
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.Title = "Canvas Sample"; 

     // Create the Canvas 
     var myParentCanvas = new Canvas(); 
     myParentCanvas.Width = 400; 
     myParentCanvas.Height = 400; 

     // Define child Canvas elements 
     var myCanvas1 = new Canvas(); 
     myCanvas1.Background = Brushes.Red; 
     myCanvas1.Height = 100; 
     myCanvas1.Width = 100; 
     Canvas.SetTop(myCanvas1, 0); 
     Canvas.SetLeft(myCanvas1, 0); 

     var myCanvas2 = new Canvas(); 
     myCanvas2.Background = Brushes.Green; 
     myCanvas2.Height = 100; 
     myCanvas2.Width = 100; 
     Canvas.SetTop(myCanvas2, 100); 
     Canvas.SetLeft(myCanvas2, 100); 

     var myCanvas3 = new Canvas(); 
     myCanvas3.Background = Brushes.Blue; 
     myCanvas3.Height = 100; 
     myCanvas3.Width = 100; 
     Canvas.SetTop(myCanvas3, 50); 
     Canvas.SetLeft(myCanvas3, 50); 

     // Add child elements to the Canvas' Children collection 
     myParentCanvas.Children.Add(myCanvas1); 
     myParentCanvas.Children.Add(myCanvas2); 
     myParentCanvas.Children.Add(myCanvas3); 

     // Add the parent Canvas as the Content of the Window Object 
     this.Content = myParentCanvas; 
    } 
} 

如果自身內部的MainWindow代碼隱藏做過相當於

<Window Title="Canvas Sample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Canvas Height="400" Width="400"> 
    <Canvas Height="100" Width="100" Top="0" Left="0" Background="Red"/> 
    <Canvas Height="100" Width="100" Top="100" Left="100" Background="Green"/> 
    <Canvas Height="100" Width="100" Top="50" Left="50" Background="Blue"/> 
    </Canvas> 
</Window> 

,然後啓動你可以調用類

// Create the application's main window 
var mainWindow = new MainWindow(); 
mainWindow.Show(); 
+0

感謝您的指導。那麼,我想問一個問題 - 「爲什麼你不需要這樣做('type objectName = new type()')?我不得不修改這個代碼爲'type objName ... pe();'謝謝! –

+1

'從Visual C#3.0開始,在方法範圍聲明的變量可以有一個隱式類型var.' https://msdn.microsoft.com/en-us/library/bb383973.aspx。 – Nkosi

+0

我知道var關鍵字以及它的用途我想在MSDN示例代碼的上下文中提問** MSDN不應該像我們所有開發人員一樣**嘗試編寫正確的代碼**。 –