I Want to Display the Images Dynamically.i'e If Whenever Click On Particular image some more (4 to 5 times)times that can be disappear and new image can be fill this place.in that i want to display the images dynamically in windows phone 7 using silverlight.
I know this is a very old question but I had a couple of free minutes ;)
The following will display a different random image from the images stored on the device every fourth time the screen is tapped.
XAML:
xmlns:Controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Background>
<ImageBrush x:Name="myImg" />
</Grid.Background>
<Controls:GestureService.GestureListener>
<Controls:GestureListener Tap="GestureListener_Tap" />
</Controls:GestureService.GestureListener>
</Grid>
C#
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;
private int tapCount = 0;
private void GestureListener_Tap(object sender, GestureEventArgs e)
{
tapCount += 1;
if (tapCount % 4 == 0)
{
SetRandomImage();
}
}
private void SetRandomImage()
{
var lib = new MediaLibrary();
using (var pic = lib.Pictures[new Random().Next(0, lib.Pictures.Count - 1)])
{
var img = new BitmapImage();
img.SetSource(pic.GetImage());
myImg.ImageSource = img;
}
}
Related
I use ZXing.Net.Mobile ZXingScannerView in a Xamarin.Forms application. I test it with Android API 21+ and Windows Mobile 10. While in Android the camera stream expands to the boundaries of the scanner view perfectly, this is not true for UWP. No matter that the emulator size or resolution. I"ve also tried with a real windows phone device and the issue still appears.
I place the ZXingScannerView in a content page similar to this:
<ContentPage.Content>
<StackLayout Spacing="20" Padding="15">
...
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<zxing:ZXingScannerView
x:Name="zxing"
Result="{Binding ScannerResult, Mode=TwoWay}"
IsScanning="{Binding ScannerScanning}"
IsVisible="{Binding ScannerVisible}"
IsAnalyzing="{Binding ScannerAnalyzing}"
ScanResultCommand="{Binding ScannedCommand}"/>
<v:MainPageZXingOverlayView />
</Grid>
...
</StackLayout>
</ContentPage.Content>
Also I've noticed that the view .xaml has a capture element set to Stretch="Fill"
<CaptureElement
Grid.Row="0"
Grid.Column="0"
x:Name="captureElement"
Stretch="Fill" />
also in view .cs
// *after* the preview is setup, set this so that the UI layout happens
// otherwise the preview gets stuck in a funny place on screen
captureElement.Stretch = Stretch.UniformToFill;
Anyone experiencing the same problem or have a fix for it?
While in Android the camera stream expands to the boundaries of the scanner view perfectly, this is not true for UWP. No matter that the emulator size or resolution. I"ve also tried with a real windows phone device and the issue still appears.
The preview resolution was set based on camera resolution when the camera start to scan.
For your requirement, you could expand preview to the boundaries via custom camera resolution. Please refer to the following code :
private async void Button_Clicked(object sender, EventArgs e)
{
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};
var scanner = new ZXing.Mobile.MobileBarcodeScanner();
scanner.AutoFocus();
var result = await scanner.Scan(options);
if (result != null)
System.Diagnostics.Debug.WriteLine("Scanned Barcode: " + result.Text);
}
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
//Don't know if this will ever be null or empty
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution() { Width = 800, Height = 600 };
//Debugging revealed that the last element in the list
//expresses the highest resolution. This could probably be more thorough.
return availableResolutions[availableResolutions.Count - 1];
}
Update
Note that i'm using the ZXingScannerView itself, but not the built-in wrappers. I've updated my question to be more clear.
If you use ZXingScannerView, you could also modify ZXingScannerView Options in the code behind.
public MainPage()
{
InitializeComponent();
var options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
CameraResolutionSelector = HandleCameraResolutionSelectorDelegate
};
zxing.Options = options;
}
CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
{
//Don't know if this will ever be null or empty
if (availableResolutions == null || availableResolutions.Count < 1)
return new CameraResolution() { Width = 800, Height = 600 };
//Debugging revealed that the last element in the list
//expresses the highest resolution. This could probably be more thorough.
return availableResolutions[availableResolutions.Count - 1];
}
i want to create a greeting card maker app for WP7, when a user double taps an image from a listbox, I want that selected image to fill a rectangle on the same page.
Im passing 50 images into the list box like this:
public GCM()
{
InitializeComponent();
var articles = new List<Article>();
for (byte i = 1; i <= 20; i++)
{
Article article = new Article() { Name = "name"+i, ImagePath = "Assets/Images/Backgrounds/"+i+".jpg" };
articles.Add(article);
}
listBox1.DataContext = articles;
}
and its working fine, now heres an xml snippet:
<Rectangle Fill="#FFF4F4F5" Margin="28,24,30,148" Stroke="Black" Name="rect1" />
................(more code here).........................
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<Image Name="bgs" Source="{Binding ImagePath}" Height="90" Width="90" DoubleTap="Load_BG" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
how can i fill the rectangle with the tapped image? this code (below) sets the string null everytime, no matter which image i select, although to my knowledge each has a different name and it should give different names for different images. I will use the name of the image to fill the rectangle. What am i doing wrong?
private void Load_BG(object sender, System.Windows.Input.GestureEventArgs e)
{
string abc = sender.GetType().Name;
}
Please excuse me if the solution is obvious..this is my first app ever. Thank you!
sender parameter should contains the control that triggered the event, in this case Image control. Try to cast it to Image type, then you can get the information you needed from Source property :
private void Load_BG(object sender, System.Windows.Input.GestureEventArgs e)
{
Image img = (Image)sender;
//do something with img.Source here
}
so heres the solution, it works now :)
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var img = listBox1.SelectedItem as Article;
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(img.ImagePath, UriKind.RelativeOrAbsolute));
rect1.Fill = imgBrush;
}
I am loading a large number of images, say 250+ and getting this Out of memory exception.
My Code:
while (kount < imageItems.Count)
{
for (int i = 0; i < _grid.RowDefinitions.Count; i++)
{
BitmapImage bit=null;
for (int j = 0; j < _grid.ColumnDefinitions.Count; j++)
{
imgGrd = new Image();
bit = new BitmapImage(new Uri(imageItems[kount].thumb_attachment, UriKind.RelativeOrAbsolute));
imgGrd.Source = bit;
imgGrd.Stretch = Stretch.UniformToFill;
_grid.Children.Add(imgGrd);
Grid.SetRow(imgGrd, i);
Grid.SetColumn(imgGrd, j);
//bit = null;
//imgGrd.Source = null;
kount++;
}
}
}
How to overcome this issue. thanks in advance..
See http://blogs.msdn.com/b/swick/archive/2011/04/07/image-tips-for-windows-phone-7.aspx for details on forcibly releasing memory used by images.
You should not create all of your images at once. The phone has ways to create and dispose of images for you. this is done by using some of the built in ItemsControl controls. The most popular of these is the ListBox. In order to let the ListBox create and dispose of the items you'll need to create a DataTemplate that will create the image.
<ListBox ItemsSource="{Binding ImageItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding thumb_attachment}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Instead of looping through your ImageItems and creating Images manually, you allow the phone to take care of this. This requires you to create an object to bind your page to that has an ImageItems property.
public class MainViewModel // Should probably implement INotifyPropertyChanged
{
public IEnumerable<ImageItem> ImageItems { get; set; }
}
With this your page would have it's DataContext set to be MainViewModel.
If you want to display the items in a grid then you can change the ItemsPanelTemplate of the ListBox to be the WrapPanel from the Windows Phone Toolkit.
<ListBox.ItemsPanelTemplate>
<toolkit:WrapPanel />
</ListBox.ItemsPanelTemplate>
I have added 10 images in a stackpanel horizontally which is inside a scrollviewer. When user swipe the page ,the scrollviewer stops at certain position, if the scroll stops in the middle of 2 images like the first image shown below i want to set the image with number 3 to be automatically scroll and fit with the left side of the screen like in the second image
for (int i = 0; i <= 9; i++)
{
Uri uri = new Uri("http://d1mu9ule1cy7bp.cloudfront.net//catalogues/47/pages/p_" + i + "/thump.jpg");
ImageSource img1 = new BitmapImage(uri);
Image rect = new Image { RenderTransform = new TranslateTransform() };
rect.Source = img1;
stack.Children.Add(rect);
}
XAML:
<Grid x:Name="LayoutRoot" Width="480" Background="Transparent" Margin="0,-33,0,0" Height="800">
<ScrollViewer HorizontalContentAlignment="Left" HorizontalAlignment="Left" Name="scroll" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Visible">
<StackPanel Name="stack" Width="Auto" Orientation="Horizontal" HorizontalAlignment="Left" >
</StackPanel>
</ScrollViewer>
</Grid>
The first thing you need to do is detect which item is overlapping the side of the screen. To do this, iterate over each item within the StackPanel and determine their location relative to some other element that has a fixed location on screen.
To do this, I use the following extension method:
/// <summary>
/// Gets the relative position of the given UIElement to this.
/// </summary>
public static Point GetRelativePosition(this UIElement element, UIElement other)
{
return element.TransformToVisual(other)
.Transform(new Point(0, 0));
}
i.e. for each item call the following;
Point position = stackPanelItem.GetRelativePosition(someFixedElement);
Using the location of each item, you should be able to work out which one overlaps the screen.
You then need to calculate by how much you need to scroll in order to ensure that your item is fully visible, then use ScrollViewer.ScrollToVerticalOffset to scroll to that location.
This probably isn't the nicest solution and I am sure there is a better way to achieve this but you could use the following :-
XAML :-
<ListBox x:Name="MyListBox"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Visible">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
C# :-
DispatcherTimer myTimer = new DispatcherTimer();
// Constructor
public MainPage()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
MyListBox.Items.Add(new Button()
{
Content = i.ToString(),
Width = 200,
Height = 100,
});
MyListBox.MouseMove += new MouseEventHandler(MyListBox_MouseMove);
}
myTimer.Interval = TimeSpan.FromSeconds(1);
myTimer.Tick += new EventHandler(myTimer_Tick);
}
private void myTimer_Tick(object sender, EventArgs e)
{
myTimer.Stop();
SnapFirstItem();
}
private void MyListBox_MouseMove(object sender, MouseEventArgs e)
{
myTimer.Stop();
myTimer.Start();
}
private void SnapFirstItem()
{
foreach (Button currentButton in MyListBox.Items)
{
bool visible = MyListBox.TestVisibility(currentButton, System.Windows.Controls.Orientation.Horizontal, true);
if (visible)
{
MyListBox.ScrollIntoView(currentButton);
break;
}
}
}
The TestVisibility extension method is from the following :-
http://blogs.msdn.com/b/ptorr/archive/2010/10/12/procrastination-ftw-lazylistbox-should-improve-your-scrolling-performance-and-responsiveness.aspx
i want to play video in my app. I see around but nothing helped me. I ask the user if he wants to listen to his phone music. If clicks ok the music continue playing, if not then the music stop. That is ok from now. Now, my problem is: I create a grid to use it like popup with width and height and so on.. When this popup appears the music stops. This is why can not certify my app in marketplace.
Here is a little code: i believe is easy to understand... Please help!
public void new_grid(int v)
{
Grid gr = new Grid();
gr.Background = new SolidColorBrush(Colors.Cyan);
gr.Opacity = 1.0;
gr.Width = 400;
gr.Height = 600;
// Create a white border.
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.White);
border.BorderThickness = new Thickness(7.0);
MediaElement video_ship = new MediaElement();
//video_ship.AutoPlay = false;
video_ship.Width = 400;
video_ship.Height = 600;
video_ship.VerticalAlignment = VerticalAlignment.Top;
video_ship.HorizontalAlignment = HorizontalAlignment.Left;
if (v == 1)
{
video_ship.Source = new Uri("videos/Lost_Ship.wmv", UriKind.RelativeOrAbsolute);
//video_ship.Play();
gr.Children.Add(video_ship);
}
else if (v == 2)
{
//video_ship.Source = "videos/Lost_Ship.wmv";
video_ship.Source = new Uri("videos/you_are_on_fire.wmv", UriKind.RelativeOrAbsolute);
//video_ship.Name = "fire";
//video_ship.Play();
gr.Children.Add(video_ship);
}
else if (v == 3)
{
//video_ship.SourceName = "videos/Lost_Ship.wmv";
video_ship.Source = new Uri("videos/EscapeShip.wmv", UriKind.RelativeOrAbsolute);
//video_ship.Name = "escape";
//video_ship.Play();
gr.Children.Add(video_ship);
}
I send a variable v to select one of my videos..I set the videos to Build Action Content
Any ideas what to do? or something different than this?
I want only to play the video ..Video does not have music ..or sound effect..
I tested it and it works. You want to popup video when you call event. so you shoud put grid inside file xaml code and it is hidden.
This is my test
on xaml
<Grid x:Name="LayoutRoot" Background="Transparent">
<Image Source="BackgroundMain3.jpg" Stretch="UniformToFill"/>
<Grid Height="400" Width="600" x:Name="playvideo" Visibility="Collapsed">
<MediaElement x:Name="element"/>
</Grid>
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="129,684,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
</Grid>
on xaml.cs
private void button1_Click(object sender, RoutedEventArgs e)
{
poupup(3);
playvideo.Visibility = Visibility.Visible;
playvideo.Opacity = 0.8;
element.MediaOpened += new RoutedEventHandler(element_MediaOpened);
}
void element_MediaOpened(object sender, RoutedEventArgs e)
{
element.Play();
}
public void poupup(int v)
{
if (v == 1)
{
element.Source = new Uri("videos/Lost_Ship.wmv", UriKind.RelativeOrAbsolute);
}
else if (v == 2)
{
element.Source = new Uri("videos/you_are_on_fire.wmv", UriKind.RelativeOrAbsolute);
}
else if (v == 3)
{
element.Source = new Uri("YouTube.mp4", UriKind.RelativeOrAbsolute);
}
}