How to set the Title of a Panorama control from value in a query string? - windows-phone-7

When I navigate to a view with a Panorama control I would like to set the Title of the Panorama control, the Title value is coming in with a query string from the previous view. How do I do that?

if you are navigating from other page:
page.xaml,cs:
private void Navigate()
{
string name = "Test";
// Navigate to Panorama page
NavigationService.Navigate(new Uri("/Pages/PanoramaPage.xaml?name=" + name, UriKind.Relative));
}
PanoramaPage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string name = "";
if (NavigationContext.QueryString.TryGetValue("name", out name))
{
PageTitle.Text = name;
}
}

Related

Save image locally and then save file path as a string into a database in Xamarin Forms

How do i after i take a picture/ select an image (either will overwrite the same image variable regardless), save the image locally and then save the file path as a string into a variable to be inserted into an existing SQLite db.
I had the same exact issue-
Here's how I got it to work
First, in your model where you've created the columns for your tables, make sure there is a property for the string that will be the imagepath
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Image { get; set; }
Then we need a method to upload and save your Image locally and another to get the file path and set it to a string. Assuming this will be in your code-behind, first add these:
public static string photofilename;
public static string imagePath;
static SQLiteConnection db;
YourModel yourmodel = new YourModel();
Then the LoadPhoto method
async void LoadPhotoAsync(FileResult photo)
{
// canceled
string PhotoPath;
if (photo == null)
{
PhotoPath = null;
return;
}
// save the file into local storage
var newFile = Path.Combine(FileSystem.AppDataDirectory, photo.FileName);
using (var stream = await photo.OpenReadAsync())
using (var newStream = File.OpenWrite(newFile))
await stream.CopyToAsync(newStream);
PhotoPath = newFile;
}
Then a method to upload/save the photo (In this case I'm having the user upload from the device) which I have attached to an Upload Image button. In this method, I display the image to the Image in my XAML called ImageViewer, but this might not be necessary for you.
async void Image_ClickedAsync(System.Object sender, System.EventArgs e)
{
try
{
var photo = await MediaPicker.PickPhotoAsync();
LoadPhotoAsync(photo);
photofilename = photo.FileName;
imagePath = Path.Combine(FileSystem.AppDataDirectory, photofilename);
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature is not supported on the device
}
catch (PermissionException pEx)
{
// Permissions not granted
}
catch (Exception ex)
{
Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
}
ImageViewer.Source = imagePath;
ImageViewer.IsVisible = true;
}
What this has done is open up MediaPicker, Allow the user to choose an Image and set the Image Path to the string imagePath. In my case here, I also have "ImageViewer" which is an Image in my XAML used to display the image, but we're not done yet- we haven't yet saved it to your SQLite db. Here's the method I used attached to a "Save" button-
private void SaveEvent(object sender, EventArgs e)
{
var databasePath = Path.Combine(FileSystem.AppDataDirectory, "yourdb.db");
db = new SQLiteConnection(databasePath);
yourmodel.Image = imagePath;
db.Insert(yourmodel);
}
Then, assuming your using ListView or something bound to the Tables in the db, you'll have an image in your XAML like so
<Image HeightRequest="340" WidthRequest="550" x:Name="Image" Source="{Binding Image}"/>
and that should do the trick- Let me know if this works for you!

Can a BindingContext object data binding object directly be changed on changing of an entry field?

I am new to xamarin, i hope someone can help me with this:
I have a sinple page with entry fields and data binding.
I have page A with a listview. When I click on an item, I get redirected to page B which has the form elements.
async void LvData_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem != null)
{
var secondPage = new ProfileDataPage();
secondPage.BindingContext = e.SelectedItem;
await Navigation.PushAsync(secondPage);
}
}
This works, and in page B the fields are filled with the right data.
So now I change the value of an entry field. Then I click on the Save Button and I do something like this (profileData = BindingContext object):
profileData.Height = Functions.ToNullableDouble(Height.Text);
profileData.Weight = Functions.ToNullableDouble(Weight.Text);
etc...
Doesn't the BindingContext know somehow that the value of the entry has changed, and I can just send the BindingContext object to my web api for save, update and so on?
Thank you very much.
for example,here is a mode:
class MyData : INotifyPropertyChanged
{
string height;
string weight;
public MyData(string height,string weight)
{
this.height= height;
this.weight= weight;
}
public string Height
{
set
{
if (height!= value)
{
height= value;
OnPropertyChanged("Height");
}
}
get
{
return height;
}
}
public string Weight
{
set
{
if (weight!= value)
{
weight= value;
OnPropertyChanged("Weight");
}
}
get
{
return weight;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
you could refer to Binding Mode
So I tried your solutions, which helped, but only after I have update my VS, all the nuget packages and so on..
came to this idea by this post: Link
I have no idea why this was necessary, but it works now!
Thank you!

How can I find out the value of an Id from a Tapped view cell event

I have code to call an event when a ViewCell is tapped. In the event handler I need to know the Id value from the cell that called the event. can someone help suggest how I can get this value.
Also I would like to pass this value to the categoriesPage that is being opened. How can I do this?
public class CategoryGroupWordCountVM
{
bool isToggled;
public int Id { get; set; }
public string Name { get; set; }
public bool IsToggled { get; set; }
public int TotalWordCount { get; set; }
}
List<CategoryGroupWordCountVM> categoryGroups;
foreach (var category in categoryGroups) {
var cell = new CategoryGroupTextCell { BindingContext = category };
cell.Tapped += openCategoriesPage;
section.Add(cell);
}
async void openCategoriesPage(object sender, EventArgs e)
{
var ctg = (CategoryGroupTextCell)sender;
var Id = ??
await Navigation.PushAsync(categoriesPage);
}
You can use BindingContext
For example: (assuming CategoryPageVM is viewmodel-type for the page you are navigating to on tapped event).
async void openCategoriesPage(object sender, EventArgs e)
{
var ctg = (CategoryGroupTextCell)sender;
// get id from binding-context
var id = (ctg.BindingContext as CategoryGroupWordCountVM)?.Id;
// construct or get viewmodel for the page you are navigating to
var newPageVM = new CategoryPageVM { CategoryId = id };
// assign viewmodel to page
var categoriesPage = new CategoryPage { BindingContext = newPageVM };
await Navigation.PushAsync(categoriesPage);
}
Also, this link offers more details regarding passing data during navigation.

How to get list item index when user clicks on the button which is in list item in windows phone 8

I've list box in my application.
Below is the screen shot.
When user clicks on the list item, then i'm displaying detailed page.
It is handling in below selection changed listener.
private void companiesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//get the selected item from list
Company selectedItem = (Company)e.AddedItems[0];
Uri uri = new Uri("/CompanyDetailsPage.xaml", UriKind.Relative);
//navigate to target page
this.NavigationService.Navigate(uri);
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = selectedItem;
}
}
Upto this it is fine.
Now when the user clicks on the Delete button which is on the item,
then i've to delete that item from the list.
private void Del_Btn_clicked(object sender, RoutedEventArgs e)
{
//get the Corresponding item from list i.e. On which delete button is placed.
//Delete saved company from the database
}
I'm unable to get that particular list item index on which the delete button is placed.
Ho could I get.
Thanks.
You can retrieve the button by casting the sender parameter. From there, you can retrieve the company by casting the DataContext property:
private void Del_Btn_clicked(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var company = (Company)button.DataContext;
// ...
}
do get the index of the listbox you can direct set the property
SelectedIndex = {Binding asd,Mode=TwoWay}
then in viewmodel
make a property
private int _asd;
public int asd
{
get
{
return _asd;
}
set
{
_asd= value;
}
}
by this you will get the index of selected item ...
hopes it might help ..

How to delete the listbox item in wp7?

Listbox having 2 buttons.When click on button need to delete the item from that listbox.
please tell me how to acheive that?
List<SampleCheckedData> interestrates = new List<SampleCheckedData>();
interestrates = (from rts in xmlDocu.Descendants("Friend")
select new SampleCheckedData
{
Id = (string)rts.Element("userid"),
Name = (string)rts.Element("name"),
Icon = (string)rts.Element("imageurl"),
VisibleStatus = (string)rts.Element("visiblestatus"),
AppStatus = (string)rts.Element("loginstatus"),
imgBubble =bitmapRed,
}).ToList<SampleCheckedData>();
this.lstImages.ItemsSource = interestrates;
private void btnAccept_MouseEnter(object sender, MouseEventArgs e)
{
int _id = int.Parse(((System.Windows.FrameworkElement)(e.OriginalSource)).Tag.ToString());
lstFriendRequuest.Items.RemoveAt(lstFriendRequuest.SelectedIndex);
}
To delete the selected item,
listbox.Items.RemoveAt(listbox.SelectedIndex);
Make your collection available globally on this page, and now you can manipulate on it easily from btnAccept_MouseEnter event:
public interestrates;
...
{
interestrates = ...
this.lstImages.ItemsSource = interestrates;
}
private void btnAccept_MouseEnter(object sender, MouseEventArgs e)
{
interestrates.RemoveAt(lstFriendRequuest.SelectedIndex);
}
Also, make sure that a click on a ListBox item changes SelectedIndex accordingly

Resources