How to delete items from a list in Windows Phone 8? - windows

I have created an app. One of the features in the app is making a list and saving to Isolated Storage.
To the point, my problem is with deleting items from the list. To clarify, the user of the app can add items to list, and then save it. I am also using a LongListMultiSelector, which I downloaded from the web. What happens here is you can check items in a special boxes that pops-up (LongListMultiSelector feature). Up to this point, every feature works well. I want the user to be able to check the checkboxes, and press delete button, much like Windows Phone Hotmail, and rid of them. This is the part where I am having trouble.
So I click the trash can button which should function as a delete button. I have the following code for it.
private void button3_Click(object sender, EventArgs e)
{
while (LLMS.SelectedItems.Count > 0)
{
source.Remove((MainList)LLMS.SelectedItems[0]);
InitializeComponent();
}
}
LLMS represents the LongListMultiSelector.
MainList is class and the list.
What happens is the app freezes up and stops working, though no pop-up comes up.

You can use this lines of code:
void OnRemoveClick(object sender, EventArgs e)
{
IList source = MainList.ItemsSource as IList;
while (MainList.SelectedItems.Count > 0)
{
source.Remove((**ObjectName**)MainList.SelectedItems[0]);
}
}

Related

Windows Form Listview is not visually showing selected items

We have a tool that is being integrated into our application. We have some strict borders around us too in that we cannot modify the application except for our extensions. I have searched here, I've searched the internet, but cannot find any postings about this problem.
I have a Windows Form that contains a ListView and our user requires we create a checkbox to Select/Deselect all. I have the event handler for when the check box state changes and call the routine to set everything to Selected.
private void SelectAllEventHandler(object sender, EventArgs e)
{
ChangeState(RadCapListView, SelectAllRadcap.Checked);
}
private void ChangeState(SWF.ListView control, bool state)
{
if (control.CheckBoxes)
{
control.Items.OfType<SWF.ListViewItem>().ToList()
.ForEach(item => item.Checked = state);
}
else
{
control.Items.OfType<SWF.ListViewItem>().ToList()
.ForEach(item => item.Selected = state);
}
control.Refresh();
}
Going into debug mode all items are marked as selected.
Also at the control level SelectedItems is properly updated.
The issue is that visually the control just will not highlight the selected items like we have our WPF forms doing. As you can see in the code I also tried to refresh the control hoping that would show items selected, but no joy.
Has anyone solved this problem in getting selected items to display properly?
Thank!
Instead of using control.Refresh(), try control.Focus().

how to not blocking loop in windows form

i'm pretty new in the .net programming and i would like to get some suggestions.
I'm trying to create an easy client GUI application using the VS2010 Designer to create a single form in which i have:
1 comboBox, (containing the list of possible commands)
1 button, (used to execute the command selected in the combobox)
1 picturebox (in which i display images received from my server application)
I was able to create my client application and display a different image in the picturebox received from the server everytime i press the button.
What i would like to do is a not blocking loop in the event button click so that as long as the the client combobox command is set to start imaging, the images sent by the server are displayed in the picturebox and it stops when the client combobox command is set to stop imaging.
I'm not sure about how to do that because if i try and loop in the event button click, the GUI becomes unresponsive and i don't have a chance to change the command in the combobox.
Any help would be much appreciated.
Thanks.
Here's a "cheap" way to update the GUI by using the ReportProgress feature of the BackgroundWorker class. First drop a BackgroundWorker object on your form. Then...
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.DoWork += DoWork;
backgroundWorker1.ProgressChanged += UpdateGui;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
System.Threading.Thread.Sleep(1000);
backgroundWorker1.ReportProgress(0);
}
}
void UpdateGui(object sender, ProgressChangedEventArgs e)
{
textBox1.Text = DateTime.Now.ToLongTimeString();
}

Windows Phone 7 Selection_Changed automatically

