2014-02-14 90 views
0

此問題最初公佈爲here。儘管OP接受了解決方案,但我仍然無法弄清楚導致異常的原因。我做了一些進一步的測試並失敗了。分配給屬性時XamlParseException

的代碼非常簡單 - Windows Phone的應用程序只使用XAML內容:

<phone:PhoneApplicationPage.Resources> 
    <Image x:Key="IButton" Source="Resources/firstImage.png"/> 
</phone:PhoneApplicationPage.Resources> 

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="1*"/> 
     <RowDefinition Height="1*"/> 
    </Grid.RowDefinitions> 
    <Button x:Name="first" Content="{StaticResource IButton}" VerticalAlignment="Center" Grid.Row="0"/> 
    <Button x:Name="second" VerticalAlignment="Center" Grid.Row="1"> 
     <Image Source="Resources/firstImage.png"/> 
    </Button> 
</Grid> 

乍看之下,一切正常,VS設計師正確顯示兩個Button裏面的圖像。當我試圖剝奪應用程序時,我得到XamlParseException:

未能分配給屬性'System.Windows.Controls.ContentControl.Content'。

問題涉及第一個按鈕 - 第二個是沒有問題

很奇怪運行。我嘗試過改變Build Action(資源/內容),清理項目,但沒有成功。

相反,非常相似的WPF應用程序沒有問題。你剛打運行,看到兩個按鈕:

<Window.Resources> 
    <Image x:Key="IButton" Source="Resources/firstImage.png"/> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="1*"/> 
     <RowDefinition Height="1*"/> 
    </Grid.RowDefinitions> 
    <Button x:Name="first" Content="{StaticResource IButton}" VerticalAlignment="Center" Grid.Row="0"/> 
    <Button x:Name="second" VerticalAlignment="Center" Grid.Row="1"> 
     <Image Source="Resources/firstImage.png"/> 
    </Button> 
</Grid> 

大家有一個想法是什麼可能是錯的?這兩個應用程序(WP/WPF)你can get here

回答

0

可能是這種差異

<Button x:Name="second" VerticalAlignment="Center" Grid.Row="1"> 
<Button.Content> 
     <Image Source="Resources/firstImage.png"/> 
</Button.Content> 
</Button> 

一個Button.Content標籤是額外這裏。

此外,圖像的構建操作應該設置爲這裏的內容。

這是在我的情況至少工作。

+0

這不是問題所在。第二個按鈕沒有問題。你的代碼當然會起作用。問題涉及第一個按鈕以及它不起作用的原因。第二個按鈕僅用於檢查圖像文件是否存在等。 – Romasz

相關問題