In the Windows Phone 7.5 application I'm creating, I've got a problem with binding a navigationservice to a button. The button is inside a ListBox datatemplate in XAML, which is populated from codebehind from a JSON deserializer:
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
List<JSON> newslistJson = JsonConvert.DeserializeObject<List<JSON>>(e.Result);
this.NewsList.ItemsSource = newslistJson;
}
The NewsList listbox is here populated via the code above, and a class file containing and the "getters" and "setters". However, inside the ListBox and it's DataTemplate, as stated earlier, there's a button:
<Button x:Name="toNewsSite" Grid.Row="1" Content="Read MoreĀ»" Height="auto" Width="auto" FontSize="19" Foreground="#FFFFFF"/>
This button should, via the first code snippet, navigate to each of the items' news_id, which is a string in the class file handling the public getters and setters.
So my dream scenario here would be, in codebehind, something like this inside the webClient_DownloadString...():
toNewsSite.NavigateService = ("TheNewPage.xaml/news?id={0}", JSON.news_id);
Aaand from there each of the news items in the listbox will have an individual button with a parameter stating which news_id it has, which will be fetched on the "TheNewPage" page.
Would this actually work?
First of all, Button control do not have any NavigationService. Inorder to navigate to navigate to a new page, you need to call NavigationService.Navigate() method
So, What i would do is to bind the id to the Tag of the Button
<Button x:Name="toNewsSite" Grid.Row="1" Click="OnButtonClick" Tag="{Binding news_id}" Content="Read MoreĀ»" Height="auto" Width="auto" FontSize="19" Foreground="#FFFFFF"/>
private void OnButtonClick(object sender, RoutedEventArgs e)
{
Button bt = (Button)sender;
NavigationService.Navigate(new Uri("TheNewPage.xaml/news?id=" + bt.Tag.ToString(), UriKind.Relative));
}
In the click handler for the button grab the DataContext of the button and you'll be able to access the
private void OnButtonClick(object sender, RoutedEventArgs e)
{
JSON item = (sender as FrameworkElement).DataContext;
NavigationService.Navigate(new Uri("TheNewPage.xaml/news?id=" + item.news_id, UriKind.Relative));
}
As a further improvement, rather than adding a button to the ItemTemplate, I'd handle the tap event of whatever you put at the root of the DataTemplate (I'd guess a Grid or StackPanel).
Related
In Windows Phone 8 (or 8.1), is there a way to prevent a ContentDialog from closing when the primary button is pressed under certain conditions? For example, say you have a Boolean done. When they click the primary button, I only want the ContentDialog to close if (done == true). Is this possible?
XAML:
<ContentDialog
...
Closing="ContentDialog_Closing">
C#:
private async void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
if (!CanClose)
args.Cancel = true;
}
Using the new AutoSuggestBox control in Windows Phone 8.1 (WinRT XAML), I am trying to keep the suggestion box open all the time -- even after the user clicks a suggestion.
I have no problem starting with the suggestion box open by programmatically setting AutoSuggestBox.IsSuggestionListOpen = true;
Then I hook the SuggestionChosen event like this:
private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) {
sender.Text = args.SelectedItem.ToString();
sender.IsSuggestionListOpen = true;
}
But unfortunately the suggestion box still closes after selecting an item, even though I set IsSuggestionListOpen to true.
Any help with getting it to stay open after a selection would be appreciated.
The solution I found to this is to hook the LayoutUpdated event.
I have the AutoSuggestBox in a PickerFlyout, so I only want the suggestion box open if the PickerFlyout is open (obviously). So I set a Tag property on the button that opens the PickerFlyout to identify if the PickerFlyout is open or closed. Then in the LayoutUpdated event of the AutoSuggestBox I set the IsSuggestionListOpen property to true if the PickerFlyout is open (and false if it's not).
The code:
private void PickerFlyout_Opened(object sender, object e) {
ActivatePickerFlyoutButton.Tag = "open";
}
private void PickerFlyout_Closed(object sender, object e) {
ActivatePickerFlyoutButton.Tag = "closed";
}
private void AutoSuggestBox_LayoutUpdated(object sender, object e) {
AutoSuggestBox.IsSuggestionListOpen = ((ActivatePickerFlyoutButton.Tag as string).Equals("open"));
}
That is the only place I need to set the IsSuggestionListOpen property, since the LayoutUpdated event fires at all the right times.
I've got a Flyout embedded within an AppBarButton like so:
<AppBarButton x:Name="appbarbtnOpenPhotosets" Icon="OpenFile" Label="Open Existing Photoset[s]" AutomationProperties.Name="Open File" Tapped="appbarbtnOpenPhotosets_Tapped" >
<Button.Flyout>
. . .
</Button.Flyout>
</AppBarButton>
I want to, under certain circumstances, first present the user with an opportunity to rename a file prior to seeing the Flyout. I tried seeing if that would work like this:
async private void appbarbtnOpenPhotosets_Tapped(object sender, TappedRoutedEventArgs args)
{
// Want to conditionally postpone the operation
bool myBucketsGotAHoleInIt = PhotraxUtils.GetLocalSetting(CAINT_BUY_NO_BEER);
if (myBucketsGotAHoleInIt)
{
MessageDialog dlgDone = new MessageDialog("Can you see me now?");
await dlgDone.ShowAsync();
args.Handled = false; // <= adding this made no difference
}
}
This works, in that I see the "Can you see me now?" dialog, but that prevents the Flyout from flying out. A Flyout that doesn't fly out is no more useful than a flying squirrel or fish that doesn't motate through the air.
So how can I temporarily suppress my flyout but then call it forth? The Flyout does not have an Open() method...Is there some other way to invoke it?
Flyouts attached to Buttons open automatically when you click the control.
If you don't want it to open automatically, you need to attach it to another control.
Example taken from official documentation:
<!-- Flyout declared inline on a FrameworkElement -->
<TextBlock>
<FlyoutBase.AttachedFlyout>
<Flyout>
<!-- Flyout content -->
</Flyout>
</FlyoutBase.AttachedFlyout>
</TextBlock>
Then you can show the Flyout whenever you want, calling FlayoutBase.ShowAttachedFlyout() and passing the FrameworkElement casted value of your control.
FlyoutBase.ShowAttachedFlyout(frameworkElement);
So, in your case:
async private void appbarbtnOpenPhotosets_Tapped(object sender, TappedRoutedEventArgs args)
{
// Want to conditionally postpone the operation
bool myBucketsGotAHoleInIt = PhotraxUtils.GetLocalSetting(CAINT_BUY_NO_BEER);
if (myBucketsGotAHoleInIt)
{
MessageDialog dlgDone = new MessageDialog("Can you see me now?");
await dlgDone.ShowAsync();
// New code
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}
}
If you can't change the control, you should able to use the code I posted with Buttoninstead of TextBlock. I'm not sure about this, but you can try.
I am developing windows phone application. I am retrieving event images from Facebook.
Problem:
how to take detail information about the event like date place of image by clicking on that image
If you are using listbox then
private void YourList_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
FaceBookClass fbc = YourEventList.SelectedItem as FaceBookClass ;
String date=fbc.Date;
String place=fbc.Place;
}
I have two pages Main.xaml which includes only a button and another page timer.xaml which includes a timer. After pressing the button in the main page I want to go to the another page and start timer. I am using following code:
enter code here
**Main Page:**
private void Start_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/timer.xaml", UriKind.Relative));
}
**timer Page:**
public Page1()
{
InitializeComponent();
dispatcherTimer.Interval = new TimeSpan(0, 0, 1, 0, 0);
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Start();
counter=0;
count.Text = counter.ToString();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
counter++;
count.Text = counter.ToString();
}
I can not see time in the timer page however, after pressing the button it will navigate to the timer page correctly but I can not see updates in my timer page. I am debugging my code and it seems that DistpacherTimer() works correctly but my timer page does not update. Do you know how can I fix this problem?
Finally, I could find the problem. the code that you can see above was not whole code that I have written. Actually, in my real code I was sending a list <> from main page to timer page. Passing data from one page to another page is a little tricky. My main problem was that I did not send the data from main page to timer page in correct way, so that caused problem. Thus, there was nothing wrong with my DispatcherTimer() class or Event handler.
In other words, the code above works well if you do not pass data (e.g. list, array and so on) from a page to another page in a wrong way.