currently I'm developing an app for WP7 but came across a little problem with a Listbox event call Selection_Change. The problem is that when i return to the page that contains the listbox the selection_change event triggers without being changed at all or without any user input. The listbox code is similar to this:
private void lsbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lsbHistory.SelectedIndex;
NavigationService.Navigate(new Uri("/Views/NextPage, UriKind.Relative));
}
On the page I navigate to, the only way out of the navigated page is by pressing back button or start button meaning that it will return to the page that contains the listbox. When I Navigate back the selection change triggers leading me sometimes to a exception. Has anyone been through this before?
Consider always checking if it's -1 (the default value).
private void lsbHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = lsbHistory.SelectedIndex;
if (index != -1)
{
NavigationService.Navigate(new Uri("/Views/NextPage, UriKind.Relative));
lsbHistory.SelectedIndex = -1; // Set it to -1, to enable re-selection.
}
}
Also, you should consider wrapping the Navigate call in Dispatcher.BeginInvoke to have a better, more smooth, page transition.
The event will be fired when the list is populated.
The simplest solution for you will probably be to add a check that there is nothing selected before triggering your navigation:
if (lsbHistory.SelectedIndex > -1)
{
// do navigation
}
One thing to notice is that when you navigate back to the page which containt the ListBox, the ListBox still has the SelectedItem set to the value it had when the user navigated away. This means that lsbHistory.SelectedIndex will get the index of the item which was selected when the user navigated forward.
Maybe there's something in your code which presumes that the ListBox's SelectedItem is null when the user navigates to the page?

PhotoChooserTask + Navigation

I taken two Images & added event (MouseButtonDown) for them.
When first image handles event to open Gallery. Second image handles events for open camera.
When user has choosed his image from the gallery, I want to navigate to next page. Its navigates. But before completing navigation process, it displays MainPage & then moves toward next page. I didnt want to display the MainPage once user chooses the image from the gallery.
Plz help.
Thanks in advance.
public partial class MainPage : PhoneApplicationPage
{
PhotoChooserTask objPhotoChooser;
CameraCaptureTask cameraCaptureTask;
// Constructor
public MainPage()
{
InitializeComponent();
objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(objPhotoChooser_Completed);
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(objCameraCapture_Completed);
}
void objPhotoChooser_Completed(object sender, PhotoResult e)
{
if (e != null && e.TaskResult == TaskResult.OK)
{
//Take JPEG stream and decode into a WriteableBitmap object
App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
//Delay navigation until the first navigated event
NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
}
}
void navigateCompleted(object sender, EventArgs e)
{
//Do the delayed navigation from the main page
this.NavigationService.Navigate(new Uri("/ImageViewer.xaml", UriKind.RelativeOrAbsolute));
NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
}
void objCameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//Take JPEG stream and decode into a WriteableBitmap object
App.CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
//Delay navigation until the first navigated event
NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
}
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
objPhotoChooser.Show();
}
private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
cameraCaptureTask.Show();
}
To my knowledge when you use one of the choosers, like the Photo gallery or the camera, when your application is activating it will take it back to the page you left it. I don't think there is a way to get around this itself. What you would have to do is catch the Activating event in your main page code and Navigate to the desired page from there.
Now I am not completely sure how you would pass the image from the MainPage to the target page. It does not look like there is a property in the Navigation service to store this value. But you could either set it in an application wide variable, ModelView or even store it in the Isolated Storage area.
You could work around this by navigating to an intermediate blank page and have that intermediate page launch the tasks. When the tasks are completed you can then navigate as normal to your new page and only this blank page will show in transit.
Chris is correct that some of the tasks will navigate away from your app (effectively tombstoning it) and will the re-activate your application when the user returns from the task. For the camera this is particularly difficult, as to my knowledge there is no simple way to detect when you are returning from the camera. Also the camera doesn't work when attached to the debugger or Zune software (at least this is true on my HTC Surround), which makes troubleshooting quite difficult!
In my WP7 Barcode Scanning application I ended up using flags on the PhoneApplicationService class to help track where the navigation events are coming from. Something like:
PhoneApplicationService.Current.State["ReturnFromSampleChooser"] = true;
You can then check for these flags in the PhoneApplicationPage_Loaded or OnNavigatedTo method of your main page and redirect to the desired page as needed. Just make sure to clear the flag and be careful to not cause any loops in the navigation, as that might make your app fail certification (back button must ALWAYS work correctly).
For an example of how to use the camera and set/clear flags using PhoneApplicationService check out the source code for the Silverlight ZXing Barcode Library. You can download the full source here or browse the files online.

Windows Phone 7 TopProperty Changed event

I want to do some code after Canvas.topProperty is changed in my control.
I've seen examples with DependencyPropertyDescriptor but Visual studio do not recognize this class in Windows Phone 7 project.
How can I manage this problem?
What you need to look at is the LayoutUpdated event handler for the canvas.
First of all you need to store the value of the top margin in a separate field/property.
double top = 0;
top = YourCanvas.Margin.Top;
Then, in the LayoutUpdated event handler you can check whether the margin was modified:
void LayoutRoot_LayoutUpdated(object sender, EventArgs e)
{
if (LayoutRoot.Margin.Top != top)
{
// Your action here
Debug.WriteLine("CHANGED!");
}
}

Resources