Windows Phone 8.1:将远程图像添加到BottomAppBar或AppBarButton

时间:2015-06-06 12:20:43

标签: c# xaml windows-phone-8 windows-phone-8.1 windows-phone-7.1

我想将远程图像添加到应用栏按钮。 我的图片是此网址http://2.bp.blogspot.com/-yEbb9_qp-jg/U8-KGeZAy3I/AAAAAAAAB0U/m91Bv1jPAQI/s1600/india+win+at+lords.jpg 我想从后端绑定到appbarbutton。我的后端代码是propic.UriSource = new Uri("http://2.bp.blogspot.com/-yEbb9_qp-jg/U8-KGeZAy3I/AAAAAAAAB0U/m91Bv1jPAQI/s1600/india+win+at+lords.jpg"); 但是我的后端代码无效......有人请告诉我该怎么做

以下是我的Xaml代码                   

            <AppBarButton Label="Ride Now">
                <AppBarButton.Icon>

                    <BitmapIcon x:Name="propic" Height="100" Width="100"/>
                </AppBarButton.Icon>
            </AppBarButton>
            <CommandBar.SecondaryCommands>
                <AppBarButton Label="Ride Now">
                <AppBarButton.Icon>

                    <BitmapIcon x:Name="propic12" Height="100" Width="100"/>
                </AppBarButton.Icon>   
                </AppBarButton>
                <AppBarButton Label="x" Icon="Admin" />
                </CommandBar.SecondaryCommands>
        </CommandBar>
    </Page.BottomAppBar>

1 个答案:

答案 0 :(得分:0)

你不能给AppBar图像提供绝对的Uri,AppBarButton只是不这样做。

可能有效的是您可以下载图像,将其保存到本地存储空间并从那里读取图像。不要忘记您的图像需要遵循尺寸格式。

您可以在此处获得更多其他信息

https://msdn.microsoft.com/en-us/library/windows/apps/ff431806%28v=vs.105%29.aspx

以下是可以帮助您的代码。

using (var htc = new HttpClient())
{
    var file = await htc.GetStreamAsync("Your image url");
    if (!isoStore.FileExists("shared/shellcontent/appbarimage.png"))
    {
        using (IsolatedStorageFileStream fileStream = isoStore.OpenFile("shared/shellcontent/appbarimage.png", FileMode.Create))
        {
            await file.CopyToAsync(fileStream);
        }
    }
}

propic.UriSource = new Uri("isostore:/Shared/ShellContent/appbarimage.png",UriKind.Relative);