wpf學習20180606

來源:互聯網
上載者:User

標籤:xmla   目錄   XA   空間   str   .sh   輸出   對象   using   

對象元素的子項目

有三類子項目:內容屬性、集合項目、值(類型轉換)

-----------------------------------------------------------------------------

內容屬性

元素對象有Content或Items屬性的,這就是內容屬性,可以包含元素對象或文本。

集合項目

兩種類型的集合:List、Dictionary

類型轉換

普通文本作為元素的子項目。

---------------------------------------------------------------------------

xaml元素必須有預設建構函式

-------------------------------------------------------------------------------------------------

運行時載入和解析XAML

使用代碼解析載入XAML檔案

XamlReader對象

如果項目根目錄有個Win.xaml的檔案,用代碼解析載入並顯示。

Window win = null;

using(FileStream fs = new FileStream ("Win.xaml", FileMode.Open, FileAccess.Read))

{

  win = (Window)XamlRead.Load(fs);

  win.show();

}

注意事項:FileStream的命名空間System.IO;XamlRead的命名空間System.Windows.Markup;

使用檔案流,需要讀取物理檔案,所以Win.xaml需要調整設定,複製到輸出目錄:始終複製;產生操作:無。

這些設定在頁面的屬性設定中。同時要刪除頁面的後置代碼檔案。Win.xaml要刪除xmlans=class""的設定。+

win.xaml的xmal代碼

<Window>

  <StackPanel>

    <Button>

      ......

    <Button>

    .......

......

</Window>

如何找到第二個按鈕並給按鈕設定文本

win = (Window)XamlRead.Load(fs);

StackPanel sp = (StackPanel)win.Content;

Button btn = sp.Children[1];

btn.Content="找到";

這是根據子集的個數順序寫入程式碼找對象,有更簡單的方法嗎?有

----------------------------------------------------------------------------------------

Xaml元素命名

<Window>

  <StackPanel>

    <Button>

      ......

    <Button X:Name="btn1">

    .......

......

</Window>

 

win = (Window)XamlRead.Load(fs);

 

Button btn = (Button)win.FindName("btn1");

btn.Content="找到";

 

wpf學習20180606

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.