Save all images in ListView - windows-phone-8

I am loading a bunch of images from Vk.com in a ListView.I'd like to be able to download/save all images in any list to the phone's gallery, either in a simple event or by default. How would I go about this?
This is the XAML.
<Page.BottomAppBar>
<CommandBar>
<AppBarButton FontSize="15" FontFamily="Times New Roman" Icon="Back" Label="Albums" Click="AppBarButton_Click"/>
</CommandBar>
</Page.BottomAppBar>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"> </RowDefinition>
</Grid.RowDefinitions>
<TextBlock x:Name="MyTextBlock" FontSize="35" HorizontalAlignment="Center"></TextBlock>
<ListView Grid.Row="1"
ItemsSource="{Binding}"
SelectionMode="None"
IsItemClickEnabled="False"
>
<ListView.ItemTemplate>
<DataTemplate>
<Image Stretch="UniformToFill" Source="{Binding image_final}"
AutomationProperties.Name="{Binding Title}"
Width="Auto"
Height="Auto"
></Image>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>

You've tagged this as WP8 and WP8.1. The code below is for 8, however shouldn't take too much work for 8.1.
You can download and save an image to the users gallery like this:
using Microsoft.Xna.Framework.Media;
using System.Windows.Media.Imaging;
using System.IO;
WebClient client = new WebClient();
client.OpenReadCompleted += (s, ev) =>
{
var streamResourceInfo = new StreamResourceInfo(ev.Result, null);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(streamResourceInfo.Stream);
WriteableBitmap bmp = new WriteableBitmap(bitmap);
MediaLibrary library = new MediaLibrary();
using (MemoryStream stream = new MemoryStream())
{
Extensions.SaveJpeg(bmp, stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
library.SavePicture("imagename.jpg", stream.ToArray());
}
MessageBox.Show("The picture has been saved to your Pictures Hub", "Success!", MessageBoxButton.OK);
};
client.OpenReadAsync(new Uri("http://example.com/imageurl.jpg"));
Obviously put the correct image URL in the last line. And optionally replace "imagename.jpg" with the real name of the image.

This link really explains everything: saving images, and organizing them into folders. Definitely worth a read.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/769c9a6c-961c-47ea-bddf-a381db932289/saving-an-image-from-the-web-to-the-saved-pictures-folder-in-windows-phone-81-rt-c?forum=wpdevelop
Explanation:
//Create a storage folder in the Pictures Library:
StorageFolder AppFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("XYZ", CreationCollisionOption.OpenIfExists);
//Naming and storing the file:
var uri = imageuri; //image uri
var filename = Path.GetFileName(uri.LocalPath); //designates file name
var thumbnail = RandomAccessStreamReference.CreateFromUri(uri);
var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(filename, uri, thumbnail);
await remoteFile.CopyAsync(albumname, filename, NameCollisionOption.FailIfExists); //Fail if argument filename is a string by which a file is already saved.
I hope this helps.

Related

Search of user's contacts: highlight part of the name that matches with the search, when using Data Binding and LongListSelector (WP8)

I'm binding the user's contact list to a LongListSelector, as I saw in a Sample, this way:
<phone:PhoneApplicationPage.Resources>
...
<DataTemplate x:Key="AddrBookItemTemplate" >
<ListBoxItem>
<StackPanel>
<TextBlock Text="{Binding Path=DisplayName, Mode=OneWay}" FontFamily="Segoe WP" FontSize="25" />
<TextBlock Text="{Binding Path=PhoneNumbers[0].PhoneNumber, Mode=OneWay}" FontFamily="Segoe WP Light" FontSize="20" />
</StackPanel>
</ListBoxItem>
</DataTemplate>
...
</phone:PhoneApplicationPage.Resources>
...
<Grid x:Name="ContentPanel">
<phone:LongListSelector
x:Name="AddrBook"
JumpListStyle="{StaticResource AddrBookJumpListStyle}"
GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"
ItemTemplate="{StaticResource AddrBookItemTemplate}"
LayoutMode="List"
IsGroupingEnabled="true"
HideEmptyGroups ="true"
SelectionChanged="AddrBook_SelectionChanged"/>
</Grid>
And I'm doing the user's search for a specific name this way (using LINQ):
contactsList = contactsEnum.Where(m => m.PhoneNumbers.Count() > 0 && (m.DisplayName.Split(' ').ToList().Any(p => p.ToLower().StartsWith(tbxSearch.Text.ToLower())) || (m.PhoneNumbers.Any(y => y.PhoneNumber.StartsWith(tbxSearch.Text))))).ToList();
AddrBook.ItemsSource = contactsList; // With IsGroupingEnabled=false
I'm looking for a way to highlight the part of the name that matches with the search, but I can't do this, with the way I made the data binding...
Does anyone know how I could do the highlight?
You could do something like this, whenever the word you type in within the textbox matches with the word which could be within the sub strings or the Collection, you could simply highlight it!
string[] substrings = regex.Split(Content.Text);
Content.Inlines.Clear();
foreach (var item in substrings)
{
//if a word from the content matches the search-term
if (regex.Match(item).Success)
{
//create a 'Run' and add it to the TextBlock
Run run = new Run(item);
run.Foreground = Brushes.Red;
Content.Inlines.Add(run);
}
else //if no match, just add the text again
Content.Inlines.Add(item);
}
References: Is there a way I can highlight specific text in a textbox?
How to Highlight the result from context sensitive search in windows phone 7?

Display Tooltip on Tapping the pushpin(Custom marker) in Windows Phone 8 Map

In a Windows Phone map I am drawing my mapmarker and using them as pushpin.
MapLayer mapLayer = new MapLayer();
foreach (LocationDetail locationDetail in locationListobj)
{
MapOverlay overlay = new MapOverlay();
overlay.Content = GetImage(locationDetail);
overlay.GeoCoordinate = new GeoCoordinate(locationDetail.Latitude, locationDetail.Longitude);
overlay.PositionOrigin = new Point(0.0, 1.0);
mapLayer.Add(overlay);
}
AllyMap.Layers.Add(mapLayer);
A new requirement came up that upon tapping on the map we have to show the description in a infobar. Can anybody please help me how could I do that.
You need to set the ToolTipService.ToolTip property on the PushpinViewModel which should be a UIElement. ToolTips must be hooked up to a UIElement, which actually renders in your visual tree, to be triggered by mouse hover. Hovering is just a way of showing the tooltip.
<ToolTipService.ToolTip>
<ToolTip Style="{StaticResource ToolTipStyle}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
You could refer this for more:
http://social.technet.microsoft.com/wiki/contents/articles/24118.windows-phone-8-maps-api-making-custom-pushpin-with-custom-tooltip-c-xaml.aspx
Hope it helps!

ApplicationBar not showing up in Windows Phone HTML Template

I have created an application bar in code behind in a new Windows Phone HTML template. Originally the application bar was in xaml but I removed it. I created my application bar like I normally do in the code behind, although in this template for some reason it will not show up. I cannot figure out what the issue is, I have no errors. My code is below. The only thing I did in the XAML of the template is add a pivot control and I made the browser visibility false. My code is below. Any ideas?
XAML
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<phone:Pivot>
<phone:Pivot.TitleTemplate>
<DataTemplate>
<TextBlock Text="APPLICATION NAME">
</DataTemplate>
</phone:Pivot.TitleTemplate>
<phone:PivotItem Header="one">
</phone:PivotItem>
<phone:PivotItem Header="two">
</phone:PivotItem>
</phone:Pivot>
<phone:WebBrowser x:Name="Browser" Visibility="Collapsed"
IsScriptEnabled="True"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Loaded="Browser_Loaded"
Navigated="Browser_Navigated"
NavigationFailed="Browser_NavigationFailed"
ScriptNotify="Browser_ScriptNotify"/>
<ProgressBar x:Name="PerformanceProgressbar"
VerticalAlignment="Top"
IsIndeterminate="False"
Visibility="Collapsed">
</Grid>
XAML.CS
public MainPage()
{
InitializeComponent();
BuildLocalizedApplicationBar();
}
private void BuildLocalizedApplicationBar()
{
ApplicationBar appbar = new ApplicationBar();
/appbar.Mode = ApplicationBarMode.Minimized;
ApplicationBarMenuItem settings = new ApplicationBarMenuItem();
settings.Text = AppResources.MainPage_Settings;
settings.Click += settings_Click;
appbar.MenuItems.Add(settings);
}
You are just creating an object ApplicationBar, but you are not adding it to your Page. Page has a property ApplicationBar which you should set with your created instance of class ApplicationBar.
What would work:
private void BuildLocalizedApplicationBar()
{
ApplicationBar appbar = new ApplicationBar();
/appbar.Mode = ApplicationBarMode.Minimized;
ApplicationBarMenuItem settings = new ApplicationBarMenuItem();
settings.Text = AppResources.MainPage_Settings;
settings.Click += settings_Click;
appbar.MenuItems.Add(settings);
this.ApplicationBar = appbar; // or simply ApplicationBar = appbar;
// you can also add Appbar directly = ApplicationBar = new ApplicationBar();
// and then modify via this property
}
Note that you can have many ApplicationBars (objects) and exchange them easily.
Use this code would work:
click here for more details
private void BuildLocalizedApplicationBar()
{
ApplicationBar appbar = new ApplicationBar();
/appbar.Mode = ApplicationBarMode.Minimized;
ApplicationBarMenuItem settings = new ApplicationBarMenuItem();
settings.Text = AppResources.MainPage_Settings;
ApplicationBar.MenuItems.Add(settings );
settings.Click += new EventHandler(settings_Click);
}

Tile and longlistselector itemTemplate

I would like to to create an itemtemplate of longlistselector using a tile
is it possible?
how to do it in xaml?
could someone show me some code to do it?
Using a longlistselector is very easy for this tile applications.
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="TileDataTemplate">
<Grid Background="{StaticResource PhoneAccentBrush}"
Margin="0,0,12,12">
<StackPanel VerticalAlignment="Top">
<TextBlock Text="{Binding Content}" TextWrapping="Wrap"/>
</StackPanel>
<StackPanel VerticalAlignment="Bottom">
<TextBlock Text="{Binding Title}" Margin="6,0,0,6"/>
</StackPanel>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<phone:LongListSelector Margin="0,0,-12,0"
SelectionChanged="LongListSelector_SelectionChanged"
LayoutMode="Grid"
GridCellSize="150,150"
ItemsSource="{Binding DataBindingItems}"
ItemTemplate="{StaticResource TileDataTemplate}"
/>
here the most important thing note is the LayoutMode="Grid" GridCellSize="150,150" properties.
Edit:::
I have added the code for the tile where i would show up a tile with two textblocks showing up some content on top and then title in the end. The way you have to wire up some sample data or dynamic depends on code behind. Ping me if that is needed. And this is really basic stuff. Accept if you get the answer.
// find the tile object for the application tile that using "flip" contains string in it.
ShellTile oTile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("flip".ToString()));
if (oTile != null && oTile.NavigationUri.ToString().Contains("flip"))
{
FlipTileData oFliptile = new FlipTileData();
oFliptile.Title = "Hello WP8!!";
oFliptile.Count = 11;
oFliptile.BackTitle = "Updated Flip Tile";
oFliptile.BackContent = "back of tile";
oFliptile.WideBackContent = "back of the wide tile";
oFliptile.SmallBackgroundImage = new Uri("Assets/Tiles/Flip/159x159.png", UriKind.Relative);
oFliptile.BackgroundImage = new Uri("Assets/Tiles/Flip/336x336.png", UriKind.Relative);
oFliptile.WideBackgroundImage = new Uri("Assets/Tiles/Flip/691x336.png", UriKind.Relative);
oFliptile.BackBackgroundImage = new Uri("/Assets/Tiles/Flip/A336.png", UriKind.Relative);
oFliptile.WideBackBackgroundImage = new Uri("/Assets/Tiles/Flip/A691.png", UriKind.Relative);
oTile.Update(oFliptile);
MessageBox.Show("Flip Tile Data successfully update.");
}
else
{
// once it is created flip tile
Uri tileUri = new Uri("/MainPage.xaml?tile=flip", UriKind.Relative);
ShellTileData tileData = this.CreateFlipTileData();
ShellTile.Create(tileUri, tileData, true);
}

