我試圖製造類似組屬性附加傷害到所有標籤網格如何通過Xamarin XAML {} GroupStyle添加樣式定義的ResourceDictionary
我知道如何使這裏面,只是這樣做:
<Grid RowSpacing="2" Padding="2,0,2,0">
<Grid.Resources>
<ResourceDictionary>
<Style BasedOn="{StaticResource Font-Awesome}" TargetType="Label"/>
</ResourceDictionary>
</Grid.Resources>
<Label Text="31 " Grid.Column="0" TextColor="#2764B5" XAlign="Start"/>
<Label Text="91 " Grid.Column="1" TextColor="#A0A1A2" XAlign="Center"/>
<Label Text="12 " Grid.Column="2" TextColor="#A0A1A2" XAlign="End"/>
</Grid>
但它的醜陋和冗餘
我想要做的smetthing像
<Grid RowSpacing="2" Padding="2,0,2,0" Style="{StaticResource grd-actions}">
<Label Text="31 " Grid.Column="0" TextColor="#2764B5" XAlign="Start"/>
<Label Text="91 " Grid.Column="1" TextColor="#A0A1A2" XAlign="Center"/>
<Label Text="Compartilhar " Grid.Column="2" TextColor="#A0A1A2" XAlign="End"/>
</Grid>
而在App靜態資源包括資源字典網格,是這樣的:
<Style x:Key="gd-actions" TargetType="Grid">
<Setter Property="Resources">
<Setter.Value>
<ResourceDictionary>
<Style BasedOn="{StaticResource Font-Awesome}" TargetType="Label"/>
</ResourceDictionary>
</Setter.Value>
</Setter>
</Style>
我已經是有很多方法試圖但它的跳投拋出某種異常!
有人可以幫我嗎?
您想使用XAML樣式像CSS - 但這不是一個完美的模擬。你的第一個例子(你不喜歡)確實是這樣的正確方法。 –
我明白了,所以我無法將樣式定位到網格並像CSS一樣更改所有項目?將所有樣式分組爲僅隱含在視圖元素內部將是非常好的。我真的不喜歡調用靜態資源給所有的東西 –
樣式不會傳播到子元素。 'BindingContext'傳播,並且資源字典的內容的可用性沿元素樹向下傳播,但不是樣式。最終,這是因爲'Style'中的'Setter'元素需要知道目標元素類型是什麼,因爲這是它將查找依賴項屬性定義的地方。 CSS完全不同。 –