【问题标题】:UWP Binding image from local folders来自本地文件夹的 UWP 绑定图像
【发布时间】:2018-07-16 11:22:08
【问题描述】:

我的应用程序使用相机拍摄照片并将其保存为 C:\Users...\Pictures\file.PNG。我有一个带有字符串“C:\Users\...\Pictures\file.PNG”的对象,并且绑定设置为该字符串,但它不加载图像。如果我将图像放置到 Assets 并将字符串设置为“Assets\file.png”,它就可以工作。是否可以在 Assets 之外绑定?

【问题讨论】:

  • 你使用什么样的 UI 控件来显示图像?
  • 您可以将Source直接绑定到一些BitmapImage,但我建议您首先仔细了解如何在UWP中处理文件以及相机如何工作(在我下面的答案中),然后您可以详细说明捆绑。我并不是说你想要做的事情是不可能的(SURE 是可能的),只是一步一步来。

标签: c# xaml uwp


【解决方案1】:

首先,您需要意识到您没有可以访问文件系统,例如在 Win32 应用程序中。因此,UWP 应用程序中的完整路径不再相关。请看一下这个link to explain it more 和nice one here。

我假设您使用Image 控件来显示图像。比如:

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Loaded="MainPage_OnLoaded">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Image Name="Img1" />
    </Grid>
</Page>

MainPage.xaml.cs

    private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        var file = await Windows.Storage.KnownFolders.CameraRoll.GetFileAsync("p1.png");
        using (var imgStream = await file.OpenStreamForReadAsync())
        {
            var bitmapImg = new BitmapImage();
            bitmapImg.SetSource(imgStream.AsRandomAccessStream());
            Img1.Height = bitmapImg.PixelHeight; //Need to take care of proper image-scaling here
            Img1.Width = bitmapImg.PixelWidth;  //Need to take care of proper image-scaling here
            Img1.Source = bitmapImg;
        }
    }

当您了解 UWP 中的文件访问概念文件后,您可以仔细查看内置的 camera control support。还有示例如何直接访问捕获的图像而无需使用文件名。

【讨论】:

    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 2020-06-27
    • 2019-02-14
    • 1970-01-01
    • 2023-03-28
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多