image - 来自网络共享的 UWP XAML ImageBrush.imageSource

标签 image xaml uwp

我编辑了我的问题:

我有一个堆栈面板,其中包含数组中的按钮。 现在我想从网络共享图像设置按钮背景。

这是我的源代码:

XAML:

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

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,0">
    <StackPanel x:Name="sp" HorizontalAlignment="Left" Height="1070" Margin="10,10,0,0" VerticalAlignment="Top" Width="145" Padding="0" CornerRadius="10" RequestedTheme="Light" ScrollViewer.HorizontalScrollBarVisibility="Visible" Grid.RowSpan="2">
        <FlyoutBase.AttachedFlyout>
            <MenuFlyout/>
        </FlyoutBase.AttachedFlyout>
    </StackPanel>
</Grid>

后台代码:

private void onLoad()
    {


        for (int i = 0; i < imgNames.Length; ++i)
        {
            ImageBrush brush1 = new ImageBrush();
            brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///assets/" + imgNames[i]));
            Button button = new Button()
            {
                Content = string.Format(""),
            Tag = i
            };
            button.Width = 100;
            button.Height = 100;
            button.Background = brush1;
            button.Margin = new Thickness(0, 20, 0, 0);


            button.Click += new RoutedEventHandler(button_Click);   

            this.sp.Children.Add(button);


        }
    }

谢谢

最佳答案

要从网络源加载图像,您需要在应用程序中使用这些功能

<Capabilities>
  <Capability Name="internetClient" />
  <Capability Name="privateNetworkClientServer" />
  <Capability Name="internetClientServer" />
  <uap:Capability Name="enterpriseAuthentication" />
</Capabilities>

和你的图片

<Image Name="YourImageElementName" />

并在后面的代码中加载图像

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"\\Your Image Full Path e.g user\folder\subfolder");
StorageFile file = await folder.GetFileAsync("ImageName.jpg");
using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
    BitmapImage bitmap = new BitmapImage();
    bitmap.SetSource(stream);
    YourImageElementName.Source = bitmap;
}

Github 示例 我为此创建了 https://github.com/shubdragon/LoadNetworkImageRepo

需要注意的地方

1) 您需要在代码和图像名称中设置您的网络位置以及所需的扩展名。

2) 必须将该位置共享给家庭组。

3) 注意不同页面和 Package.appxmanifes 中隐藏的代码(在 xml 编辑器中将其视为代码)

关于image - 来自网络共享的 UWP XAML ImageBrush.imageSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453463/

相关文章:

javascript - SetVisibleRect 使图像困惑(gwt)

c# - System.Core.ni.dll 中发生类型 'System.ArgumentOutOfRangeException' 的异常,但未在用户代码中处理

c# - 如何从 UWP map 上的 MapIcon 中删除点和尾部

sqlite - 如何在UWP应用程序中的应用程序文件夹之外访问文件(如SQLite数据库)?

performance - 人眼能否感知图像加载时间的 10 毫秒延迟

html - 使用CSS将图像保持在中间

html - 当窗口变小时保持图像比例相同

wpf - 扩展器标题文本对齐

c# - 加载具有条件字符串格式数据绑定(bind)的 XAML 失败

c# - 在 C# 中从后端发送 UWP 原始通知