我已經找到了很多關于這個問題的答案,但無論我嘗試什么,似乎都不起作用。我正在嘗試在 UWP 應用程序中創建用戶控件。用戶控件由邊框組成,邊框內有圖像和文本塊。我設法讓TextBlock文本顯示出來,但無論我做什么,我似乎都無法獲得圖像顯示在UserControl中。到目前為止,事情已經嘗試過:將“圖形屬性類型”更改為“圖像”、“圖像源”和“Uri”。將邊框中的圖像放入內容預覽中。在這一點上,我不記得其他事情。我不知道如何讓它工作。我知道我以前做過這件事,但那是幾年前的事了,我顯然已經意識到了如何(或者UWP發生了重大變化,因為我的大部分經驗都是在WPF中)。有人可以幫忙找到我在哪里搞砸了嗎?用戶控制 XAML:<UserControl x:Class="ShirtSleeves.CardControlxaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:ShirtSleeves" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="CardControl" mc:Ignorable="d" d:DesignHeight="400" d:DesignWidth="300"> <Grid> <Border Margin="0,10" Background="White" BorderBrush="Black" BorderThickness="5" Width="260" Height="352" CornerRadius="20"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Image Source="{Binding ElementName=CardControl, Path=Graphic}" Height="200" Width="200" HorizontalAlignment="Center" VerticalAlignment="Bottom" /> <TextBlock Text="{Binding ElementName=CardControl, Path=Label}" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,30" Foreground="Black" FontSize="48" FontWeight="Bold" /> </Grid> </Border> </Grid></UserControl>
1 回答

HUX布斯
TA貢獻1876條經驗 獲得超6個贊
UWP 不允許直接訪問應用容器外部的文件。這意味著您無法像這樣設置圖像源。C:\Users\<username>\source\repos\ShirtSleeves\ShirtSleeves\Images\Rook (Games).png
在您的例子中,最簡單的方法是將圖像放入項目的 Assets 文件夾中,如下所示:
然后,您可以指定“圖形”屬性,如下所示:
<local:CardControlxaml Label="Search" Graphic="Assets/animals.jpg" Foreground="Black" />
有關詳細信息,請閱讀文件訪問權限。
- 1 回答
- 0 關注
- 85 瀏覽
添加回答
舉報
0/150
提交
取消