来自networkshare的UWP XAML ImageBrush.imageSource

时间:2017-11-23 10:38:55

标签: image xaml uwp

我编辑了我的问题:

我有一个带有数组按钮的堆栈面板。 现在我想从networkshare图像设置按钮背景。

这是我的源代码:

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);


        }
    }

由于

1 个答案:

答案 0 :(得分:3)

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

<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;
}
我为此https://github.com/shubdragon/LoadNetworkImageRepo

创建了

Github示例

需要注意的要点

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

2)必须将该位置分享到家庭组。

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