第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 XAML 中重復編程行?

如何在 XAML 中重復編程行?

PHP
斯蒂芬大帝 2024-01-20 16:12:35
...術(shù)語“編程行”是指代碼的一部分。我目前正在使用 c# 和 XAML 創(chuàng)建 UI。但 XAML 代碼變得越來越長,因此我意識到,如果我能夠以某種方式在代碼內(nèi)部設(shè)置或單獨存儲代碼的可重復部分并在每次需要時使用它們,則整個 XAML 代碼將會更短、更清晰。例如,假設(shè)我有一個特定的標簽,我想在代碼的幾個點中重復該標簽:  <Label Name="myLabel" Content="something">   </Label>我如何才能在 XAML 代碼中應用并重復該標簽?
查看完整描述

1 回答

?
蝴蝶刀刀

TA貢獻1801條經(jīng)驗 獲得超8個贊

有一個簡單的示例,說明如何在不同的視圖/窗口之間共享 XAML 代碼。創(chuàng)建一個ResourceDictionary,定義共享屬性/樣式/控件模板,如下所示


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

? ? ? ? ? ? ? ? ? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

? ? <Style TargetType="{x:Type Label}">

? ? ? ? <Setter Property="Foreground" Value="Black" />

? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />

? ? </Style>

? ? <Style TargetType="{x:Type Button}">

? ? ? ? <Setter Property="Foreground" Value="Black" />

? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />

? ? </Style>

? ? <Style TargetType="{x:Type TextBox}">

? ? ? ? <Setter Property="Foreground" Value="Black" />

? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />

? ? </Style>

? ? <Style TargetType="{x:Type ScrollBar}">

? ? ? ? <Setter Property="Foreground" Value="Black" />

? ? ? ? <Setter Property="FontFamily" Value="Segoe UI" />

? ? </Style>

? ? <Style TargetType="Label" x:Key="TitleStyle" BasedOn="{StaticResource {x:Type Label}}">

? ? ? ? <Setter Property="HorizontalContentAlignment" Value="Center"/>

? ? ? ? <Setter Property="VerticalContentAlignment" Value="Center" />

? ? ? ? <Setter Property="HorizontalAlignment" Value="Stretch" />

? ? ? ? <Setter Property="VerticalAlignment" Value="Stretch"/>

? ? ? ? <Setter Property="FontSize" Value="16" />

? ? </Style>

</ResourceDictionary>

您可以將此字典添加到應用程序/窗口MergedDictionaries來使用它們,例如


<Window.Resources>

? ? <ResourceDictionary>

? ? ? ? <ResourceDictionary.MergedDictionaries>

? ? ? ? ? ? <ResourceDictionary Source="Styles.xaml"/>

? ? ? ? </ResourceDictionary.MergedDictionaries>

? ? </ResourceDictionary>

</Window.Resources>

請注意,這只是一個簡單的例子來簡要解釋這個想法。您還可以查看Style.TargetType文檔以查看樣式之間TargetTypex:Key樣式中的解釋



查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號