2017-02-15 70 views
0

我創建了一個包含靜態圖像的UserControl,並且我想在我的MainPage上顯示它。但是,該圖像只顯示在我的UserControl上而不顯示在MainPage上。UserControl的圖像不顯示在MainPage上

這是我的用戶XAML:

<UserControl 
    x:Class="Example.Views.UserControls.Gift.GiftView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:Example.Views.UserControls.Gift" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid x:Name="RootLayout"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="90"/> 
     </Grid.RowDefinitions> 

     <Border Grid.Row="1" Background="#dedede"> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
       <Grid HorizontalAlignment="Stretch"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <Button Grid.Column="0" HorizontalAlignment="Stretch"> 
         <Button.Template> 
          <ControlTemplate > 
           <Grid Background="{Binding FleetTabColor}"> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="*"></RowDefinition> 
             <RowDefinition Height="Auto"></RowDefinition> 
            </Grid.RowDefinitions> 
            <Image Height="40" Width="40" Source="../Assets/Icon/unselect_gift.png" VerticalAlignment="Center" /> 
            <TextBlock Foreground="Black" Margin="0" Grid.Row="1" FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Gift"/> 
           </Grid> 
          </ControlTemplate> 
         </Button.Template> 
        </Button> 
        <Button Grid.Column="1" HorizontalAlignment="Stretch" > 
         <Button.Template> 
          <ControlTemplate > 
           <Grid Background="{Binding FleetTabColor}"> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="*"></RowDefinition> 
             <RowDefinition Height="Auto"></RowDefinition> 
            </Grid.RowDefinitions> 
            <Image Height="40" Width="40" Source="../Assets/birthday.png" VerticalAlignment="Center" /> 
            <TextBlock Foreground="Black" Margin="0" Grid.Row="1" FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center" Text="BirthDay"/> 
           </Grid> 
          </ControlTemplate> 
         </Button.Template> 
        </Button> 
        <Button Grid.Column="2" HorizontalAlignment="Stretch"> 
         <Button.Template> 
          <ControlTemplate > 
           <Grid Background="{Binding FleetTabColor}"> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="*"></RowDefinition> 
             <RowDefinition Height="Auto"></RowDefinition> 
            </Grid.RowDefinitions> 
            <Image Height="40" Width="40" Source="../Assets/package.png" VerticalAlignment="Center" /> 
            <TextBlock Foreground="Black" Margin="0" Grid.Row="1" FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Package"/> 
           </Grid> 
          </ControlTemplate> 
         </Button.Template> 
        </Button> 
       </Grid> 
      </StackPanel> 
     </Border> 

    </Grid> 
</UserControl> 

這是我的XAML的MainPage:

<Page 
    x:Class="Example.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:Example" 
    xmlns:menu="using:Example.Views.UserControls.Menu" 
    xmlns:popup="using:Example.Views.UserControls.Popup" 
    xmlns:giftview="using:Example.Views.UserControls.Gift" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

    <Grid x:Name="Root">   
     <Grid Background="#FFCFD4E2"> 
      <Grid Visibility="Visible"> 
       <giftview:GiftView x:Name="GiftViewTest" DataContext="{Binding GiftViewModel}"/> 
      </Grid> 
     </Grid> 

    </Grid> 


</Page> 

當我試圖清除我的項目,圖像會正確顯示在我的預覽的MainPage。但是當我重建我的項目時,它會再次在我的預覽中消失。

我該怎麼做?

+0

的'資料來源圖像上的財產看起來很可疑。你在運行時遇到任何錯誤嗎? –

+0

@ByronRoss我只收到一條消息「跳過加載符號,模塊已經優化,調試器選項'Just My Code'已啓用。」在調試控制檯上。 – user259429

+0

嘗試一個絕對路徑,並確保您正在建立調試,而不是發佈模式 –

回答

0

幾乎可以肯定你的圖像在運行時無法找到。

您需要檢查項目輸出以確保資產位於正確的文件夾中。

很有可能,你應該使用的資源來接入圖片更多:看this other stackoverflow question

實例用戶控制如預期那樣工作:

<UserControl x:Class="WpfApp1.UserControl1" 
      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" 
      mc:Ignorable="d"> 
    <StackPanel Orientation="Vertical"> 
     <Button> 
      <Button.Template> 
       <ControlTemplate> 
        <Grid> 
         <Image Height="20" Width="40" Source="http://lorempixel.com/400/200/" /> 
        </Grid> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 
     <Button> 
      <Button.Template> 
       <ControlTemplate> 
        <Grid> 
         <Image Height="20" Width="40" Source="sample-image.jpg" /> 
        </Grid> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 
    </StackPanel> 
</UserControl> 

項目:

enter image description here