gpt4 book ai didi

c# - 尝试在 WinRT 中使用绑定(bind)替换文件时访问被拒绝

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:43 25 4
gpt4 key购买 nike

我有一个 WinRT 应用程序,它可以拍摄照片并将它们显示在图像中。第一张照片正确拍摄并显示图像。但是,设置为覆盖 原始图片的后续图片会抛出带有ACCESSDENIEDUnauthorizedAccessException。我还将图像源绑定(bind)到学生的 Uri。我也肯定绑定(bind)是导致问题的原因,因为 WinRT 不喜欢我替换当前正在使用的文件。我已经尝试在文件替换等之前将源设置为 null,但这不起作用。处理这个问题的优雅方法是什么?另请注意,我也在该文件之前的页面上绑定(bind)了该文件,并且我还需要删除该页面上的绑定(bind)以避免错误。

private async void btnTakePic_Click(object sender, RoutedEventArgs e)
{
await CameraCapture();
}

private async Task CameraCapture()
{
CameraCaptureUI camUI = new CameraCaptureUI();

camUI.PhotoSettings.AllowCropping = true;
camUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.MediumXga;
camUI.PhotoSettings.CroppedAspectRatio = new Size(4, 3);

Windows.Storage.StorageFile imageFile = await camUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (imageFile != null)
{

IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read);
BitmapImage bitmapCamera = new BitmapImage();
bitmapCamera.SetSource(stream);

// Use unique id for image name since name could change

string filename = student.StudentID + "Photo.jpg";

await imageFile.MoveAsync(ApplicationData.Current.LocalFolder, filename, NameCollisionOption.ReplaceExisting);

student.UriPhoto = new Uri(string.Format("ms-appdata:///local/{0}", filename), UriKind.Absolute);

}
}

这里是绑定(bind)部分,只是为了好玩:

<Image x:Name="imgStudent" Grid.Row="0" Width="400" Height="300" Margin="15" Grid.ColumnSpan="2">
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="{Binding UriPhoto}" />
</Image.Source>
</Image>

最佳答案

好的,经过几个小时的研究,我得出结论,绑定(bind)到 Uri 几乎是不可能的,替代方法是绑定(bind) BitmapImage。我找不到确切的原因,但它与以下事实有关:使用 Uri 会使 BitmapImage 打开并且绑定(bind)会很快打破这种范式。解决方案是绑定(bind)BitmapImage 并将BitmapImage 设置为流,这似乎很好地支持绑定(bind)。

我喜欢 Uri 的想法,因为它很容易序列化,而 BitmapImage 更难并且占用更多空间(因为您将图片数据序列化为反对链接)。我决定采用的有效解决方案是序列化 Uri,并使用 OnDeserialized 属性在启动时创建 BitmapImage。然后我需要一个方法/事件来在 Uri 更改时重置 BitmapImage

这里是要回顾的最终代码:

private async Task CameraCapture()
{
CameraCaptureUI camUI = new CameraCaptureUI();

camUI.PhotoSettings.AllowCropping = true;
camUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.MediumXga;
camUI.PhotoSettings.CroppedAspectRatio = new Size(4, 3);

Windows.Storage.StorageFile imageFile = await camUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

if (imageFile != null)
{
// Use unique id for image name since name could change

string filename = student.StudentID + "Photo.jpg";

// Move photo to Local Storate and overwrite existing file

await imageFile.MoveAsync(ApplicationData.Current.LocalFolder, filename, NameCollisionOption.ReplaceExisting);

// Open file stream of photo

IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read);
BitmapImage bitmapCamera = new BitmapImage();
bitmapCamera.SetSource(stream);

student.BitmapPhoto = bitmapCamera // BitmapImage

// Save Uri to photo since we can serialize this and re-create BitmapPhoto on startup/deserialization

student.UriPhoto = new Uri(string.Format("ms-appdata:///local/{0}", filename), UriKind.Absolute);

}
}

以及绑定(bind)到学生 BitmapImage 的新 XAML

<Image x:Name="imgStudent" Source="{Binding BitmapPhoto}" Grid.Row="0" Width="400" Height="300" Margin="15" Grid.ColumnSpan="2"/>

关于c# - 尝试在 WinRT 中使用绑定(bind)替换文件时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com