我正在使用 XAML 在 C# 中創建一個 WPF 應用程式。我查看了為 XAML 創建樣式的檔案。
在我實際運行我的應用程式之前,它看起來在 Designer 中正常作業。在運行我的應用程式時,我收到一個決議例外。查看指示的線和位置,<Style="{StaticResource T}" />是導致此錯誤的原因。洗掉它可以解決問題,但這需要我做一個Style我想避免的行內。
Page遇到此問題的 XAML 代碼如下,如果您對此問題有任何反饋和指導,我將不勝感激。在這里不起作用的樣式是x:Key="T" TargetType="Border".
<Page x:Class="NGClient1.Screen1.BE2.WindowBe2Tablet"
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"
xmlns:local="clr-namespace:NGClient1.Screen1.BE2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="WindowBe2Tablet" Width="1024" Height="1280" Background="Black">
<Grid>
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Source="Resources/backdrop.png" Stretch="UniformToFill" Grid.ColumnSpan="2" Grid.RowSpan="3" />
<StackPanel Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Advert Section Here" />
</StackPanel>
<Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" BorderBrush="White" BorderThickness="4" Background="Black" Margin="40, 40, 40, 40" Opacity="0.5"/> -->
<Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Style="{StaticResource T}" /> <!-- Exception being thrown here, unsure why -->
<StackPanel Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Lower Section Left Here" />
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="Lower Section Right Here" />
</StackPanel>
</Grid>
</Grid>
<Page.Resources>
<Style x:Key="T" TargetType="Border" > <!-- x:Key causing exception, unsure why -->
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="4"/>
<Setter Property="Background" Value="Black"/>
<Setter Property="Margin" Value="40, 40, 40, 40"/>
<Setter Property="Opacity" Value="0.5"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Page.Resources>
</Page>
uj5u.com熱心網友回復:
使用StaticResources 時,在 XAML 中定義和參考資源的順序很重要。
通過查找對已定義資源的參考,為任何 XAML 屬性特性提供值。該資源的查找行為類似于加載時查找,它將查找先前從當前 XAML 頁面的標記以及其他應用程式源加載的資源,并將該資源值生成為運行中的屬性值-時間物件。
如靜態資源查找檔案中所述:
靜態資源參考無法決議前向參考。
在您的情況下,資源部分位于頁面底部,因此將在第一次通過 參考后定義資源StaticResource。為了解決這個問題,您必須在參考或使用其中定義的資源之前將資源部分移動到一個位置DynamicResource。
<Border Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Style="{DynamicResource T}"/>
DynamicResource Markup Extension 而是通過創建運算式來處理鍵,并且該運算式保持未計算狀態,直到應用程式運行,此時該運算式被計算以提供一個 value。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376940.html