How to show list of items on button click?

I am developing windows phone app..
I am taking 1 button and on click of that button the dropdown list of clothes should come..
how to code for this?
Problem: How to take this list on button click
Your xaml should be :
<Button x:Name="BtnShowCloth" Content="showCloth" Margin="10,5" Click="BtnAddProduct_Click"/>
<toolkit:ListPicker ExpansionMode="FullScreenOnly" x:Name="ClothListPicker" Visibilty="Collapsed" Margin="0,0,0,10" Height="100" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource AppTextBlockStyle}" Text="{Binding}"/>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource AppTextBlockStyle}" Text="{Binding}" Margin="2,10,0,0" FontSize="31"/>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
//Another way use ListBox Control
<ListBox Grid.Row="1" Name="ListBoxCloth" Height="50">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource AppTextBlockStyle}" Text="{Binding}" Margin="2,10,0,0" FontSize="31"/>
</DataTemplate>
<ListBox.ItemTemplate>
</ListBox>
Code Behind of button click fill list of cloth names. In windows phone no dropdown list exist, you should use longlistselector instead of dropdown
private void BtnAddProduct_Click(object sender, RoutedEventArgs e)
{
List<string> clothNameList = new List<string>();
clothNameList .Add("a");
clothNameList .Add("b");
clothNameList .Add("c");
clothNameList .Add("d");
clothNameList .Add("e");
//ClothListPicker.ItemsSource = clothNameList;
// ClothListPicker.Visibility = Visibility.visible;
ListBoxCloth.ItemsSource = clothNameList;
ListBoxCloth.Height = 400;
}
This is very broad question, infact does not fall in SO standards of Questions.
You should research and try something and ask only if you got stuck somewhere or if your method fails.
And to your question, it needs clarification on many things like
What is the format of your clothes data
Are you getting it from a web service or a local db or some other resource
Is it a single List or a groups of items etc
Hence, it becomes hard to answer. Please keep these things in mind, next time when asking questions.
Based on your comment:
You should split your task into multiple small tasks
First thing, learn how to fetch the data from your db
Converting that data into List of objects
Creating your UI for the data
Bind the data to the UI in your Button_Click event handler
Happy coding !!