我在尝试在 UWP ContentDialog 中打开图像并允许用户使用 InkCanvas 在其上绘图时遇到问题。问题是图像没有出现在对话框中,我正在努力找出根本原因。即使我为图像设置了固定来源,它仍然不显示
这是我的xaml
<ContentDialog
x:Class="eseua.Views.ImageEditorDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:eseua.Helpers"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="{helpers:ResourceString Name=ExportHistoryNoteDialog_Header}"
Width="600"
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
CornerRadius="{StaticResource OverlayCornerRadius}"
PrimaryButtonStyle="{StaticResource DefaultButtonStyle}"
PrimaryButtonText="{helpers:ResourceString Name=ExportHistoryNoteDialog_PrimaryButtonText}"
SecondaryButtonStyle="{StaticResource ThemeAwareContentDialogSecondaryButtonStyle}"
SecondaryButtonText="{helpers:ResourceString Name=Edit_Note_Discard_Changes_Discard}"
mc:Ignorable="d">
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Vertical">
<Grid
x:Name="img_grid"
Width="600"
Height="500">
<Image
x:Name="img"
Width="600"
Height="400"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ManipulationMode="All"
Source="{x:Bind ViewModel.FilePath, Mode=OneWay}"
Stretch="Fill" />
<InkCanvas x:Name="img_inkcanvas" Margin="0,20,0,0" />
<InkToolbar
x:Name="img_inktoolbar"
Margin="0,20,0,0"
VerticalAlignment="Top"
TargetInkCanvas="{x:Bind img_inkcanvas}" />
<Button
x:Name="IMG_C"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="Close" />
<Button
x:Name="Img_draw"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="Draw" />
</Grid>
</StackPanel>
</ContentDialog>
这是我背后的代码
internal ImageEditorDialogViewModel ViewModel => DataContext as ImageEditorDialogViewModel;
private BitmapImage _imageSource;
public ImageEditorDialog()
{
this.InitializeComponent();
var themeBinding = new Binding { Source = ThemeAwareHelper.Instance, Path = new PropertyPath("Theme") };
SetBinding(RequestedThemeProperty, themeBinding);
img_inkcanvas.InkPresenter.InputDeviceTypes =
Windows.UI.Core.CoreInputDeviceTypes.Mouse |
Windows.UI.Core.CoreInputDeviceTypes.Pen;
}
这是输出:
我尝试在 XAML 中为图像控件设置固定源。
我不确定源是否应该采用什么格式,所以我尝试添加图像路径 像这样 C:\Users\Mariam Khaled\Downloadsx1342504893.jpeg 和 C:/用户/玛丽亚姆·哈立德/下载/1701342504893.jpeg
我还尝试在代码隐藏中使用 BitmapImage 以编程方式设置图像源。
ImageSource image_source= new BitmapImage(new Uri(FilePath));
根据文档文件访问权限
默认情况下,您的应用程序只能访问用户的文件和文件夹 下载您的应用程序创建的文件夹。
UWP运行在沙箱中,无法直接访问外部资源。建议将需要的图片添加到项目中,并使用URI(例如“Assets/StoreLogo.png”)设置
ViewModel.FilePath
,而不是绝对路径。